blob: 7ad9538e674e3f20d4781bc8235f6e2d550b4683 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* png.c - location for general purpose libpng functions
3 *
Patrick Scott5f6bd842010-06-28 16:55:16 -04004 * Last changed in libpng 1.2.43 [February 25, 2010]
5 * Copyright (c) 1998-2010 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"
18
19/* Generate a compiler error if there is an old png.h in the search path. */
Patrick Scott5f6bd842010-06-28 16:55:16 -040020typedef version_1_2_44 Your_png_h_is_not_version_1_2_44;
The Android Open Source Project893912b2009-03-03 19:30:05 -080021
22/* Version information for C files. This had better match the version
Patrick Scott5f6bd842010-06-28 16:55:16 -040023 * string defined in png.h.
24 */
The Android Open Source Project893912b2009-03-03 19:30:05 -080025
26#ifdef PNG_USE_GLOBAL_ARRAYS
27/* png_libpng_ver was changed to a function in version 1.0.5c */
28PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
29
30#ifdef PNG_READ_SUPPORTED
31
32/* png_sig was changed to a function in version 1.0.5c */
33/* Place to hold the signature string for a PNG file. */
34PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
35#endif /* PNG_READ_SUPPORTED */
36
37/* Invoke global declarations for constant strings for known chunk types */
38PNG_IHDR;
39PNG_IDAT;
40PNG_IEND;
41PNG_PLTE;
42PNG_bKGD;
43PNG_cHRM;
44PNG_gAMA;
45PNG_hIST;
46PNG_iCCP;
47PNG_iTXt;
48PNG_oFFs;
49PNG_pCAL;
50PNG_sCAL;
51PNG_pHYs;
52PNG_sBIT;
53PNG_sPLT;
54PNG_sRGB;
55PNG_tEXt;
56PNG_tIME;
57PNG_tRNS;
58PNG_zTXt;
59
60#ifdef PNG_READ_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -040061/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
The Android Open Source Project893912b2009-03-03 19:30:05 -080062
Patrick Scotta0bb96c2009-07-22 11:50:02 -040063/* Start of interlace block */
The Android Open Source Project893912b2009-03-03 19:30:05 -080064PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
65
Patrick Scotta0bb96c2009-07-22 11:50:02 -040066/* Offset to next interlace block */
The Android Open Source Project893912b2009-03-03 19:30:05 -080067PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
68
Patrick Scotta0bb96c2009-07-22 11:50:02 -040069/* Start of interlace block in the y direction */
The Android Open Source Project893912b2009-03-03 19:30:05 -080070PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
71
Patrick Scotta0bb96c2009-07-22 11:50:02 -040072/* Offset to next interlace block in the y direction */
The Android Open Source Project893912b2009-03-03 19:30:05 -080073PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
74
75/* Height of interlace block. This is not currently used - if you need
76 * it, uncomment it here and in png.h
77PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
78*/
79
80/* Mask to determine which pixels are valid in a pass */
Patrick Scott5f6bd842010-06-28 16:55:16 -040081PNG_CONST int FARDATA png_pass_mask[] =
82 {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
The Android Open Source Project893912b2009-03-03 19:30:05 -080083
84/* Mask to determine which pixels to overwrite while displaying */
85PNG_CONST int FARDATA png_pass_dsp_mask[]
86 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
87
88#endif /* PNG_READ_SUPPORTED */
89#endif /* PNG_USE_GLOBAL_ARRAYS */
90
91/* Tells libpng that we have already handled the first "num_bytes" bytes
92 * of the PNG file signature. If the PNG data is embedded into another
93 * stream we can set num_bytes = 8 so that libpng will not attempt to read
94 * or write any of the magic bytes before it starts on the IHDR.
95 */
96
97#ifdef PNG_READ_SUPPORTED
98void PNGAPI
99png_set_sig_bytes(png_structp png_ptr, int num_bytes)
100{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400101 png_debug(1, "in png_set_sig_bytes");
102
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400103 if (png_ptr == NULL)
104 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400105
The Android Open Source Project893912b2009-03-03 19:30:05 -0800106 if (num_bytes > 8)
107 png_error(png_ptr, "Too many bytes for PNG signature.");
108
109 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
110}
111
112/* Checks whether the supplied bytes match the PNG signature. We allow
113 * checking less than the full 8-byte signature so that those apps that
114 * already read the first few bytes of a file to determine the file type
115 * can simply check the remaining bytes for extra assurance. Returns
116 * an integer less than, equal to, or greater than zero if sig is found,
117 * respectively, to be less than, to match, or be greater than the correct
118 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
119 */
120int PNGAPI
121png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
122{
123 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
124 if (num_to_check > 8)
125 num_to_check = 8;
126 else if (num_to_check < 1)
127 return (-1);
128
129 if (start > 7)
130 return (-1);
131
132 if (start + num_to_check > 8)
133 num_to_check = 8 - start;
134
135 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
136}
137
138#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
139/* (Obsolete) function to check signature bytes. It does not allow one
140 * to check a partial signature. This function might be removed in the
141 * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
142 */
143int PNGAPI
144png_check_sig(png_bytep sig, int num)
145{
146 return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
147}
148#endif
149#endif /* PNG_READ_SUPPORTED */
150
151#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
152/* Function to allocate memory for zlib and clear it to 0. */
153#ifdef PNG_1_0_X
154voidpf PNGAPI
155#else
Patrick Scott5f6bd842010-06-28 16:55:16 -0400156voidpf /* PRIVATE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800157#endif
158png_zalloc(voidpf png_ptr, uInt items, uInt size)
159{
160 png_voidp ptr;
161 png_structp p=(png_structp)png_ptr;
162 png_uint_32 save_flags=p->flags;
163 png_uint_32 num_bytes;
164
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400165 if (png_ptr == NULL)
166 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800167 if (items > PNG_UINT_32_MAX/size)
168 {
169 png_warning (p, "Potential overflow in png_zalloc()");
170 return (NULL);
171 }
172 num_bytes = (png_uint_32)items * size;
173
174 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
175 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
176 p->flags=save_flags;
177
178#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
179 if (ptr == NULL)
180 return ((voidpf)ptr);
181
182 if (num_bytes > (png_uint_32)0x8000L)
183 {
184 png_memset(ptr, 0, (png_size_t)0x8000L);
185 png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
186 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
187 }
188 else
189 {
190 png_memset(ptr, 0, (png_size_t)num_bytes);
191 }
192#endif
193 return ((voidpf)ptr);
194}
195
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400196/* Function to free memory for zlib */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800197#ifdef PNG_1_0_X
198void PNGAPI
199#else
Patrick Scott5f6bd842010-06-28 16:55:16 -0400200void /* PRIVATE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800201#endif
202png_zfree(voidpf png_ptr, voidpf ptr)
203{
204 png_free((png_structp)png_ptr, (png_voidp)ptr);
205}
206
207/* Reset the CRC variable to 32 bits of 1's. Care must be taken
208 * in case CRC is > 32 bits to leave the top bits 0.
209 */
210void /* PRIVATE */
211png_reset_crc(png_structp png_ptr)
212{
213 png_ptr->crc = crc32(0, Z_NULL, 0);
214}
215
216/* Calculate the CRC over a section of data. We can only pass as
217 * much data to this routine as the largest single buffer size. We
218 * also check that this data will actually be used before going to the
219 * trouble of calculating it.
220 */
221void /* PRIVATE */
222png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
223{
224 int need_crc = 1;
225
226 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
227 {
228 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
229 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
230 need_crc = 0;
231 }
232 else /* critical */
233 {
234 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
235 need_crc = 0;
236 }
237
238 if (need_crc)
239 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
240}
241
242/* Allocate the memory for an info_struct for the application. We don't
243 * really need the png_ptr, but it could potentially be useful in the
244 * future. This should be used in favour of malloc(png_sizeof(png_info))
245 * and png_info_init() so that applications that want to use a shared
246 * libpng don't have to be recompiled if png_info changes size.
247 */
248png_infop PNGAPI
249png_create_info_struct(png_structp png_ptr)
250{
251 png_infop info_ptr;
252
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700253 png_debug(1, "in png_create_info_struct");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400254
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400255 if (png_ptr == NULL)
256 return (NULL);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400257
The Android Open Source Project893912b2009-03-03 19:30:05 -0800258#ifdef PNG_USER_MEM_SUPPORTED
259 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
260 png_ptr->malloc_fn, png_ptr->mem_ptr);
261#else
262 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
263#endif
264 if (info_ptr != NULL)
265 png_info_init_3(&info_ptr, png_sizeof(png_info));
266
267 return (info_ptr);
268}
269
270/* This function frees the memory associated with a single info struct.
271 * Normally, one would use either png_destroy_read_struct() or
272 * png_destroy_write_struct() to free an info struct, but this may be
273 * useful for some applications.
274 */
275void PNGAPI
276png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
277{
278 png_infop info_ptr = NULL;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400279
280 png_debug(1, "in png_destroy_info_struct");
281
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400282 if (png_ptr == NULL)
283 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800284
The Android Open Source Project893912b2009-03-03 19:30:05 -0800285 if (info_ptr_ptr != NULL)
286 info_ptr = *info_ptr_ptr;
287
288 if (info_ptr != NULL)
289 {
290 png_info_destroy(png_ptr, info_ptr);
291
292#ifdef PNG_USER_MEM_SUPPORTED
293 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
294 png_ptr->mem_ptr);
295#else
296 png_destroy_struct((png_voidp)info_ptr);
297#endif
298 *info_ptr_ptr = NULL;
299 }
300}
301
302/* Initialize the info structure. This is now an internal function (0.89)
303 * and applications using it are urged to use png_create_info_struct()
304 * instead.
305 */
306#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
307#undef png_info_init
308void PNGAPI
309png_info_init(png_infop info_ptr)
310{
311 /* We only come here via pre-1.0.12-compiled applications */
312 png_info_init_3(&info_ptr, 0);
313}
314#endif
315
316void PNGAPI
317png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
318{
319 png_infop info_ptr = *ptr_ptr;
320
Patrick Scott5f6bd842010-06-28 16:55:16 -0400321 png_debug(1, "in png_info_init_3");
322
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400323 if (info_ptr == NULL)
324 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800325
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700326 if (png_sizeof(png_info) > png_info_struct_size)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400327 {
328 png_destroy_struct(info_ptr);
329 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
330 *ptr_ptr = info_ptr;
331 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800332
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400333 /* Set everything to 0 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700334 png_memset(info_ptr, 0, png_sizeof(png_info));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800335}
336
337#ifdef PNG_FREE_ME_SUPPORTED
338void PNGAPI
339png_data_freer(png_structp png_ptr, png_infop info_ptr,
340 int freer, png_uint_32 mask)
341{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700342 png_debug(1, "in png_data_freer");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400343
The Android Open Source Project893912b2009-03-03 19:30:05 -0800344 if (png_ptr == NULL || info_ptr == NULL)
345 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400346
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700347 if (freer == PNG_DESTROY_WILL_FREE_DATA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800348 info_ptr->free_me |= mask;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700349 else if (freer == PNG_USER_WILL_FREE_DATA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800350 info_ptr->free_me &= ~mask;
351 else
352 png_warning(png_ptr,
353 "Unknown freer parameter in png_data_freer.");
354}
355#endif
356
357void PNGAPI
358png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
359 int num)
360{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700361 png_debug(1, "in png_free_data");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400362
The Android Open Source Project893912b2009-03-03 19:30:05 -0800363 if (png_ptr == NULL || info_ptr == NULL)
364 return;
365
Patrick Scott5f6bd842010-06-28 16:55:16 -0400366#ifdef PNG_TEXT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400367 /* Free text item num or (if num == -1) all text items */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800368#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400369 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800370#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400371 if (mask & PNG_FREE_TEXT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800372#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800373 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400374 if (num != -1)
375 {
376 if (info_ptr->text && info_ptr->text[num].key)
377 {
378 png_free(png_ptr, info_ptr->text[num].key);
379 info_ptr->text[num].key = NULL;
380 }
381 }
382 else
383 {
384 int i;
385 for (i = 0; i < info_ptr->num_text; i++)
386 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
387 png_free(png_ptr, info_ptr->text);
388 info_ptr->text = NULL;
389 info_ptr->num_text=0;
390 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800391 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800392#endif
393
Patrick Scott5f6bd842010-06-28 16:55:16 -0400394#ifdef PNG_tRNS_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400395 /* Free any tRNS entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800396#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400397 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800398#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400399 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800400#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400401 {
402 png_free(png_ptr, info_ptr->trans);
403 info_ptr->trans = NULL;
404 info_ptr->valid &= ~PNG_INFO_tRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800405#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400406 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800407#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400408 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800409#endif
410
Patrick Scott5f6bd842010-06-28 16:55:16 -0400411#ifdef PNG_sCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400412 /* Free any sCAL entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800413#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400414 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800415#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400416 if (mask & PNG_FREE_SCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800417#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400418 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800419#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400420 png_free(png_ptr, info_ptr->scal_s_width);
421 png_free(png_ptr, info_ptr->scal_s_height);
422 info_ptr->scal_s_width = NULL;
423 info_ptr->scal_s_height = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800424#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400425 info_ptr->valid &= ~PNG_INFO_sCAL;
426 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800427#endif
428
Patrick Scott5f6bd842010-06-28 16:55:16 -0400429#ifdef PNG_pCAL_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400430 /* Free any pCAL entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800431#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400432 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800433#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400434 if (mask & PNG_FREE_PCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800435#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400436 {
437 png_free(png_ptr, info_ptr->pcal_purpose);
438 png_free(png_ptr, info_ptr->pcal_units);
439 info_ptr->pcal_purpose = NULL;
440 info_ptr->pcal_units = NULL;
441 if (info_ptr->pcal_params != NULL)
442 {
443 int i;
444 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
445 {
446 png_free(png_ptr, info_ptr->pcal_params[i]);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400447 info_ptr->pcal_params[i] = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400448 }
449 png_free(png_ptr, info_ptr->pcal_params);
450 info_ptr->pcal_params = NULL;
451 }
452 info_ptr->valid &= ~PNG_INFO_pCAL;
453 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800454#endif
455
Patrick Scott5f6bd842010-06-28 16:55:16 -0400456#ifdef PNG_iCCP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400457 /* Free any iCCP entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800458#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400459 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800460#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400461 if (mask & PNG_FREE_ICCP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800462#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400463 {
464 png_free(png_ptr, info_ptr->iccp_name);
465 png_free(png_ptr, info_ptr->iccp_profile);
466 info_ptr->iccp_name = NULL;
467 info_ptr->iccp_profile = NULL;
468 info_ptr->valid &= ~PNG_INFO_iCCP;
469 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800470#endif
471
Patrick Scott5f6bd842010-06-28 16:55:16 -0400472#ifdef PNG_sPLT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400473 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800474#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400475 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800476#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400477 if (mask & PNG_FREE_SPLT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800478#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800479 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400480 if (num != -1)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800481 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400482 if (info_ptr->splt_palettes)
483 {
484 png_free(png_ptr, info_ptr->splt_palettes[num].name);
485 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
486 info_ptr->splt_palettes[num].name = NULL;
487 info_ptr->splt_palettes[num].entries = NULL;
488 }
489 }
490 else
491 {
492 if (info_ptr->splt_palettes_num)
493 {
494 int i;
495 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
496 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
497
498 png_free(png_ptr, info_ptr->splt_palettes);
499 info_ptr->splt_palettes = NULL;
500 info_ptr->splt_palettes_num = 0;
501 }
502 info_ptr->valid &= ~PNG_INFO_sPLT;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800503 }
504 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800505#endif
506
Patrick Scott5f6bd842010-06-28 16:55:16 -0400507#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400508 if (png_ptr->unknown_chunk.data)
509 {
510 png_free(png_ptr, png_ptr->unknown_chunk.data);
511 png_ptr->unknown_chunk.data = NULL;
512 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700513
The Android Open Source Project893912b2009-03-03 19:30:05 -0800514#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400515 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800516#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400517 if (mask & PNG_FREE_UNKN)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800518#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800519 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400520 if (num != -1)
521 {
522 if (info_ptr->unknown_chunks)
523 {
524 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
525 info_ptr->unknown_chunks[num].data = NULL;
526 }
527 }
528 else
529 {
530 int i;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800531
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400532 if (info_ptr->unknown_chunks_num)
533 {
534 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
535 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800536
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400537 png_free(png_ptr, info_ptr->unknown_chunks);
538 info_ptr->unknown_chunks = NULL;
539 info_ptr->unknown_chunks_num = 0;
540 }
541 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800542 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800543#endif
544
Patrick Scott5f6bd842010-06-28 16:55:16 -0400545#ifdef PNG_hIST_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400546 /* Free any hIST entry */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800547#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400548 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800549#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400550 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800551#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400552 {
553 png_free(png_ptr, info_ptr->hist);
554 info_ptr->hist = NULL;
555 info_ptr->valid &= ~PNG_INFO_hIST;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800556#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400557 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800558#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400559 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800560#endif
561
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400562 /* Free any PLTE entry that was internally allocated */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800563#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400564 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800565#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400566 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800567#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400568 {
569 png_zfree(png_ptr, info_ptr->palette);
570 info_ptr->palette = NULL;
571 info_ptr->valid &= ~PNG_INFO_PLTE;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800572#ifndef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400573 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800574#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400575 info_ptr->num_palette = 0;
576 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800577
Patrick Scott5f6bd842010-06-28 16:55:16 -0400578#ifdef PNG_INFO_IMAGE_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400579 /* Free any image bits attached to the info structure */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800580#ifdef PNG_FREE_ME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400581 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800582#else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400583 if (mask & PNG_FREE_ROWS)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800584#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400585 {
586 if (info_ptr->row_pointers)
587 {
588 int row;
589 for (row = 0; row < (int)info_ptr->height; row++)
590 {
591 png_free(png_ptr, info_ptr->row_pointers[row]);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400592 info_ptr->row_pointers[row] = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400593 }
594 png_free(png_ptr, info_ptr->row_pointers);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400595 info_ptr->row_pointers = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400596 }
597 info_ptr->valid &= ~PNG_INFO_IDAT;
598 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800599#endif
600
601#ifdef PNG_FREE_ME_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700602 if (num == -1)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400603 info_ptr->free_me &= ~mask;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800604 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400605 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800606#endif
607}
608
609/* This is an internal routine to free any memory that the info struct is
610 * pointing to before re-using it or freeing the struct itself. Recall
611 * that png_free() checks for NULL pointers for us.
612 */
613void /* PRIVATE */
614png_info_destroy(png_structp png_ptr, png_infop info_ptr)
615{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700616 png_debug(1, "in png_info_destroy");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800617
618 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
619
Patrick Scott5f6bd842010-06-28 16:55:16 -0400620#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800621 if (png_ptr->num_chunk_list)
622 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400623 png_free(png_ptr, png_ptr->chunk_list);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400624 png_ptr->chunk_list = NULL;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400625 png_ptr->num_chunk_list = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800626 }
627#endif
628
629 png_info_init_3(&info_ptr, png_sizeof(png_info));
630}
631#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
632
633/* This function returns a pointer to the io_ptr associated with the user
634 * functions. The application should free any memory associated with this
635 * pointer before png_write_destroy() or png_read_destroy() are called.
636 */
637png_voidp PNGAPI
638png_get_io_ptr(png_structp png_ptr)
639{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400640 if (png_ptr == NULL)
641 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800642 return (png_ptr->io_ptr);
643}
644
645#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400646#ifdef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800647/* Initialize the default input/output functions for the PNG file. If you
648 * use your own read or write routines, you can call either png_set_read_fn()
649 * or png_set_write_fn() instead of png_init_io(). If you have defined
650 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
651 * necessarily available.
652 */
653void PNGAPI
654png_init_io(png_structp png_ptr, png_FILE_p fp)
655{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700656 png_debug(1, "in png_init_io");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400657
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400658 if (png_ptr == NULL)
659 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400660
The Android Open Source Project893912b2009-03-03 19:30:05 -0800661 png_ptr->io_ptr = (png_voidp)fp;
662}
663#endif
664
Patrick Scott5f6bd842010-06-28 16:55:16 -0400665#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800666/* Convert the supplied time into an RFC 1123 string suitable for use in
667 * a "Creation Time" or other text-based time string.
668 */
669png_charp PNGAPI
670png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
671{
672 static PNG_CONST char short_months[12][4] =
673 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
674 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
675
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400676 if (png_ptr == NULL)
677 return (NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800678 if (png_ptr->time_buffer == NULL)
679 {
680 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
681 png_sizeof(char)));
682 }
683
Patrick Scott5f6bd842010-06-28 16:55:16 -0400684#ifdef _WIN32_WCE
The Android Open Source Project893912b2009-03-03 19:30:05 -0800685 {
686 wchar_t time_buf[29];
687 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
688 ptime->day % 32, short_months[(ptime->month - 1) % 12],
689 ptime->year, ptime->hour % 24, ptime->minute % 60,
690 ptime->second % 61);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400691 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer,
692 29, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800693 }
694#else
695#ifdef USE_FAR_KEYWORD
696 {
697 char near_time_buf[29];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700698 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800699 ptime->day % 32, short_months[(ptime->month - 1) % 12],
700 ptime->year, ptime->hour % 24, ptime->minute % 60,
701 ptime->second % 61);
702 png_memcpy(png_ptr->time_buffer, near_time_buf,
703 29*png_sizeof(char));
704 }
705#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700706 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800707 ptime->day % 32, short_months[(ptime->month - 1) % 12],
708 ptime->year, ptime->hour % 24, ptime->minute % 60,
709 ptime->second % 61);
710#endif
711#endif /* _WIN32_WCE */
712 return ((png_charp)png_ptr->time_buffer);
713}
714#endif /* PNG_TIME_RFC1123_SUPPORTED */
715
716#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
717
718png_charp PNGAPI
719png_get_copyright(png_structp png_ptr)
720{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400721 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400722#ifdef PNG_STRING_COPYRIGHT
723 return PNG_STRING_COPYRIGHT
724#else
725#ifdef __STDC__
726 return ((png_charp) PNG_STRING_NEWLINE \
727 "libpng version 1.2.44 - June 26, 2010" PNG_STRING_NEWLINE \
728 "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
729 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
730 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
731 PNG_STRING_NEWLINE);
732#else
733 return ((png_charp) "libpng version 1.2.44 - June 26, 2010\
734 Copyright (c) 1998-2010 Glenn Randers-Pehrson\
735 Copyright (c) 1996-1997 Andreas Dilger\
736 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
737#endif
738#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800739}
740
741/* The following return the library version as a short string in the
742 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
743 * used with your application, print out PNG_LIBPNG_VER_STRING, which
744 * is defined in png.h.
745 * Note: now there is no difference between png_get_libpng_ver() and
746 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
747 * it is guaranteed that png.c uses the correct version of png.h.
748 */
749png_charp PNGAPI
750png_get_libpng_ver(png_structp png_ptr)
751{
752 /* Version of *.c files used when building libpng */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400753 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800754 return ((png_charp) PNG_LIBPNG_VER_STRING);
755}
756
757png_charp PNGAPI
758png_get_header_ver(png_structp png_ptr)
759{
760 /* Version of *.h files used when building libpng */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400761 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800762 return ((png_charp) PNG_LIBPNG_VER_STRING);
763}
764
765png_charp PNGAPI
766png_get_header_version(png_structp png_ptr)
767{
768 /* Returns longer string containing both version and date */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400769 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400770#ifdef __STDC__
The Android Open Source Project893912b2009-03-03 19:30:05 -0800771 return ((png_charp) PNG_HEADER_VERSION_STRING
772#ifndef PNG_READ_SUPPORTED
773 " (NO READ SUPPORT)"
774#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400775 PNG_STRING_NEWLINE);
776#else
777 return ((png_charp) PNG_HEADER_VERSION_STRING);
778#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800779}
780
781#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
782#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
783int PNGAPI
784png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
785{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400786 /* 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 -0800787 int i;
788 png_bytep p;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700789 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800790 return 0;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700791 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
792 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800793 if (!png_memcmp(chunk_name, p, 4))
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700794 return ((int)*(p + 4));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800795 return 0;
796}
797#endif
798
799/* This function, added to libpng-1.0.6g, is untested. */
800int PNGAPI
801png_reset_zstream(png_structp png_ptr)
802{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400803 if (png_ptr == NULL)
804 return Z_STREAM_ERROR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800805 return (inflateReset(&png_ptr->zstream));
806}
807#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
808
809/* This function was added to libpng-1.0.7 */
810png_uint_32 PNGAPI
811png_access_version_number(void)
812{
813 /* Version of *.c files used when building libpng */
814 return((png_uint_32) PNG_LIBPNG_VER);
815}
816
817
818#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400819#ifndef PNG_1_0_X
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400820/* This function was added to libpng 1.2.0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800821int PNGAPI
822png_mmx_support(void)
823{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400824 /* Obsolete, to be removed from libpng-1.4.0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800825 return -1;
826}
827#endif /* PNG_1_0_X */
828#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
829
830#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
831#ifdef PNG_SIZE_T
832/* Added at libpng version 1.2.6 */
833 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
834png_size_t PNGAPI
835png_convert_size(size_t size)
836{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400837 if (size > (png_size_t)-1)
838 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
839 return ((png_size_t)size);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800840}
841#endif /* PNG_SIZE_T */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700842
843/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400844#ifdef PNG_cHRM_SUPPORTED
845#ifdef PNG_CHECK_cHRM_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400846
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700847/*
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400848 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
849 * arithmetic, to produce a 64 bit result in the HI/LO words.
850 *
851 * A B
852 * x C D
853 * ------
854 * AD || BD
855 * AC || CB || 0
856 *
857 * where A and B are the high and low 16-bit words of V1,
858 * C and D are the 16-bit words of V2, AD is the product of
859 * A and D, and X || Y is (X << 16) + Y.
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700860*/
861
Patrick Scott5f6bd842010-06-28 16:55:16 -0400862void /* PRIVATE */
863png_64bit_product (long v1, long v2, unsigned long *hi_product,
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700864 unsigned long *lo_product)
865{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400866 int a, b, c, d;
867 long lo, hi, x, y;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700868
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400869 a = (v1 >> 16) & 0xffff;
870 b = v1 & 0xffff;
871 c = (v2 >> 16) & 0xffff;
872 d = v2 & 0xffff;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700873
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400874 lo = b * d; /* BD */
875 x = a * d + c * b; /* AD + CB */
876 y = ((lo >> 16) & 0xffff) + x;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700877
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400878 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
879 hi = (y >> 16) & 0xffff;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700880
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400881 hi += a * c; /* AC */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700882
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400883 *hi_product = (unsigned long)hi;
884 *lo_product = (unsigned long)lo;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700885}
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400886
Patrick Scott5f6bd842010-06-28 16:55:16 -0400887int /* PRIVATE */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700888png_check_cHRM_fixed(png_structp png_ptr,
889 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
890 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
891 png_fixed_point blue_x, png_fixed_point blue_y)
892{
893 int ret = 1;
894 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
895
896 png_debug(1, "in function png_check_cHRM_fixed");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400897
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700898 if (png_ptr == NULL)
899 return 0;
900
901 if (white_x < 0 || white_y <= 0 ||
902 red_x < 0 || red_y < 0 ||
903 green_x < 0 || green_y < 0 ||
904 blue_x < 0 || blue_y < 0)
905 {
906 png_warning(png_ptr,
907 "Ignoring attempt to set negative chromaticity value");
908 ret = 0;
909 }
910 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
911 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
912 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
913 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
914 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
915 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
916 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
917 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
918 {
919 png_warning(png_ptr,
920 "Ignoring attempt to set chromaticity value exceeding 21474.83");
921 ret = 0;
922 }
923 if (white_x > 100000L - white_y)
924 {
925 png_warning(png_ptr, "Invalid cHRM white point");
926 ret = 0;
927 }
928 if (red_x > 100000L - red_y)
929 {
930 png_warning(png_ptr, "Invalid cHRM red point");
931 ret = 0;
932 }
933 if (green_x > 100000L - green_y)
934 {
935 png_warning(png_ptr, "Invalid cHRM green point");
936 ret = 0;
937 }
938 if (blue_x > 100000L - blue_y)
939 {
940 png_warning(png_ptr, "Invalid cHRM blue point");
941 ret = 0;
942 }
943
944 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
945 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
946
947 if (xy_hi == yx_hi && xy_lo == yx_lo)
948 {
949 png_warning(png_ptr,
950 "Ignoring attempt to set cHRM RGB triangle with zero area");
951 ret = 0;
952 }
953
954 return ret;
955}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400956#endif /* PNG_CHECK_cHRM_SUPPORTED */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700957#endif /* PNG_cHRM_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400958
959void /* PRIVATE */
960png_check_IHDR(png_structp png_ptr,
961 png_uint_32 width, png_uint_32 height, int bit_depth,
962 int color_type, int interlace_type, int compression_type,
963 int filter_type)
964{
965 int error = 0;
966
967 /* Check for width and height valid values */
968 if (width == 0)
969 {
970 png_warning(png_ptr, "Image width is zero in IHDR");
971 error = 1;
972 }
973
974 if (height == 0)
975 {
976 png_warning(png_ptr, "Image height is zero in IHDR");
977 error = 1;
978 }
979
980#ifdef PNG_SET_USER_LIMITS_SUPPORTED
981 if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
982#else
983 if (width > PNG_USER_WIDTH_MAX)
984#endif
985 {
986 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
987 error = 1;
988 }
989
990#ifdef PNG_SET_USER_LIMITS_SUPPORTED
991 if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
992#else
993 if (height > PNG_USER_HEIGHT_MAX)
994#endif
995 {
996 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
997 error = 1;
998 }
999
1000 if (width > PNG_UINT_31_MAX)
1001 {
1002 png_warning(png_ptr, "Invalid image width in IHDR");
1003 error = 1;
1004 }
1005
1006 if ( height > PNG_UINT_31_MAX)
1007 {
1008 png_warning(png_ptr, "Invalid image height in IHDR");
1009 error = 1;
1010 }
1011
1012 if ( width > (PNG_UINT_32_MAX
1013 >> 3) /* 8-byte RGBA pixels */
1014 - 64 /* bigrowbuf hack */
1015 - 1 /* filter byte */
1016 - 7*8 /* rounding of width to multiple of 8 pixels */
1017 - 8) /* extra max_pixel_depth pad */
1018 png_warning(png_ptr, "Width is too large for libpng to process pixels");
1019
1020 /* Check other values */
1021 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
1022 bit_depth != 8 && bit_depth != 16)
1023 {
1024 png_warning(png_ptr, "Invalid bit depth in IHDR");
1025 error = 1;
1026 }
1027
1028 if (color_type < 0 || color_type == 1 ||
1029 color_type == 5 || color_type > 6)
1030 {
1031 png_warning(png_ptr, "Invalid color type in IHDR");
1032 error = 1;
1033 }
1034
1035 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
1036 ((color_type == PNG_COLOR_TYPE_RGB ||
1037 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
1038 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
1039 {
1040 png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
1041 error = 1;
1042 }
1043
1044 if (interlace_type >= PNG_INTERLACE_LAST)
1045 {
1046 png_warning(png_ptr, "Unknown interlace method in IHDR");
1047 error = 1;
1048 }
1049
1050 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
1051 {
1052 png_warning(png_ptr, "Unknown compression method in IHDR");
1053 error = 1;
1054 }
1055
1056#ifdef PNG_MNG_FEATURES_SUPPORTED
1057 /* Accept filter_method 64 (intrapixel differencing) only if
1058 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
1059 * 2. Libpng did not read a PNG signature (this filter_method is only
1060 * used in PNG datastreams that are embedded in MNG datastreams) and
1061 * 3. The application called png_permit_mng_features with a mask that
1062 * included PNG_FLAG_MNG_FILTER_64 and
1063 * 4. The filter_method is 64 and
1064 * 5. The color_type is RGB or RGBA
1065 */
1066 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
1067 png_ptr->mng_features_permitted)
1068 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
1069
1070 if (filter_type != PNG_FILTER_TYPE_BASE)
1071 {
1072 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1073 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
1074 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
1075 (color_type == PNG_COLOR_TYPE_RGB ||
1076 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
1077 {
1078 png_warning(png_ptr, "Unknown filter method in IHDR");
1079 error = 1;
1080 }
1081
1082 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
1083 {
1084 png_warning(png_ptr, "Invalid filter method in IHDR");
1085 error = 1;
1086 }
1087 }
1088
1089#else
1090 if (filter_type != PNG_FILTER_TYPE_BASE)
1091 {
1092 png_warning(png_ptr, "Unknown filter method in IHDR");
1093 error = 1;
1094 }
1095#endif
1096
1097 if (error == 1)
1098 png_error(png_ptr, "Invalid IHDR data");
1099}
The Android Open Source Project893912b2009-03-03 19:30:05 -08001100#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */