blob: 2e005c0b476d8083a95216efb3c3950df6c7898e [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-Pehrsonf10fa3c2010-04-29 08:25:29 -05004 * Last changed in libpng 1.5.0 [April 29, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060016/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrsoncb096a42010-04-28 13:10:47 -050017typedef version_1_5_0beta23 Your_png_h_is_not_version_1_5_0beta23;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060018
Andreas Dilger47a0c421997-05-16 02:46:07 -050019/* Version information for C files. This had better match the version
Glenn Randers-Pehrson5ade7ed2009-09-30 15:11:49 -050020 * string defined in png.h.
21 */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060022
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060023/* Tells libpng that we have already handled the first "num_bytes" bytes
24 * of the PNG file signature. If the PNG data is embedded into another
25 * stream we can set num_bytes = 8 so that libpng will not attempt to read
26 * or write any of the magic bytes before it starts on the IHDR.
27 */
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050028
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060029#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050030void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060031png_set_sig_bytes(png_structp png_ptr, int num_bytes)
32{
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -050033 png_debug(1, "in png_set_sig_bytes");
34
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050035 if (png_ptr == NULL)
36 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050037
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038 if (num_bytes > 8)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050039 png_error(png_ptr, "Too many bytes for PNG signature");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060040
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -050041 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060042}
43
44/* Checks whether the supplied bytes match the PNG signature. We allow
45 * checking less than the full 8-byte signature so that those apps that
46 * already read the first few bytes of a file to determine the file type
47 * can simply check the remaining bytes for extra assurance. Returns
48 * an integer less than, equal to, or greater than zero if sig is found,
49 * respectively, to be less than, to match, or be greater than the correct
50 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
51 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050052int PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050053png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060055 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060056 if (num_to_check > 8)
57 num_to_check = 8;
58 else if (num_to_check < 1)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060059 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060
Andreas Dilger47a0c421997-05-16 02:46:07 -050061 if (start > 7)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060062 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063
64 if (start + num_to_check > 8)
65 num_to_check = 8 - start;
66
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050067 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068}
69
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060070#endif /* PNG_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -050071
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060072#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050073/* Function to allocate memory for zlib and clear it to 0. */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -050074voidpf /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050075png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -050076{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050077 png_voidp ptr;
78 png_structp p=(png_structp)png_ptr;
79 png_uint_32 save_flags=p->flags;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050080 png_alloc_size_t num_bytes;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050081
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050082 if (png_ptr == NULL)
83 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050084 if (items > PNG_UINT_32_MAX/size)
85 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050086 png_warning (p, "Potential overflow in png_zalloc()");
87 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050088 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050089 num_bytes = (png_alloc_size_t)items * size;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060090
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050091 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
92 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
93 p->flags=save_flags;
Guy Schalnat6d764711995-12-19 03:22:19 -060094
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050095 return ((voidpf)ptr);
96}
97
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050098/* Function to free memory for zlib */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -050099void /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500100png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600102 png_free((png_structp)png_ptr, (png_voidp)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500103}
104
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105/* Reset the CRC variable to 32 bits of 1's. Care must be taken
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600106 * in case CRC is > 32 bits to leave the top bits 0.
107 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500108void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600109png_reset_crc(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500110{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600111 png_ptr->crc = crc32(0, Z_NULL, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500112}
113
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114/* Calculate the CRC over a section of data. We can only pass as
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600115 * much data to this routine as the largest single buffer size. We
116 * also check that this data will actually be used before going to the
117 * trouble of calculating it.
118 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500119void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500121{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500122 int need_crc = 1;
123
124 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
125 {
126 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
127 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
128 need_crc = 0;
129 }
130 else /* critical */
131 {
132 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
133 need_crc = 0;
134 }
135
136 if (need_crc)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600137 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500138}
Guy Schalnate5a37791996-06-05 15:50:50 -0500139
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600140/* Allocate the memory for an info_struct for the application. We don't
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600141 * really need the png_ptr, but it could potentially be useful in the
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500142 * future. This should be used in favour of malloc(png_sizeof(png_info))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600143 * and png_info_init() so that applications that want to use a shared
144 * libpng don't have to be recompiled if png_info changes size.
145 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500146png_infop PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500147png_create_info_struct(png_structp png_ptr)
148{
149 png_infop info_ptr;
150
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500151 png_debug(1, "in png_create_info_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500152
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153 if (png_ptr == NULL)
154 return (NULL);
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500155
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500156#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600157 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
158 png_ptr->malloc_fn, png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500159#else
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600160 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500161#endif
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600162 if (info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500163 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnate5a37791996-06-05 15:50:50 -0500164
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600165 return (info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500166}
167
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168/* This function frees the memory associated with a single info struct.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600169 * Normally, one would use either png_destroy_read_struct() or
170 * png_destroy_write_struct() to free an info struct, but this may be
171 * useful for some applications.
172 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500173void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
175{
176 png_infop info_ptr = NULL;
177
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500178 png_debug(1, "in png_destroy_info_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500179
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500180 if (png_ptr == NULL)
181 return;
182
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184 info_ptr = *info_ptr_ptr;
185
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600187 {
188 png_info_destroy(png_ptr, info_ptr);
189
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500190#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500191 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
192 png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500193#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500195#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500196 *info_ptr_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600197 }
198}
199
200/* Initialize the info structure. This is now an internal function (0.89)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600201 * and applications using it are urged to use png_create_info_struct()
202 * instead.
203 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500204
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500205void PNGAPI
206png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
207{
208 png_infop info_ptr = *ptr_ptr;
209
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500210 png_debug(1, "in png_info_init_3");
211
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500212 if (info_ptr == NULL)
213 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600214
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500215 if (png_sizeof(png_info) > png_info_struct_size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500216 {
217 png_destroy_struct(info_ptr);
218 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
219 *ptr_ptr = info_ptr;
220 }
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500221
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500222 /* Set everything to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500223 png_memset(info_ptr, 0, png_sizeof(png_info));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224}
225
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500226void PNGAPI
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500227png_data_freer(png_structp png_ptr, png_infop info_ptr,
228 int freer, png_uint_32 mask)
229{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500230 png_debug(1, "in png_data_freer");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500231
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500232 if (png_ptr == NULL || info_ptr == NULL)
233 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500234
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500235 if (freer == PNG_DESTROY_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500236 info_ptr->free_me |= mask;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500237 else if (freer == PNG_USER_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500238 info_ptr->free_me &= ~mask;
239 else
240 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500241 "Unknown freer parameter in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500242}
243
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500244void PNGAPI
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500245png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
246 int num)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500248 png_debug(1, "in png_free_data");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500249
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600250 if (png_ptr == NULL || info_ptr == NULL)
251 return;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600252
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500253#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500254 /* Free text item num or (if num == -1) all text items */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500255 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600256 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500257 if (num != -1)
258 {
259 if (info_ptr->text && info_ptr->text[num].key)
260 {
261 png_free(png_ptr, info_ptr->text[num].key);
262 info_ptr->text[num].key = NULL;
263 }
264 }
265 else
266 {
267 int i;
268 for (i = 0; i < info_ptr->num_text; i++)
269 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
270 png_free(png_ptr, info_ptr->text);
271 info_ptr->text = NULL;
272 info_ptr->num_text=0;
273 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600274 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600275#endif
276
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500277#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500278 /* Free any tRNS entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500279 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500280 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500281 png_free(png_ptr, info_ptr->trans_alpha);
282 info_ptr->trans_alpha = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500283 info_ptr->valid &= ~PNG_INFO_tRNS;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500284 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285#endif
286
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500287#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500288 /* Free any sCAL entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500289 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500290 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600291#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500292 png_free(png_ptr, info_ptr->scal_s_width);
293 png_free(png_ptr, info_ptr->scal_s_height);
294 info_ptr->scal_s_width = NULL;
295 info_ptr->scal_s_height = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600296#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500297 info_ptr->valid &= ~PNG_INFO_sCAL;
298 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600299#endif
300
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500301#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500302 /* Free any pCAL entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500303 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500304 {
305 png_free(png_ptr, info_ptr->pcal_purpose);
306 png_free(png_ptr, info_ptr->pcal_units);
307 info_ptr->pcal_purpose = NULL;
308 info_ptr->pcal_units = NULL;
309 if (info_ptr->pcal_params != NULL)
310 {
311 int i;
312 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
313 {
314 png_free(png_ptr, info_ptr->pcal_params[i]);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600315 info_ptr->pcal_params[i] = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 }
317 png_free(png_ptr, info_ptr->pcal_params);
318 info_ptr->pcal_params = NULL;
319 }
320 info_ptr->valid &= ~PNG_INFO_pCAL;
321 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322#endif
323
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500324#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500325 /* Free any iCCP entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500326 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500327 {
328 png_free(png_ptr, info_ptr->iccp_name);
329 png_free(png_ptr, info_ptr->iccp_profile);
330 info_ptr->iccp_name = NULL;
331 info_ptr->iccp_profile = NULL;
332 info_ptr->valid &= ~PNG_INFO_iCCP;
333 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600334#endif
335
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500336#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500337 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500338 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600339 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500340 if (num != -1)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500341 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500342 if (info_ptr->splt_palettes)
343 {
344 png_free(png_ptr, info_ptr->splt_palettes[num].name);
345 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
346 info_ptr->splt_palettes[num].name = NULL;
347 info_ptr->splt_palettes[num].entries = NULL;
348 }
349 }
350 else
351 {
352 if (info_ptr->splt_palettes_num)
353 {
354 int i;
355 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
356 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
357
358 png_free(png_ptr, info_ptr->splt_palettes);
359 info_ptr->splt_palettes = NULL;
360 info_ptr->splt_palettes_num = 0;
361 }
362 info_ptr->valid &= ~PNG_INFO_sPLT;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500363 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365#endif
366
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500367#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500368 if (png_ptr->unknown_chunk.data)
369 {
370 png_free(png_ptr, png_ptr->unknown_chunk.data);
371 png_ptr->unknown_chunk.data = NULL;
372 }
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500373
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500374 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500376 if (num != -1)
377 {
378 if (info_ptr->unknown_chunks)
379 {
380 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
381 info_ptr->unknown_chunks[num].data = NULL;
382 }
383 }
384 else
385 {
386 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600387
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500388 if (info_ptr->unknown_chunks_num)
389 {
390 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
391 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600392
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500393 png_free(png_ptr, info_ptr->unknown_chunks);
394 info_ptr->unknown_chunks = NULL;
395 info_ptr->unknown_chunks_num = 0;
396 }
397 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600398 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399#endif
400
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500401#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500402 /* Free any hIST entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500403 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500404 {
405 png_free(png_ptr, info_ptr->hist);
406 info_ptr->hist = NULL;
407 info_ptr->valid &= ~PNG_INFO_hIST;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500408 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600409#endif
410
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500411 /* Free any PLTE entry that was internally allocated */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500412 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500413 {
414 png_zfree(png_ptr, info_ptr->palette);
415 info_ptr->palette = NULL;
416 info_ptr->valid &= ~PNG_INFO_PLTE;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500417 info_ptr->num_palette = 0;
418 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600419
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500420#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500421 /* Free any image bits attached to the info structure */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500422 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500423 {
424 if (info_ptr->row_pointers)
425 {
426 int row;
427 for (row = 0; row < (int)info_ptr->height; row++)
428 {
429 png_free(png_ptr, info_ptr->row_pointers[row]);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600430 info_ptr->row_pointers[row] = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500431 }
432 png_free(png_ptr, info_ptr->row_pointers);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600433 info_ptr->row_pointers = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500434 }
435 info_ptr->valid &= ~PNG_INFO_IDAT;
436 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600437#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500438
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500439 if (num == -1)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500440 info_ptr->free_me &= ~mask;
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500441 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500442 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600443}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600444
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600445/* This is an internal routine to free any memory that the info struct is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 * pointing to before re-using it or freeing the struct itself. Recall
447 * that png_free() checks for NULL pointers for us.
448 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500449void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600450png_info_destroy(png_structp png_ptr, png_infop info_ptr)
451{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500452 png_debug(1, "in png_info_destroy");
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600453
454 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600455
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500456#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600457 if (png_ptr->num_chunk_list)
458 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500459 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600460 png_ptr->chunk_list = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500461 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600462 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600463#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600464
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500465 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500466}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600467#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500468
Guy Schalnate5a37791996-06-05 15:50:50 -0500469/* This function returns a pointer to the io_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600470 * functions. The application should free any memory associated with this
471 * pointer before png_write_destroy() or png_read_destroy() are called.
472 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500473png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500474png_get_io_ptr(png_structp png_ptr)
475{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500476 if (png_ptr == NULL)
477 return (NULL);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600478 return (png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500479}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500480
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600481#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600482# ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483/* Initialize the default input/output functions for the PNG file. If you
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600484 * use your own read or write routines, you can call either png_set_read_fn()
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500485 * or png_set_write_fn() instead of png_init_io(). If you have defined
486 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
487 * necessarily available.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600488 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500489void PNGAPI
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500490png_init_io(png_structp png_ptr, png_FILE_p fp)
Guy Schalnate5a37791996-06-05 15:50:50 -0500491{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500492 png_debug(1, "in png_init_io");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500493
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500494 if (png_ptr == NULL)
495 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500496
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 png_ptr->io_ptr = (png_voidp)fp;
498}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600499# endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500500
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600501# ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502/* Convert the supplied time into an RFC 1123 string suitable for use in
503 * a "Creation Time" or other text-based time string.
504 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500505png_charp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500506png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
507{
508 static PNG_CONST char short_months[12][4] =
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600509 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
510 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500511
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500512 if (png_ptr == NULL)
513 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500514 if (png_ptr->time_buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500515 {
516 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
517 png_sizeof(char)));
518 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500519
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600520# ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500521 {
522 char near_time_buf[29];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500523 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500524 ptime->day % 32, short_months[(ptime->month - 1) % 12],
525 ptime->year, ptime->hour % 24, ptime->minute % 60,
526 ptime->second % 61);
527 png_memcpy(png_ptr->time_buffer, near_time_buf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500528 29*png_sizeof(char));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500529 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600530# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500531 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500532 ptime->day % 32, short_months[(ptime->month - 1) % 12],
533 ptime->year, ptime->hour % 24, ptime->minute % 60,
534 ptime->second % 61);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600535# endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500536 return ((png_charp)png_ptr->time_buffer);
537}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600538# endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600539
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600540#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600541
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500542png_charp PNGAPI
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600543png_get_copyright(png_structp png_ptr)
544{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500545 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500546#ifdef PNG_STRING_COPYRIGHT
547 return PNG_STRING_COPYRIGHT
548#else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600549# ifdef __STDC__
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500550 return ((png_charp) PNG_STRING_NEWLINE \
Glenn Randers-Pehrsonf10fa3c2010-04-29 08:25:29 -0500551 "libpng version 1.5.0beta23 - April 29, 2010" PNG_STRING_NEWLINE \
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600552 "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500553 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
554 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
555 PNG_STRING_NEWLINE);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600556# else
Glenn Randers-Pehrsonf10fa3c2010-04-29 08:25:29 -0500557 return ((png_charp) "libpng version 1.5.0beta23 - April 29, 2010\
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600558 Copyright (c) 1998-2010 Glenn Randers-Pehrson\
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500559 Copyright (c) 1996-1997 Andreas Dilger\
560 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600561# endif
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500562#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600563}
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500564
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600565/* The following return the library version as a short string in the
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500566 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
567 * used with your application, print out PNG_LIBPNG_VER_STRING, which
568 * is defined in png.h.
569 * Note: now there is no difference between png_get_libpng_ver() and
570 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
571 * it is guaranteed that png.c uses the correct version of png.h.
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600572 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500573png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600574png_get_libpng_ver(png_structp png_ptr)
575{
576 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson4c8f7262010-03-16 19:30:01 -0500577 return png_get_header_ver(png_ptr);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600578}
579
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500580png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600581png_get_header_ver(png_structp png_ptr)
582{
583 /* Version of *.h files used when building libpng */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500584 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500585 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600586}
587
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500588png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600589png_get_header_version(png_structp png_ptr)
590{
591 /* Returns longer string containing both version and date */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500592 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500593#ifdef __STDC__
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500594 return ((png_charp) PNG_HEADER_VERSION_STRING
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600595# ifndef PNG_READ_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500596 " (NO READ SUPPORT)"
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600597# endif
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500598 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500599#else
600 return ((png_charp) PNG_HEADER_VERSION_STRING);
601#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600602}
603
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600604#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600605# ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600606int PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600607png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
608{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500609 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600610 int i;
611 png_bytep p;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500612 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600613 return 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500614 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
615 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600616 if (!png_memcmp(chunk_name, p, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500617 return ((int)*(p + 4));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600618 return 0;
619}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600620# endif
Glenn Randers-Pehrsonf10fa3c2010-04-29 08:25:29 -0500621#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500622
Glenn Randers-Pehrsonf10fa3c2010-04-29 08:25:29 -0500623#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500624/* This function, added to libpng-1.0.6g, is untested. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500625int PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500626png_reset_zstream(png_structp png_ptr)
627{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500628 if (png_ptr == NULL)
629 return Z_STREAM_ERROR;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500630 return (inflateReset(&png_ptr->zstream));
631}
Glenn Randers-Pehrsonf10fa3c2010-04-29 08:25:29 -0500632#endif /* PNG_WRITE_SUPPORTED */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500633
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600634/* This function was added to libpng-1.0.7 */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500635png_uint_32 PNGAPI
636png_access_version_number(void)
637{
638 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500639 return((png_uint_32) PNG_LIBPNG_VER);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500640}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600641
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500642
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500643
644#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600645# ifdef PNG_SIZE_T
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500646/* Added at libpng version 1.2.6 */
647 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
648png_size_t PNGAPI
649png_convert_size(size_t size)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500650{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500651 if (size > (png_size_t)-1)
652 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
653 return ((png_size_t)size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500654}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600655# endif /* PNG_SIZE_T */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600656
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600657/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600658# ifdef PNG_cHRM_SUPPORTED
659# ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600660
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600661/*
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500662 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
663 * arithmetic, to produce a 64 bit result in the HI/LO words.
664 *
665 * A B
666 * x C D
667 * ------
668 * AD || BD
669 * AC || CB || 0
670 *
671 * where A and B are the high and low 16-bit words of V1,
672 * C and D are the 16-bit words of V2, AD is the product of
673 * A and D, and X || Y is (X << 16) + Y.
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600674*/
675
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500676void /* PRIVATE */
677png_64bit_product (long v1, long v2, unsigned long *hi_product,
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600678 unsigned long *lo_product)
679{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500680 int a, b, c, d;
681 long lo, hi, x, y;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600682
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500683 a = (v1 >> 16) & 0xffff;
684 b = v1 & 0xffff;
685 c = (v2 >> 16) & 0xffff;
686 d = v2 & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600687
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500688 lo = b * d; /* BD */
689 x = a * d + c * b; /* AD + CB */
690 y = ((lo >> 16) & 0xffff) + x;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600691
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500692 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
693 hi = (y >> 16) & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600694
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500695 hi += a * c; /* AC */
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600696
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500697 *hi_product = (unsigned long)hi;
698 *lo_product = (unsigned long)lo;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600699}
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500700
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500701int /* PRIVATE */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600702png_check_cHRM_fixed(png_structp png_ptr,
703 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
704 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
705 png_fixed_point blue_x, png_fixed_point blue_y)
706{
707 int ret = 1;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600708 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600709
710 png_debug(1, "in function png_check_cHRM_fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500711
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600712 if (png_ptr == NULL)
713 return 0;
714
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600715 if (white_x < 0 || white_y <= 0 ||
716 red_x < 0 || red_y < 0 ||
717 green_x < 0 || green_y < 0 ||
718 blue_x < 0 || blue_y < 0)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600719 {
720 png_warning(png_ptr,
721 "Ignoring attempt to set negative chromaticity value");
722 ret = 0;
723 }
724 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
725 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
726 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
727 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
728 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
729 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
730 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
731 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
732 {
733 png_warning(png_ptr,
734 "Ignoring attempt to set chromaticity value exceeding 21474.83");
735 ret = 0;
736 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600737 if (white_x > 100000L - white_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600738 {
739 png_warning(png_ptr, "Invalid cHRM white point");
740 ret = 0;
741 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600742 if (red_x > 100000L - red_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600743 {
744 png_warning(png_ptr, "Invalid cHRM red point");
745 ret = 0;
746 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600747 if (green_x > 100000L - green_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600748 {
749 png_warning(png_ptr, "Invalid cHRM green point");
750 ret = 0;
751 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600752 if (blue_x > 100000L - blue_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600753 {
754 png_warning(png_ptr, "Invalid cHRM blue point");
755 ret = 0;
756 }
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600757
758 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
759 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
760
761 if (xy_hi == yx_hi && xy_lo == yx_lo)
762 {
763 png_warning(png_ptr,
764 "Ignoring attempt to set cHRM RGB triangle with zero area");
765 ret = 0;
766 }
767
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600768 return ret;
769}
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600770# endif /* PNG_CHECK_cHRM_SUPPORTED */
771# endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500772
773void /* PRIVATE */
774png_check_IHDR(png_structp png_ptr,
775 png_uint_32 width, png_uint_32 height, int bit_depth,
776 int color_type, int interlace_type, int compression_type,
777 int filter_type)
778{
779 int error = 0;
780
781 /* Check for width and height valid values */
782 if (width == 0)
783 {
784 png_warning(png_ptr, "Image width is zero in IHDR");
785 error = 1;
786 }
787
788 if (height == 0)
789 {
790 png_warning(png_ptr, "Image height is zero in IHDR");
791 error = 1;
792 }
793
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600794# ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500795 if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600796# else
Glenn Randers-Pehrsondd66f3e2009-09-30 14:58:28 -0500797 if (width > PNG_USER_WIDTH_MAX)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600798# endif
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500799 {
800 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
801 error = 1;
802 }
803
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600804# ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsondd66f3e2009-09-30 14:58:28 -0500805 if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600806# else
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500807 if (height > PNG_USER_HEIGHT_MAX)
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600808# endif
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500809 {
810 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
811 error = 1;
812 }
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500813
Glenn Randers-Pehrsondd66f3e2009-09-30 14:58:28 -0500814 if (width > PNG_UINT_31_MAX)
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500815 {
Glenn Randers-Pehrsondd66f3e2009-09-30 14:58:28 -0500816 png_warning(png_ptr, "Invalid image width in IHDR");
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500817 error = 1;
818 }
819
820 if ( height > PNG_UINT_31_MAX)
821 {
Glenn Randers-Pehrsondd66f3e2009-09-30 14:58:28 -0500822 png_warning(png_ptr, "Invalid image height in IHDR");
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500823 error = 1;
824 }
825
826 if ( width > (PNG_UINT_32_MAX
827 >> 3) /* 8-byte RGBA pixels */
828 - 64 /* bigrowbuf hack */
829 - 1 /* filter byte */
830 - 7*8 /* rounding of width to multiple of 8 pixels */
831 - 8) /* extra max_pixel_depth pad */
832 png_warning(png_ptr, "Width is too large for libpng to process pixels");
833
834 /* Check other values */
835 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
836 bit_depth != 8 && bit_depth != 16)
837 {
838 png_warning(png_ptr, "Invalid bit depth in IHDR");
839 error = 1;
840 }
841
842 if (color_type < 0 || color_type == 1 ||
843 color_type == 5 || color_type > 6)
844 {
845 png_warning(png_ptr, "Invalid color type in IHDR");
846 error = 1;
847 }
848
849 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
850 ((color_type == PNG_COLOR_TYPE_RGB ||
851 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
852 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
853 {
854 png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
855 error = 1;
856 }
857
858 if (interlace_type >= PNG_INTERLACE_LAST)
859 {
860 png_warning(png_ptr, "Unknown interlace method in IHDR");
861 error = 1;
862 }
863
864 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
865 {
866 png_warning(png_ptr, "Unknown compression method in IHDR");
867 error = 1;
868 }
869
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600870# ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500871 /* Accept filter_method 64 (intrapixel differencing) only if
872 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
873 * 2. Libpng did not read a PNG signature (this filter_method is only
874 * used in PNG datastreams that are embedded in MNG datastreams) and
875 * 3. The application called png_permit_mng_features with a mask that
876 * included PNG_FLAG_MNG_FILTER_64 and
877 * 4. The filter_method is 64 and
878 * 5. The color_type is RGB or RGBA
879 */
880 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
881 png_ptr->mng_features_permitted)
882 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
883
884 if (filter_type != PNG_FILTER_TYPE_BASE)
885 {
Glenn Randers-Pehrson7ec330d2009-09-25 11:45:42 -0500886 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500887 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
Glenn Randers-Pehrson7ec330d2009-09-25 11:45:42 -0500888 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500889 (color_type == PNG_COLOR_TYPE_RGB ||
890 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
Glenn Randers-Pehrson7ec330d2009-09-25 11:45:42 -0500891 {
892 png_warning(png_ptr, "Unknown filter method in IHDR");
893 error = 1;
894 }
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500895
Glenn Randers-Pehrson7ec330d2009-09-25 11:45:42 -0500896 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
897 {
898 png_warning(png_ptr, "Invalid filter method in IHDR");
899 error = 1;
900 }
901 }
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500902
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600903# else
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500904 if (filter_type != PNG_FILTER_TYPE_BASE)
905 {
906 png_warning(png_ptr, "Unknown filter method in IHDR");
907 error = 1;
908 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600909# endif
Glenn Randers-Pehrson134bbe42009-09-24 18:10:49 -0500910
911 if (error == 1)
912 png_error(png_ptr, "Invalid IHDR data");
913}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500914#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */