blob: 86514fc3c50eea1a48cba851a336c0315e33e692 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* png.c - location for general purpose libpng functions
3 *
Eric Vannier66dce0d2011-07-20 17:03:29 -07004 * Last changed in libpng 1.2.46 [February 25, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04008 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
The Android Open Source Project893912b2009-03-03 19:30:05 -080012 */
13
14#define PNG_INTERNAL
15#define PNG_NO_EXTERN
Patrick Scott5f6bd842010-06-28 16:55:16 -040016#define PNG_NO_PEDANTIC_WARNINGS
The Android Open Source Project893912b2009-03-03 19:30:05 -080017#include "png.h"
Geremy Condra14cab862012-06-05 15:17:48 -070018#include <stdint.h>
The Android Open Source Project893912b2009-03-03 19:30:05 -080019
20/* Generate a compiler error if there is an old png.h in the search path. */
Eric Vannier66dce0d2011-07-20 17:03:29 -070021typedef version_1_2_46 Your_png_h_is_not_version_1_2_46;
The Android Open Source Project893912b2009-03-03 19:30:05 -080022
23/* Version information for C files. This had better match the version
Patrick Scott5f6bd842010-06-28 16:55:16 -040024 * string defined in png.h.
25 */
The Android Open Source Project893912b2009-03-03 19:30:05 -080026
27#ifdef PNG_USE_GLOBAL_ARRAYS
28/* png_libpng_ver was changed to a function in version 1.0.5c */
29PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
30
31#ifdef PNG_READ_SUPPORTED
32
33/* png_sig was changed to a function in version 1.0.5c */
34/* Place to hold the signature string for a PNG file. */
35PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
36#endif /* PNG_READ_SUPPORTED */
37
38/* Invoke global declarations for constant strings for known chunk types */
39PNG_IHDR;
40PNG_IDAT;
41PNG_IEND;
42PNG_PLTE;
43PNG_bKGD;
44PNG_cHRM;
45PNG_gAMA;
46PNG_hIST;
47PNG_iCCP;
48PNG_iTXt;
49PNG_oFFs;
50PNG_pCAL;
51PNG_sCAL;
52PNG_pHYs;
53PNG_sBIT;
54PNG_sPLT;
55PNG_sRGB;
56PNG_tEXt;
57PNG_tIME;
58PNG_tRNS;
59PNG_zTXt;
60
61#ifdef PNG_READ_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -040062/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -080063
Patrick Scotta0bb96c2009-07-22 11:50:02 -040064/* Start of interlace block */
The Android Open Source Project893912b2009-03-03 19:30:05 -080065PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
66
Patrick Scotta0bb96c2009-07-22 11:50:02 -040067/* Offset to next interlace block */
The Android Open Source Project893912b2009-03-03 19:30:05 -080068PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
69
Patrick Scotta0bb96c2009-07-22 11:50:02 -040070/* Start of interlace block in the y direction */
The Android Open Source Project893912b2009-03-03 19:30:05 -080071PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
72
Patrick Scotta0bb96c2009-07-22 11:50:02 -040073/* Offset to next interlace block in the y direction */
The Android Open Source Project893912b2009-03-03 19:30:05 -080074PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
75
76/* Height of interlace block. This is not currently used - if you need
77 * it, uncomment it here and in png.h
78PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
79*/
80
81/* Mask to determine which pixels are valid in a pass */
Patrick Scott5f6bd842010-06-28 16:55:16 -040082PNG_CONST int FARDATA png_pass_mask[] =
83 {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
The Android Open Source Project893912b2009-03-03 19:30:05 -080084
85/* Mask to determine which pixels to overwrite while displaying */
86PNG_CONST int FARDATA png_pass_dsp_mask[]
87 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
88
89#endif /* PNG_READ_SUPPORTED */
90#endif /* PNG_USE_GLOBAL_ARRAYS */
91
92/* Tells libpng that we have already handled the first "num_bytes" bytes
93 * of the PNG file signature. If the PNG data is embedded into another
94 * stream we can set num_bytes = 8 so that libpng will not attempt to read
95 * or write any of the magic bytes before it starts on the IHDR.
96 */
97
98#ifdef PNG_READ_SUPPORTED
99void PNGAPI
100png_set_sig_bytes(png_structp png_ptr, int num_bytes)
101{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400102 png_debug(1, "in png_set_sig_bytes");
103
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400104 if (png_ptr == NULL)
105 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400106
The Android Open Source Project893912b2009-03-03 19:30:05 -0800107 if (num_bytes > 8)
108 png_error(png_ptr, "Too many bytes for PNG signature.");
109
110 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
111}
112
113/* Checks whether the supplied bytes match the PNG signature. We allow
114 * checking less than the full 8-byte signature so that those apps that
115 * already read the first few bytes of a file to determine the file type
116 * can simply check the remaining bytes for extra assurance. Returns
117 * an integer less than, equal to, or greater than zero if sig is found,
118 * respectively, to be less than, to match, or be greater than the correct
119 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
120 */
121int PNGAPI
122png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
123{
124 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
125 if (num_to_check > 8)
126 num_to_check = 8;
127 else if (num_to_check < 1)
128 return (-1);
129
130 if (start > 7)
131 return (-1);
132
133 if (start + num_to_check > 8)
134 num_to_check = 8 - start;
135
136 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
137}
138
139#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
140/* (Obsolete) function to check signature bytes. It does not allow one
141 * to check a partial signature. This function might be removed in the
142 * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
143 */
144int PNGAPI
145png_check_sig(png_bytep sig, int num)
146{
147 return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
148}
149#endif
150#endif /* PNG_READ_SUPPORTED */
151
152#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
153/* Function to allocate memory for zlib and clear it to 0. */
154#ifdef PNG_1_0_X
155voidpf PNGAPI
156#else
Patrick Scott5f6bd842010-06-28 16:55:16 -0400157voidpf /* PRIVATE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800158#endif
159png_zalloc(voidpf png_ptr, uInt items, uInt size)
160{
161 png_voidp ptr;
162 png_structp p=(png_structp)png_ptr;
163 png_uint_32 save_flags=p->flags;
164 png_uint_32 num_bytes;
165
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400166 if (png_ptr == NULL)
167 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800168 if (items > PNG_UINT_32_MAX/size)
169 {
170 png_warning (p, "Potential overflow in png_zalloc()");
171 return (NULL);
172 }
173 num_bytes = (png_uint_32)items * size;
174
175 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
176 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
177 p->flags=save_flags;
178
179#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
180 if (ptr == NULL)
181 return ((voidpf)ptr);
182
183 if (num_bytes > (png_uint_32)0x8000L)
184 {
185 png_memset(ptr, 0, (png_size_t)0x8000L);
186 png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
187 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
188 }
189 else
190 {
191 png_memset(ptr, 0, (png_size_t)num_bytes);
192 }
193#endif
194 return ((voidpf)ptr);
195}
196
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400197/* Function to free memory for zlib */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800198#ifdef PNG_1_0_X
199void PNGAPI
200#else
Patrick Scott5f6bd842010-06-28 16:55:16 -0400201void /* PRIVATE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800202#endif
203png_zfree(voidpf png_ptr, voidpf ptr)
204{
205 png_free((png_structp)png_ptr, (png_voidp)ptr);
206}
207
208/* Reset the CRC variable to 32 bits of 1's. Care must be taken
209 * in case CRC is > 32 bits to leave the top bits 0.
210 */
211void /* PRIVATE */
212png_reset_crc(png_structp png_ptr)
213{
214 png_ptr->crc = crc32(0, Z_NULL, 0);
215}
216
217/* Calculate the CRC over a section of data. We can only pass as
218 * much data to this routine as the largest single buffer size. We
219 * also check that this data will actually be used before going to the
220 * trouble of calculating it.
221 */
222void /* PRIVATE */
223png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
224{
225 int need_crc = 1;
226
227 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
228 {
229 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
230 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
231 need_crc = 0;
232 }
233 else /* critical */
234 {
235 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
236 need_crc = 0;
237 }
238
239 if (need_crc)
240 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
241}
242
243/* Allocate the memory for an info_struct for the application. We don't
244 * really need the png_ptr, but it could potentially be useful in the
245 * future. This should be used in favour of malloc(png_sizeof(png_info))
246 * and png_info_init() so that applications that want to use a shared
247 * libpng don't have to be recompiled if png_info changes size.
248 */
249png_infop PNGAPI
250png_create_info_struct(png_structp png_ptr)
251{
252 png_infop info_ptr;
253
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700254 png_debug(1, "in png_create_info_struct");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400255
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400256 if (png_ptr == NULL)
257 return (NULL);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400258
The Android Open Source Project893912b2009-03-03 19:30:05 -0800259#ifdef PNG_USER_MEM_SUPPORTED
260 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
261 png_ptr->malloc_fn, png_ptr->mem_ptr);
262#else
263 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
264#endif
265 if (info_ptr != NULL)
266 png_info_init_3(&info_ptr, png_sizeof(png_info));
267
268 return (info_ptr);
269}
270
271/* This function frees the memory associated with a single info struct.
272 * Normally, one would use either png_destroy_read_struct() or
273 * png_destroy_write_struct() to free an info struct, but this may be
274 * useful for some applications.
275 */
276void PNGAPI
277png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
278{
279 png_infop info_ptr = NULL;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400280
281 png_debug(1, "in png_destroy_info_struct");
282
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400283 if (png_ptr == NULL)
284 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800285
The Android Open Source Project893912b2009-03-03 19:30:05 -0800286 if (info_ptr_ptr != NULL)
287 info_ptr = *info_ptr_ptr;
288
289 if (info_ptr != NULL)
290 {
291 png_info_destroy(png_ptr, info_ptr);
292
293#ifdef PNG_USER_MEM_SUPPORTED
294 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
295 png_ptr->mem_ptr);
296#else
297 png_destroy_struct((png_voidp)info_ptr);
298#endif
299 *info_ptr_ptr = NULL;
300 }
301}
302
303/* Initialize the info structure. This is now an internal function (0.89)
304 * and applications using it are urged to use png_create_info_struct()
305 * instead.
306 */
307#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
308#undef png_info_init
309void PNGAPI
310png_info_init(png_infop info_ptr)
311{
312 /* We only come here via pre-1.0.12-compiled applications */
313 png_info_init_3(&info_ptr, 0);
314}
315#endif
316
317void PNGAPI
318png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
319{
320 png_infop info_ptr = *ptr_ptr;
321
Patrick Scott5f6bd842010-06-28 16:55:16 -0400322 png_debug(1, "in png_info_init_3");
323
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400324 if (info_ptr == NULL)
325 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800326
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700327 if (png_sizeof(png_info) > png_info_struct_size)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400328 {
329 png_destroy_struct(info_ptr);
330 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
331 *ptr_ptr = info_ptr;
332 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800333
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400334 /* Set everything to 0 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700335 png_memset(info_ptr, 0, png_sizeof(png_info));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800336}
337
338#ifdef PNG_FREE_ME_SUPPORTED
339void PNGAPI
340png_data_freer(png_structp png_ptr, png_infop info_ptr,
341 int freer, png_uint_32 mask)
342{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700343 png_debug(1, "in png_data_freer");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400344
The Android Open Source Project893912b2009-03-03 19:30:05 -0800345 if (png_ptr == NULL || info_ptr == NULL)
346 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400347
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700348 if (freer == PNG_DESTROY_WILL_FREE_DATA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800349 info_ptr->free_me |= mask;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700350 else if (freer == PNG_USER_WILL_FREE_DATA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800351 info_ptr->free_me &= ~mask;
352 else
353 png_warning(png_ptr,
354 "Unknown freer parameter in png_data_freer.");
355}
356#endif
357
358void PNGAPI
359png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
360 int num)
361{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700362 png_debug(1, "in png_free_data");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400363
The Android Open Source Project893912b2009-03-03 19:30:05 -0800364 if (png_ptr == NULL || info_ptr == NULL)
365 return;
366
Patrick Scott5f6bd842010-06-28 16:55:16 -0400367#ifdef PNG_TEXT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400368 /* Free text item num or (if num == -1) all text items */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800369#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400370 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800371#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400372 if (mask & PNG_FREE_TEXT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800373#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800374 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400375 if (num != -1)
376 {
377 if (info_ptr->text && info_ptr->text[num].key)
378 {
379 png_free(png_ptr, info_ptr->text[num].key);
380 info_ptr->text[num].key = NULL;
381 }
382 }
383 else
384 {
385 int i;
386 for (i = 0; i < info_ptr->num_text; i++)
387 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
388 png_free(png_ptr, info_ptr->text);
389 info_ptr->text = NULL;
390 info_ptr->num_text=0;
391 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800392 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800393#endif
394
Patrick Scott5f6bd842010-06-28 16:55:16 -0400395#ifdef PNG_tRNS_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400396 /* Free any tRNS entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800397#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400398 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800399#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400400 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800401#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400402 {
403 png_free(png_ptr, info_ptr->trans);
404 info_ptr->trans = NULL;
405 info_ptr->valid &= ~PNG_INFO_tRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800406#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400407 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800408#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400409 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800410#endif
411
Patrick Scott5f6bd842010-06-28 16:55:16 -0400412#ifdef PNG_sCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400413 /* Free any sCAL entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800414#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400415 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800416#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400417 if (mask & PNG_FREE_SCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800418#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400419 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800420#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400421 png_free(png_ptr, info_ptr->scal_s_width);
422 png_free(png_ptr, info_ptr->scal_s_height);
423 info_ptr->scal_s_width = NULL;
424 info_ptr->scal_s_height = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800425#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400426 info_ptr->valid &= ~PNG_INFO_sCAL;
427 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800428#endif
429
Patrick Scott5f6bd842010-06-28 16:55:16 -0400430#ifdef PNG_pCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400431 /* Free any pCAL entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800432#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400433 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800434#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400435 if (mask & PNG_FREE_PCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800436#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400437 {
438 png_free(png_ptr, info_ptr->pcal_purpose);
439 png_free(png_ptr, info_ptr->pcal_units);
440 info_ptr->pcal_purpose = NULL;
441 info_ptr->pcal_units = NULL;
442 if (info_ptr->pcal_params != NULL)
443 {
444 int i;
445 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
446 {
447 png_free(png_ptr, info_ptr->pcal_params[i]);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400448 info_ptr->pcal_params[i] = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400449 }
450 png_free(png_ptr, info_ptr->pcal_params);
451 info_ptr->pcal_params = NULL;
452 }
453 info_ptr->valid &= ~PNG_INFO_pCAL;
454 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800455#endif
456
Patrick Scott5f6bd842010-06-28 16:55:16 -0400457#ifdef PNG_iCCP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400458 /* Free any iCCP entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800459#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400460 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800461#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400462 if (mask & PNG_FREE_ICCP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800463#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400464 {
465 png_free(png_ptr, info_ptr->iccp_name);
466 png_free(png_ptr, info_ptr->iccp_profile);
467 info_ptr->iccp_name = NULL;
468 info_ptr->iccp_profile = NULL;
469 info_ptr->valid &= ~PNG_INFO_iCCP;
470 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800471#endif
472
Patrick Scott5f6bd842010-06-28 16:55:16 -0400473#ifdef PNG_sPLT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400474 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800475#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400476 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800477#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400478 if (mask & PNG_FREE_SPLT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800479#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800480 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400481 if (num != -1)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800482 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400483 if (info_ptr->splt_palettes)
484 {
485 png_free(png_ptr, info_ptr->splt_palettes[num].name);
486 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
487 info_ptr->splt_palettes[num].name = NULL;
488 info_ptr->splt_palettes[num].entries = NULL;
489 }
490 }
491 else
492 {
493 if (info_ptr->splt_palettes_num)
494 {
495 int i;
496 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
497 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
498
499 png_free(png_ptr, info_ptr->splt_palettes);
500 info_ptr->splt_palettes = NULL;
501 info_ptr->splt_palettes_num = 0;
502 }
503 info_ptr->valid &= ~PNG_INFO_sPLT;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800504 }
505 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800506#endif
507
Patrick Scott5f6bd842010-06-28 16:55:16 -0400508#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400509 if (png_ptr->unknown_chunk.data)
510 {
511 png_free(png_ptr, png_ptr->unknown_chunk.data);
512 png_ptr->unknown_chunk.data = NULL;
513 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700514
The Android Open Source Project893912b2009-03-03 19:30:05 -0800515#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400516 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800517#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400518 if (mask & PNG_FREE_UNKN)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800519#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800520 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400521 if (num != -1)
522 {
523 if (info_ptr->unknown_chunks)
524 {
525 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
526 info_ptr->unknown_chunks[num].data = NULL;
527 }
528 }
529 else
530 {
531 int i;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800532
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400533 if (info_ptr->unknown_chunks_num)
534 {
535 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
536 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800537
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400538 png_free(png_ptr, info_ptr->unknown_chunks);
539 info_ptr->unknown_chunks = NULL;
540 info_ptr->unknown_chunks_num = 0;
541 }
542 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800543 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800544#endif
545
Patrick Scott5f6bd842010-06-28 16:55:16 -0400546#ifdef PNG_hIST_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400547 /* Free any hIST entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800548#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400549 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800550#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400551 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800552#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400553 {
554 png_free(png_ptr, info_ptr->hist);
555 info_ptr->hist = NULL;
556 info_ptr->valid &= ~PNG_INFO_hIST;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800557#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400558 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800559#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400560 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800561#endif
562
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400563 /* Free any PLTE entry that was internally allocated */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800564#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400565 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800566#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400567 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800568#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400569 {
570 png_zfree(png_ptr, info_ptr->palette);
571 info_ptr->palette = NULL;
572 info_ptr->valid &= ~PNG_INFO_PLTE;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800573#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400574 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800575#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400576 info_ptr->num_palette = 0;
577 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800578
Patrick Scott5f6bd842010-06-28 16:55:16 -0400579#ifdef PNG_INFO_IMAGE_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400580 /* Free any image bits attached to the info structure */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800581#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400582 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800583#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400584 if (mask & PNG_FREE_ROWS)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800585#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400586 {
587 if (info_ptr->row_pointers)
588 {
589 int row;
590 for (row = 0; row < (int)info_ptr->height; row++)
591 {
592 png_free(png_ptr, info_ptr->row_pointers[row]);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400593 info_ptr->row_pointers[row] = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400594 }
595 png_free(png_ptr, info_ptr->row_pointers);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400596 info_ptr->row_pointers = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400597 }
598 info_ptr->valid &= ~PNG_INFO_IDAT;
599 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800600#endif
601
602#ifdef PNG_FREE_ME_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700603 if (num == -1)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400604 info_ptr->free_me &= ~mask;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800605 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400606 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800607#endif
608}
609
610/* This is an internal routine to free any memory that the info struct is
611 * pointing to before re-using it or freeing the struct itself. Recall
612 * that png_free() checks for NULL pointers for us.
613 */
614void /* PRIVATE */
615png_info_destroy(png_structp png_ptr, png_infop info_ptr)
616{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700617 png_debug(1, "in png_info_destroy");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800618
619 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
620
Patrick Scott5f6bd842010-06-28 16:55:16 -0400621#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800622 if (png_ptr->num_chunk_list)
623 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400624 png_free(png_ptr, png_ptr->chunk_list);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400625 png_ptr->chunk_list = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400626 png_ptr->num_chunk_list = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800627 }
628#endif
629
630 png_info_init_3(&info_ptr, png_sizeof(png_info));
631}
632#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
633
634/* This function returns a pointer to the io_ptr associated with the user
635 * functions. The application should free any memory associated with this
636 * pointer before png_write_destroy() or png_read_destroy() are called.
637 */
638png_voidp PNGAPI
639png_get_io_ptr(png_structp png_ptr)
640{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400641 if (png_ptr == NULL)
642 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800643 return (png_ptr->io_ptr);
644}
645
646#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400647#ifdef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800648/* Initialize the default input/output functions for the PNG file. If you
649 * use your own read or write routines, you can call either png_set_read_fn()
650 * or png_set_write_fn() instead of png_init_io(). If you have defined
651 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
652 * necessarily available.
653 */
654void PNGAPI
655png_init_io(png_structp png_ptr, png_FILE_p fp)
656{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700657 png_debug(1, "in png_init_io");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400658
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400659 if (png_ptr == NULL)
660 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400661
The Android Open Source Project893912b2009-03-03 19:30:05 -0800662 png_ptr->io_ptr = (png_voidp)fp;
663}
664#endif
665
Patrick Scott5f6bd842010-06-28 16:55:16 -0400666#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800667/* Convert the supplied time into an RFC 1123 string suitable for use in
668 * a "Creation Time" or other text-based time string.
669 */
670png_charp PNGAPI
671png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
672{
673 static PNG_CONST char short_months[12][4] =
674 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
675 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
676
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400677 if (png_ptr == NULL)
678 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800679 if (png_ptr->time_buffer == NULL)
680 {
681 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
682 png_sizeof(char)));
683 }
684
Patrick Scott5f6bd842010-06-28 16:55:16 -0400685#ifdef _WIN32_WCE
The Android Open Source Project893912b2009-03-03 19:30:05 -0800686 {
687 wchar_t time_buf[29];
688 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
689 ptime->day % 32, short_months[(ptime->month - 1) % 12],
690 ptime->year, ptime->hour % 24, ptime->minute % 60,
691 ptime->second % 61);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400692 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer,
693 29, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800694 }
695#else
696#ifdef USE_FAR_KEYWORD
697 {
698 char near_time_buf[29];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700699 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800700 ptime->day % 32, short_months[(ptime->month - 1) % 12],
701 ptime->year, ptime->hour % 24, ptime->minute % 60,
702 ptime->second % 61);
703 png_memcpy(png_ptr->time_buffer, near_time_buf,
704 29*png_sizeof(char));
705 }
706#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700707 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800708 ptime->day % 32, short_months[(ptime->month - 1) % 12],
709 ptime->year, ptime->hour % 24, ptime->minute % 60,
710 ptime->second % 61);
711#endif
712#endif /* _WIN32_WCE */
713 return ((png_charp)png_ptr->time_buffer);
714}
715#endif /* PNG_TIME_RFC1123_SUPPORTED */
716
717#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
718
719png_charp PNGAPI
720png_get_copyright(png_structp png_ptr)
721{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400722 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400723#ifdef PNG_STRING_COPYRIGHT
724 return PNG_STRING_COPYRIGHT
725#else
726#ifdef __STDC__
727 return ((png_charp) PNG_STRING_NEWLINE \
Eric Vannier66dce0d2011-07-20 17:03:29 -0700728 "libpng version 1.2.46 - July 9, 2011" PNG_STRING_NEWLINE \
729 "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
Patrick Scott5f6bd842010-06-28 16:55:16 -0400730 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
731 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
732 PNG_STRING_NEWLINE);
733#else
Eric Vannier66dce0d2011-07-20 17:03:29 -0700734 return ((png_charp) "libpng version 1.2.46 - July 9, 2011\
735 Copyright (c) 1998-2011 Glenn Randers-Pehrson\
Patrick Scott5f6bd842010-06-28 16:55:16 -0400736 Copyright (c) 1996-1997 Andreas Dilger\
737 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
738#endif
739#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800740}
741
742/* The following return the library version as a short string in the
743 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
744 * used with your application, print out PNG_LIBPNG_VER_STRING, which
745 * is defined in png.h.
746 * Note: now there is no difference between png_get_libpng_ver() and
747 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
748 * it is guaranteed that png.c uses the correct version of png.h.
749 */
750png_charp PNGAPI
751png_get_libpng_ver(png_structp png_ptr)
752{
753 /* Version of *.c files used when building libpng */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400754 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800755 return ((png_charp) PNG_LIBPNG_VER_STRING);
756}
757
758png_charp PNGAPI
759png_get_header_ver(png_structp png_ptr)
760{
761 /* Version of *.h files used when building libpng */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400762 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800763 return ((png_charp) PNG_LIBPNG_VER_STRING);
764}
765
766png_charp PNGAPI
767png_get_header_version(png_structp png_ptr)
768{
769 /* Returns longer string containing both version and date */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400770 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400771#ifdef __STDC__
The Android Open Source Project893912b2009-03-03 19:30:05 -0800772 return ((png_charp) PNG_HEADER_VERSION_STRING
773#ifndef PNG_READ_SUPPORTED
774 " (NO READ SUPPORT)"
775#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400776 PNG_STRING_NEWLINE);
777#else
778 return ((png_charp) PNG_HEADER_VERSION_STRING);
779#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800780}
781
782#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
783#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
784int PNGAPI
785png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
786{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400787 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800788 int i;
789 png_bytep p;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700790 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800791 return 0;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700792 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
793 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800794 if (!png_memcmp(chunk_name, p, 4))
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700795 return ((int)*(p + 4));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800796 return 0;
797}
798#endif
799
800/* This function, added to libpng-1.0.6g, is untested. */
801int PNGAPI
802png_reset_zstream(png_structp png_ptr)
803{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400804 if (png_ptr == NULL)
805 return Z_STREAM_ERROR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800806 return (inflateReset(&png_ptr->zstream));
807}
808#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
809
810/* This function was added to libpng-1.0.7 */
811png_uint_32 PNGAPI
812png_access_version_number(void)
813{
814 /* Version of *.c files used when building libpng */
815 return((png_uint_32) PNG_LIBPNG_VER);
816}
817
818
819#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400820#ifndef PNG_1_0_X
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400821/* This function was added to libpng 1.2.0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800822int PNGAPI
823png_mmx_support(void)
824{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400825 /* Obsolete, to be removed from libpng-1.4.0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800826 return -1;
827}
828#endif /* PNG_1_0_X */
829#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
830
831#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
832#ifdef PNG_SIZE_T
833/* Added at libpng version 1.2.6 */
834 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
835png_size_t PNGAPI
836png_convert_size(size_t size)
837{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400838 if (size > (png_size_t)-1)
839 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
840 return ((png_size_t)size);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800841}
842#endif /* PNG_SIZE_T */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700843
844/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400845#ifdef PNG_cHRM_SUPPORTED
846#ifdef PNG_CHECK_cHRM_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400847
Patrick Scott5f6bd842010-06-28 16:55:16 -0400848void /* PRIVATE */
849png_64bit_product (long v1, long v2, unsigned long *hi_product,
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700850 unsigned long *lo_product)
851{
Geremy Condra14cab862012-06-05 15:17:48 -0700852 int64_t x = (int64_t)v1 * (int64_t)v2;
853 *hi_product = (unsigned long) (x >> 32);
854 *lo_product = (unsigned long) x;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700855}
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400856
Patrick Scott5f6bd842010-06-28 16:55:16 -0400857int /* PRIVATE */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700858png_check_cHRM_fixed(png_structp png_ptr,
859 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
860 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
861 png_fixed_point blue_x, png_fixed_point blue_y)
862{
863 int ret = 1;
864 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
865
866 png_debug(1, "in function png_check_cHRM_fixed");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400867
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700868 if (png_ptr == NULL)
869 return 0;
870
871 if (white_x < 0 || white_y <= 0 ||
872 red_x < 0 || red_y < 0 ||
873 green_x < 0 || green_y < 0 ||
874 blue_x < 0 || blue_y < 0)
875 {
876 png_warning(png_ptr,
877 "Ignoring attempt to set negative chromaticity value");
878 ret = 0;
879 }
880 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
881 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
882 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
883 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
884 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
885 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
886 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
887 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
888 {
889 png_warning(png_ptr,
890 "Ignoring attempt to set chromaticity value exceeding 21474.83");
891 ret = 0;
892 }
893 if (white_x > 100000L - white_y)
894 {
895 png_warning(png_ptr, "Invalid cHRM white point");
896 ret = 0;
897 }
898 if (red_x > 100000L - red_y)
899 {
900 png_warning(png_ptr, "Invalid cHRM red point");
901 ret = 0;
902 }
903 if (green_x > 100000L - green_y)
904 {
905 png_warning(png_ptr, "Invalid cHRM green point");
906 ret = 0;
907 }
908 if (blue_x > 100000L - blue_y)
909 {
910 png_warning(png_ptr, "Invalid cHRM blue point");
911 ret = 0;
912 }
913
914 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
915 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
916
917 if (xy_hi == yx_hi && xy_lo == yx_lo)
918 {
919 png_warning(png_ptr,
920 "Ignoring attempt to set cHRM RGB triangle with zero area");
921 ret = 0;
922 }
923
924 return ret;
925}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400926#endif /* PNG_CHECK_cHRM_SUPPORTED */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700927#endif /* PNG_cHRM_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400928
Narayan Kamath1a173d92015-04-03 14:15:07 +0100929#ifdef __GNUC__
930/* This exists solely to work round a warning from GNU C. */
931static int /* PRIVATE */
932png_gt(size_t a, size_t b)
933{
934 return a > b;
935}
936#else
937# define png_gt(a,b) ((a) > (b))
938#endif
939
Patrick Scott5f6bd842010-06-28 16:55:16 -0400940void /* PRIVATE */
941png_check_IHDR(png_structp png_ptr,
942 png_uint_32 width, png_uint_32 height, int bit_depth,
943 int color_type, int interlace_type, int compression_type,
944 int filter_type)
945{
946 int error = 0;
947
948 /* Check for width and height valid values */
949 if (width == 0)
950 {
951 png_warning(png_ptr, "Image width is zero in IHDR");
952 error = 1;
953 }
Narayan Kamath1a173d92015-04-03 14:15:07 +0100954 else if (width > PNG_UINT_31_MAX)
955 {
956 png_warning(png_ptr, "Invalid image width in IHDR");
957 error = 1;
958 }
959
960 else if (png_gt(width,
961 (PNG_SIZE_MAX >> 3) /* 8-byte RGBA pixels */
962 - 48 /* big_row_buf hack */
963 - 1 /* filter byte */
964 - 7*8 /* rounding width to multiple of 8 pix */
965 - 8)) /* extra max_pixel_depth pad */
966 {
967 /* The size of the row must be within the limits of this architecture.
968 * Because the read code can perform arbitrary transformations the
969 * maximum size is checked here. Because the code in png_read_start_row
970 * adds extra space "for safety's sake" in several places a conservative
971 * limit is used here.
972 *
973 * NOTE: it would be far better to check the size that is actually used,
974 * but the effect in the real world is minor and the changes are more
975 * extensive, therefore much more dangerous and much more difficult to
976 * write in a way that avoids compiler warnings.
977 */
978 png_warning(png_ptr, "Image width is too large for this architecture");
979 error = 1;
980 }
981 else
982 {
983# ifdef PNG_SET_USER_LIMITS_SUPPORTED
984 if (width > png_ptr->user_width_max)
985# else
986 if (width > PNG_USER_WIDTH_MAX)
987# endif
988 {
989 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
990 error = 1;
991 }
992 }
Patrick Scott5f6bd842010-06-28 16:55:16 -0400993
994 if (height == 0)
995 {
996 png_warning(png_ptr, "Image height is zero in IHDR");
997 error = 1;
998 }
Narayan Kamath1a173d92015-04-03 14:15:07 +0100999 else if (height > PNG_UINT_31_MAX)
Patrick Scott5f6bd842010-06-28 16:55:16 -04001000 {
1001 png_warning(png_ptr, "Invalid image height in IHDR");
1002 error = 1;
1003 }
Narayan Kamath1a173d92015-04-03 14:15:07 +01001004 else
1005 {
1006# ifdef PNG_SET_USER_LIMITS_SUPPORTED
1007 if (height > png_ptr->user_height_max)
1008# else
1009 if (height > PNG_USER_HEIGHT_MAX)
1010# endif
1011 {
1012 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
1013 error = 1;
1014 }
1015 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04001016
1017 /* Check other values */
1018 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
1019 bit_depth != 8 && bit_depth != 16)
1020 {
1021 png_warning(png_ptr, "Invalid bit depth in IHDR");
1022 error = 1;
1023 }
1024
1025 if (color_type < 0 || color_type == 1 ||
1026 color_type == 5 || color_type > 6)
1027 {
1028 png_warning(png_ptr, "Invalid color type in IHDR");
1029 error = 1;
1030 }
1031
1032 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
1033 ((color_type == PNG_COLOR_TYPE_RGB ||
1034 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
1035 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
1036 {
1037 png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
1038 error = 1;
1039 }
1040
1041 if (interlace_type >= PNG_INTERLACE_LAST)
1042 {
1043 png_warning(png_ptr, "Unknown interlace method in IHDR");
1044 error = 1;
1045 }
1046
1047 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
1048 {
1049 png_warning(png_ptr, "Unknown compression method in IHDR");
1050 error = 1;
1051 }
1052
1053#ifdef PNG_MNG_FEATURES_SUPPORTED
1054 /* Accept filter_method 64 (intrapixel differencing) only if
1055 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
1056 * 2. Libpng did not read a PNG signature (this filter_method is only
1057 * used in PNG datastreams that are embedded in MNG datastreams) and
1058 * 3. The application called png_permit_mng_features with a mask that
1059 * included PNG_FLAG_MNG_FILTER_64 and
1060 * 4. The filter_method is 64 and
1061 * 5. The color_type is RGB or RGBA
1062 */
1063 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
1064 png_ptr->mng_features_permitted)
1065 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
1066
1067 if (filter_type != PNG_FILTER_TYPE_BASE)
1068 {
1069 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1070 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
1071 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
1072 (color_type == PNG_COLOR_TYPE_RGB ||
1073 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
1074 {
1075 png_warning(png_ptr, "Unknown filter method in IHDR");
1076 error = 1;
1077 }
1078
1079 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
1080 {
1081 png_warning(png_ptr, "Invalid filter method in IHDR");
1082 error = 1;
1083 }
1084 }
1085
1086#else
1087 if (filter_type != PNG_FILTER_TYPE_BASE)
1088 {
1089 png_warning(png_ptr, "Unknown filter method in IHDR");
1090 error = 1;
1091 }
1092#endif
1093
1094 if (error == 1)
1095 png_error(png_ptr, "Invalid IHDR data");
1096}
The Android Open Source Project893912b2009-03-03 19:30:05 -08001097#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */