blob: b5f41617406edf5c88f8336276b5e8bd6c5158ea [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-Pehrsone26c0952009-09-23 11:22:08 -05004 * Last changed in libpng 1.4.0 [September 23, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Guy Schalnat0d580581995-07-20 02:43:20 -050014#define PNG_NO_EXTERN
15#include "png.h"
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050016#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050017
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060018/* Generate a compiler error if there is an old png.h in the search path. */
Glenn Randers-Pehrson93fc3da2009-09-17 11:07:01 -050019typedef version_1_4_0beta81 Your_png_h_is_not_version_1_4_0beta81;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Version information for C files. This had better match the version
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -060022 * string defined in png.h. */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060023
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060024/* Tells libpng that we have already handled the first "num_bytes" bytes
25 * of the PNG file signature. If the PNG data is embedded into another
26 * stream we can set num_bytes = 8 so that libpng will not attempt to read
27 * or write any of the magic bytes before it starts on the IHDR.
28 */
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -050029
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060030#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050031void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060032png_set_sig_bytes(png_structp png_ptr, int num_bytes)
33{
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -050034 png_debug(1, "in png_set_sig_bytes");
35
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050036 if (png_ptr == NULL)
37 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050038
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039 if (num_bytes > 8)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050040 png_error(png_ptr, "Too many bytes for PNG signature");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060041
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -050042 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060043}
44
45/* Checks whether the supplied bytes match the PNG signature. We allow
46 * checking less than the full 8-byte signature so that those apps that
47 * already read the first few bytes of a file to determine the file type
48 * can simply check the remaining bytes for extra assurance. Returns
49 * an integer less than, equal to, or greater than zero if sig is found,
50 * respectively, to be less than, to match, or be greater than the correct
51 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
52 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050053int PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050054png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060055{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060056 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060057 if (num_to_check > 8)
58 num_to_check = 8;
59 else if (num_to_check < 1)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060060 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060061
Andreas Dilger47a0c421997-05-16 02:46:07 -050062 if (start > 7)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060063 return (-1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064
65 if (start + num_to_check > 8)
66 num_to_check = 8 - start;
67
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050068 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060069}
70
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060071#endif /* PNG_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -050072
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060073#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050074/* Function to allocate memory for zlib and clear it to 0. */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -050075voidpf /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050076png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -050077{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050078 png_voidp ptr;
79 png_structp p=(png_structp)png_ptr;
80 png_uint_32 save_flags=p->flags;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050081 png_alloc_size_t num_bytes;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050082
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050083 if (png_ptr == NULL)
84 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050085 if (items > PNG_UINT_32_MAX/size)
86 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050087 png_warning (p, "Potential overflow in png_zalloc()");
88 return (NULL);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050089 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050090 num_bytes = (png_alloc_size_t)items * size;
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060091
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050092 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
93 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
94 p->flags=save_flags;
Guy Schalnat6d764711995-12-19 03:22:19 -060095
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050096 return ((voidpf)ptr);
97}
98
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050099/* Function to free memory for zlib */
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500100void /* PRIVATE */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500101png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500102{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103 png_free((png_structp)png_ptr, (png_voidp)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500104}
105
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600106/* Reset the CRC variable to 32 bits of 1's. Care must be taken
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600107 * in case CRC is > 32 bits to leave the top bits 0.
108 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500109void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600110png_reset_crc(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500111{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600112 png_ptr->crc = crc32(0, Z_NULL, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500113}
114
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115/* Calculate the CRC over a section of data. We can only pass as
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600116 * much data to this routine as the largest single buffer size. We
117 * also check that this data will actually be used before going to the
118 * trouble of calculating it.
119 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500120void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500122{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500123 int need_crc = 1;
124
125 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
126 {
127 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
128 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
129 need_crc = 0;
130 }
131 else /* critical */
132 {
133 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
134 need_crc = 0;
135 }
136
137 if (need_crc)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600138 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500139}
Guy Schalnate5a37791996-06-05 15:50:50 -0500140
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600141/* Allocate the memory for an info_struct for the application. We don't
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600142 * really need the png_ptr, but it could potentially be useful in the
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500143 * future. This should be used in favour of malloc(png_sizeof(png_info))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600144 * and png_info_init() so that applications that want to use a shared
145 * libpng don't have to be recompiled if png_info changes size.
146 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500147png_infop PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500148png_create_info_struct(png_structp png_ptr)
149{
150 png_infop info_ptr;
151
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500152 png_debug(1, "in png_create_info_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500153
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 if (png_ptr == NULL)
155 return (NULL);
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500156
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500157#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600158 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
159 png_ptr->malloc_fn, png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500160#else
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600161 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500162#endif
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600163 if (info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500164 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnate5a37791996-06-05 15:50:50 -0500165
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600166 return (info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500167}
168
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600169/* This function frees the memory associated with a single info struct.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600170 * Normally, one would use either png_destroy_read_struct() or
171 * png_destroy_write_struct() to free an info struct, but this may be
172 * useful for some applications.
173 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500174void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600175png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
176{
177 png_infop info_ptr = NULL;
178
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500179 png_debug(1, "in png_destroy_info_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500180
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500181 if (png_ptr == NULL)
182 return;
183
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600185 info_ptr = *info_ptr_ptr;
186
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600188 {
189 png_info_destroy(png_ptr, info_ptr);
190
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500191#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500192 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
193 png_ptr->mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500194#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600195 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500196#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500197 *info_ptr_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198 }
199}
200
201/* Initialize the info structure. This is now an internal function (0.89)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600202 * and applications using it are urged to use png_create_info_struct()
203 * instead.
204 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500205
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500206void PNGAPI
207png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
208{
209 png_infop info_ptr = *ptr_ptr;
210
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500211 png_debug(1, "in png_info_init_3");
212
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500213 if (info_ptr == NULL)
214 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600215
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500216 if (png_sizeof(png_info) > png_info_struct_size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500217 {
218 png_destroy_struct(info_ptr);
219 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
220 *ptr_ptr = info_ptr;
221 }
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500222
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500223 /* Set everything to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500224 png_memset(info_ptr, 0, png_sizeof(png_info));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225}
226
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500227void PNGAPI
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500228png_data_freer(png_structp png_ptr, png_infop info_ptr,
229 int freer, png_uint_32 mask)
230{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500231 png_debug(1, "in png_data_freer");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500232
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500233 if (png_ptr == NULL || info_ptr == NULL)
234 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500235
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500236 if (freer == PNG_DESTROY_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500237 info_ptr->free_me |= mask;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500238 else if (freer == PNG_USER_WILL_FREE_DATA)
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500239 info_ptr->free_me &= ~mask;
240 else
241 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500242 "Unknown freer parameter in png_data_freer");
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500243}
244
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500245void PNGAPI
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500246png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
247 int num)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600248{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500249 png_debug(1, "in png_free_data");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500250
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600251 if (png_ptr == NULL || info_ptr == NULL)
252 return;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600253
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500254#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500255 /* Free text item num or (if num == -1) all text items */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500256 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600257 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500258 if (num != -1)
259 {
260 if (info_ptr->text && info_ptr->text[num].key)
261 {
262 png_free(png_ptr, info_ptr->text[num].key);
263 info_ptr->text[num].key = NULL;
264 }
265 }
266 else
267 {
268 int i;
269 for (i = 0; i < info_ptr->num_text; i++)
270 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
271 png_free(png_ptr, info_ptr->text);
272 info_ptr->text = NULL;
273 info_ptr->num_text=0;
274 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600275 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600276#endif
277
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500278#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500279 /* Free any tRNS entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500280 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500281 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500282 png_free(png_ptr, info_ptr->trans_alpha);
283 info_ptr->trans_alpha = NULL;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500284 info_ptr->valid &= ~PNG_INFO_tRNS;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500285 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600286#endif
287
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500288#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500289 /* Free any sCAL entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500290 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500291 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600292#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500293 png_free(png_ptr, info_ptr->scal_s_width);
294 png_free(png_ptr, info_ptr->scal_s_height);
295 info_ptr->scal_s_width = NULL;
296 info_ptr->scal_s_height = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600297#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500298 info_ptr->valid &= ~PNG_INFO_sCAL;
299 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600300#endif
301
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500302#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500303 /* Free any pCAL entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500304 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500305 {
306 png_free(png_ptr, info_ptr->pcal_purpose);
307 png_free(png_ptr, info_ptr->pcal_units);
308 info_ptr->pcal_purpose = NULL;
309 info_ptr->pcal_units = NULL;
310 if (info_ptr->pcal_params != NULL)
311 {
312 int i;
313 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
314 {
315 png_free(png_ptr, info_ptr->pcal_params[i]);
316 info_ptr->pcal_params[i]=NULL;
317 }
318 png_free(png_ptr, info_ptr->pcal_params);
319 info_ptr->pcal_params = NULL;
320 }
321 info_ptr->valid &= ~PNG_INFO_pCAL;
322 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600323#endif
324
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500325#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500326 /* Free any iCCP entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500327 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500328 {
329 png_free(png_ptr, info_ptr->iccp_name);
330 png_free(png_ptr, info_ptr->iccp_profile);
331 info_ptr->iccp_name = NULL;
332 info_ptr->iccp_profile = NULL;
333 info_ptr->valid &= ~PNG_INFO_iCCP;
334 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600335#endif
336
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500337#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500338 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500339 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600340 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500341 if (num != -1)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500342 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500343 if (info_ptr->splt_palettes)
344 {
345 png_free(png_ptr, info_ptr->splt_palettes[num].name);
346 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
347 info_ptr->splt_palettes[num].name = NULL;
348 info_ptr->splt_palettes[num].entries = NULL;
349 }
350 }
351 else
352 {
353 if (info_ptr->splt_palettes_num)
354 {
355 int i;
356 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
357 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
358
359 png_free(png_ptr, info_ptr->splt_palettes);
360 info_ptr->splt_palettes = NULL;
361 info_ptr->splt_palettes_num = 0;
362 }
363 info_ptr->valid &= ~PNG_INFO_sPLT;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500364 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366#endif
367
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500368#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500369 if (png_ptr->unknown_chunk.data)
370 {
371 png_free(png_ptr, png_ptr->unknown_chunk.data);
372 png_ptr->unknown_chunk.data = NULL;
373 }
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500374
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500375 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600376 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500377 if (num != -1)
378 {
379 if (info_ptr->unknown_chunks)
380 {
381 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
382 info_ptr->unknown_chunks[num].data = NULL;
383 }
384 }
385 else
386 {
387 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389 if (info_ptr->unknown_chunks_num)
390 {
391 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
392 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600393
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500394 png_free(png_ptr, info_ptr->unknown_chunks);
395 info_ptr->unknown_chunks = NULL;
396 info_ptr->unknown_chunks_num = 0;
397 }
398 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600400#endif
401
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500402#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500403 /* Free any hIST entry */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500404 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500405 {
406 png_free(png_ptr, info_ptr->hist);
407 info_ptr->hist = NULL;
408 info_ptr->valid &= ~PNG_INFO_hIST;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500409 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600410#endif
411
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500412 /* Free any PLTE entry that was internally allocated */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500413 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500414 {
415 png_zfree(png_ptr, info_ptr->palette);
416 info_ptr->palette = NULL;
417 info_ptr->valid &= ~PNG_INFO_PLTE;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500418 info_ptr->num_palette = 0;
419 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600420
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500421#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500422 /* Free any image bits attached to the info structure */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500423 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500424 {
425 if (info_ptr->row_pointers)
426 {
427 int row;
428 for (row = 0; row < (int)info_ptr->height; row++)
429 {
430 png_free(png_ptr, info_ptr->row_pointers[row]);
431 }
432 png_free(png_ptr, info_ptr->row_pointers);
433 }
434 info_ptr->valid &= ~PNG_INFO_IDAT;
435 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436#endif
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -0500437
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500438 if (num == -1)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500439 info_ptr->free_me &= ~mask;
Glenn Randers-Pehrsonec61c232000-05-16 06:17:36 -0500440 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500441 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600442}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600443
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600444/* This is an internal routine to free any memory that the info struct is
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 * pointing to before re-using it or freeing the struct itself. Recall
446 * that png_free() checks for NULL pointers for us.
447 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500448void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600449png_info_destroy(png_structp png_ptr, png_infop info_ptr)
450{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500451 png_debug(1, "in png_info_destroy");
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600452
453 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600454
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500455#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600456 if (png_ptr->num_chunk_list)
457 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500458 png_free(png_ptr, png_ptr->chunk_list);
459 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600460 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600461#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600462
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500463 png_info_init_3(&info_ptr, png_sizeof(png_info));
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500464}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600465#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500466
Guy Schalnate5a37791996-06-05 15:50:50 -0500467/* This function returns a pointer to the io_ptr associated with the user
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600468 * functions. The application should free any memory associated with this
469 * pointer before png_write_destroy() or png_read_destroy() are called.
470 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500471png_voidp PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500472png_get_io_ptr(png_structp png_ptr)
473{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500474 if (png_ptr == NULL)
475 return (NULL);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600476 return (png_ptr->io_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500477}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500478
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600479#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500480#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481/* Initialize the default input/output functions for the PNG file. If you
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600482 * use your own read or write routines, you can call either png_set_read_fn()
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500483 * or png_set_write_fn() instead of png_init_io(). If you have defined
484 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
485 * necessarily available.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600486 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500487void PNGAPI
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500488png_init_io(png_structp png_ptr, png_FILE_p fp)
Guy Schalnate5a37791996-06-05 15:50:50 -0500489{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500490 png_debug(1, "in png_init_io");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500491
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500492 if (png_ptr == NULL)
493 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500494
Guy Schalnate5a37791996-06-05 15:50:50 -0500495 png_ptr->io_ptr = (png_voidp)fp;
496}
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500498
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500499#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500500/* Convert the supplied time into an RFC 1123 string suitable for use in
501 * a "Creation Time" or other text-based time string.
502 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500503png_charp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500504png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
505{
506 static PNG_CONST char short_months[12][4] =
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600507 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
508 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500509
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 if (png_ptr == NULL)
511 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500512 if (png_ptr->time_buffer == NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500513 {
514 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
515 png_sizeof(char)));
516 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500517
518#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500519 {
520 char near_time_buf[29];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500521 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500522 ptime->day % 32, short_months[(ptime->month - 1) % 12],
523 ptime->year, ptime->hour % 24, ptime->minute % 60,
524 ptime->second % 61);
525 png_memcpy(png_ptr->time_buffer, near_time_buf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500526 29*png_sizeof(char));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500527 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500528#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500529 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
Glenn Randers-Pehrsone1eff582001-04-14 20:15:41 -0500530 ptime->day % 32, short_months[(ptime->month - 1) % 12],
531 ptime->year, ptime->hour % 24, ptime->minute % 60,
532 ptime->second % 61);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500533#endif
534 return ((png_charp)png_ptr->time_buffer);
535}
536#endif /* PNG_TIME_RFC1123_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600537
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600538#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600539
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500540png_charp PNGAPI
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600541png_get_copyright(png_structp png_ptr)
542{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500543 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500544#ifdef PNG_STRING_COPYRIGHT
545 return PNG_STRING_COPYRIGHT
546#else
547#ifdef __STDC__
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500548 return ((png_charp) PNG_STRING_NEWLINE \
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500549 "libpng version x 1.4.0beta81 - September 23, 2009" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600550 "Copyright (c) 1998-2009 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500551 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
552 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
553 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500554#else
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500555 return ((png_charp) "libpng version 1.4.0beta81 - September 23, 2009\
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600556 Copyright (c) 1998-2009 Glenn Randers-Pehrson\
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500557 Copyright (c) 1996-1997 Andreas Dilger\
558 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
559#endif
560#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600561}
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500562
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600563/* The following return the library version as a short string in the
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500564 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
565 * used with your application, print out PNG_LIBPNG_VER_STRING, which
566 * is defined in png.h.
567 * Note: now there is no difference between png_get_libpng_ver() and
568 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
569 * it is guaranteed that png.c uses the correct version of png.h.
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600570 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600572png_get_libpng_ver(png_structp png_ptr)
573{
574 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500575 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500576 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600577}
578
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600580png_get_header_ver(png_structp png_ptr)
581{
582 /* Version of *.h files used when building libpng */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500583 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500584 return ((png_charp) PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600585}
586
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500587png_charp PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600588png_get_header_version(png_structp png_ptr)
589{
590 /* Returns longer string containing both version and date */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500591 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500592#ifdef __STDC__
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500593 return ((png_charp) PNG_HEADER_VERSION_STRING
594#ifndef PNG_READ_SUPPORTED
595 " (NO READ SUPPORT)"
596#endif
Glenn Randers-Pehrson43aaf6e2008-08-05 22:17:03 -0500597 PNG_STRING_NEWLINE);
Glenn Randers-Pehrsone0784c72008-08-09 07:11:44 -0500598#else
599 return ((png_charp) PNG_HEADER_VERSION_STRING);
600#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600601}
602
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600603#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600604#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonc1bfe682002-03-06 22:08:00 -0600605int PNGAPI
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600606png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
607{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500608 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600609 int i;
610 png_bytep p;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500611 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600612 return 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500613 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
614 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600615 if (!png_memcmp(chunk_name, p, 4))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500616 return ((int)*(p + 4));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600617 return 0;
618}
619#endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500620
621/* This function, added to libpng-1.0.6g, is untested. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500622int PNGAPI
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500623png_reset_zstream(png_structp png_ptr)
624{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500625 if (png_ptr == NULL)
626 return Z_STREAM_ERROR;
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -0500627 return (inflateReset(&png_ptr->zstream));
628}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600629#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500630
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600631/* This function was added to libpng-1.0.7 */
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500632png_uint_32 PNGAPI
633png_access_version_number(void)
634{
635 /* Version of *.c files used when building libpng */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500636 return((png_uint_32) PNG_LIBPNG_VER);
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500637}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600638
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500639
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500640
641#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
642#ifdef PNG_SIZE_T
643/* Added at libpng version 1.2.6 */
644 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
645png_size_t PNGAPI
646png_convert_size(size_t size)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500647{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500648 if (size > (png_size_t)-1)
649 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
650 return ((png_size_t)size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500651}
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500652#endif /* PNG_SIZE_T */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600653
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600654/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500655#ifdef PNG_cHRM_SUPPORTED
656#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600657
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600658/*
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500659 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
660 * arithmetic, to produce a 64 bit result in the HI/LO words.
661 *
662 * A B
663 * x C D
664 * ------
665 * AD || BD
666 * AC || CB || 0
667 *
668 * where A and B are the high and low 16-bit words of V1,
669 * C and D are the 16-bit words of V2, AD is the product of
670 * A and D, and X || Y is (X << 16) + Y.
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600671*/
672
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500673void /* PRIVATE */
674png_64bit_product (long v1, long v2, unsigned long *hi_product,
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600675 unsigned long *lo_product)
676{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500677 int a, b, c, d;
678 long lo, hi, x, y;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600679
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500680 a = (v1 >> 16) & 0xffff;
681 b = v1 & 0xffff;
682 c = (v2 >> 16) & 0xffff;
683 d = v2 & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600684
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500685 lo = b * d; /* BD */
686 x = a * d + c * b; /* AD + CB */
687 y = ((lo >> 16) & 0xffff) + x;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600688
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500689 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
690 hi = (y >> 16) & 0xffff;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600691
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500692 hi += a * c; /* AC */
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600693
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500694 *hi_product = (unsigned long)hi;
695 *lo_product = (unsigned long)lo;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600696}
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500697
Glenn Randers-Pehrson3f705ba2009-07-23 12:53:06 -0500698int /* PRIVATE */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600699png_check_cHRM_fixed(png_structp png_ptr,
700 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
701 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
702 png_fixed_point blue_x, png_fixed_point blue_y)
703{
704 int ret = 1;
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600705 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600706
707 png_debug(1, "in function png_check_cHRM_fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500708
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600709 if (png_ptr == NULL)
710 return 0;
711
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600712 if (white_x < 0 || white_y <= 0 ||
713 red_x < 0 || red_y < 0 ||
714 green_x < 0 || green_y < 0 ||
715 blue_x < 0 || blue_y < 0)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600716 {
717 png_warning(png_ptr,
718 "Ignoring attempt to set negative chromaticity value");
719 ret = 0;
720 }
721 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
722 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
723 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
724 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
725 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
726 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
727 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
728 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
729 {
730 png_warning(png_ptr,
731 "Ignoring attempt to set chromaticity value exceeding 21474.83");
732 ret = 0;
733 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600734 if (white_x > 100000L - white_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600735 {
736 png_warning(png_ptr, "Invalid cHRM white point");
737 ret = 0;
738 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600739 if (red_x > 100000L - red_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600740 {
741 png_warning(png_ptr, "Invalid cHRM red point");
742 ret = 0;
743 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600744 if (green_x > 100000L - green_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600745 {
746 png_warning(png_ptr, "Invalid cHRM green point");
747 ret = 0;
748 }
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -0600749 if (blue_x > 100000L - blue_y)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600750 {
751 png_warning(png_ptr, "Invalid cHRM blue point");
752 ret = 0;
753 }
Glenn Randers-Pehrsond0c40592008-11-22 07:09:51 -0600754
755 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
756 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
757
758 if (xy_hi == yx_hi && xy_lo == yx_lo)
759 {
760 png_warning(png_ptr,
761 "Ignoring attempt to set cHRM RGB triangle with zero area");
762 ret = 0;
763 }
764
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600765 return ret;
766}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500767#endif /* PNG_CHECK_cHRM_SUPPORTED */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600768#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */