The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | |
| 2 | /* png.c - location for general purpose libpng functions |
| 3 | * |
Eric Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 4 | * Last changed in libpng 1.2.46 [February 25, 2011] |
| 5 | * Copyright (c) 1998-2011 Glenn Randers-Pehrson |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 8 | * |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #define PNG_INTERNAL |
| 15 | #define PNG_NO_EXTERN |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 16 | #define PNG_NO_PEDANTIC_WARNINGS |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 17 | #include "png.h" |
Geremy Condra | 14cab86 | 2012-06-05 15:17:48 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 19 | |
| 20 | /* Generate a compiler error if there is an old png.h in the search path. */ |
Eric Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 21 | typedef version_1_2_46 Your_png_h_is_not_version_1_2_46; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 22 | |
| 23 | /* Version information for C files. This had better match the version |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 24 | * string defined in png.h. |
| 25 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 26 | |
| 27 | #ifdef PNG_USE_GLOBAL_ARRAYS |
| 28 | /* png_libpng_ver was changed to a function in version 1.0.5c */ |
| 29 | PNG_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. */ |
| 35 | PNG_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 */ |
| 39 | PNG_IHDR; |
| 40 | PNG_IDAT; |
| 41 | PNG_IEND; |
| 42 | PNG_PLTE; |
| 43 | PNG_bKGD; |
| 44 | PNG_cHRM; |
| 45 | PNG_gAMA; |
| 46 | PNG_hIST; |
| 47 | PNG_iCCP; |
| 48 | PNG_iTXt; |
| 49 | PNG_oFFs; |
| 50 | PNG_pCAL; |
| 51 | PNG_sCAL; |
| 52 | PNG_pHYs; |
| 53 | PNG_sBIT; |
| 54 | PNG_sPLT; |
| 55 | PNG_sRGB; |
| 56 | PNG_tEXt; |
| 57 | PNG_tIME; |
| 58 | PNG_tRNS; |
| 59 | PNG_zTXt; |
| 60 | |
| 61 | #ifdef PNG_READ_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 62 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 63 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 64 | /* Start of interlace block */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 65 | PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; |
| 66 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 67 | /* Offset to next interlace block */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 68 | PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; |
| 69 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 70 | /* Start of interlace block in the y direction */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 71 | PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; |
| 72 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 73 | /* Offset to next interlace block in the y direction */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 74 | PNG_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 |
| 78 | PNG_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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 82 | PNG_CONST int FARDATA png_pass_mask[] = |
| 83 | {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 84 | |
| 85 | /* Mask to determine which pixels to overwrite while displaying */ |
| 86 | PNG_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 |
| 99 | void PNGAPI |
| 100 | png_set_sig_bytes(png_structp png_ptr, int num_bytes) |
| 101 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 102 | png_debug(1, "in png_set_sig_bytes"); |
| 103 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 104 | if (png_ptr == NULL) |
| 105 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 106 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 107 | 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 | */ |
| 121 | int PNGAPI |
| 122 | png_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 | */ |
| 144 | int PNGAPI |
| 145 | png_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 |
| 155 | voidpf PNGAPI |
| 156 | #else |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 157 | voidpf /* PRIVATE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 158 | #endif |
| 159 | png_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 Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 166 | if (png_ptr == NULL) |
| 167 | return (NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 168 | 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 Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 197 | /* Function to free memory for zlib */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 198 | #ifdef PNG_1_0_X |
| 199 | void PNGAPI |
| 200 | #else |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 201 | void /* PRIVATE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 202 | #endif |
| 203 | png_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 | */ |
| 211 | void /* PRIVATE */ |
| 212 | png_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 | */ |
| 222 | void /* PRIVATE */ |
| 223 | png_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 | */ |
| 249 | png_infop PNGAPI |
| 250 | png_create_info_struct(png_structp png_ptr) |
| 251 | { |
| 252 | png_infop info_ptr; |
| 253 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 254 | png_debug(1, "in png_create_info_struct"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 255 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 256 | if (png_ptr == NULL) |
| 257 | return (NULL); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 258 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 259 | #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 | */ |
| 276 | void PNGAPI |
| 277 | png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) |
| 278 | { |
| 279 | png_infop info_ptr = NULL; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 280 | |
| 281 | png_debug(1, "in png_destroy_info_struct"); |
| 282 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 283 | if (png_ptr == NULL) |
| 284 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 285 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 286 | 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 |
| 309 | void PNGAPI |
| 310 | png_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 | |
| 317 | void PNGAPI |
| 318 | png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) |
| 319 | { |
| 320 | png_infop info_ptr = *ptr_ptr; |
| 321 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 322 | png_debug(1, "in png_info_init_3"); |
| 323 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 324 | if (info_ptr == NULL) |
| 325 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 327 | if (png_sizeof(png_info) > png_info_struct_size) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 328 | { |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 333 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 334 | /* Set everything to 0 */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 335 | png_memset(info_ptr, 0, png_sizeof(png_info)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | #ifdef PNG_FREE_ME_SUPPORTED |
| 339 | void PNGAPI |
| 340 | png_data_freer(png_structp png_ptr, png_infop info_ptr, |
| 341 | int freer, png_uint_32 mask) |
| 342 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 343 | png_debug(1, "in png_data_freer"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 344 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 345 | if (png_ptr == NULL || info_ptr == NULL) |
| 346 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 347 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 348 | if (freer == PNG_DESTROY_WILL_FREE_DATA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 349 | info_ptr->free_me |= mask; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 350 | else if (freer == PNG_USER_WILL_FREE_DATA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 351 | info_ptr->free_me &= ~mask; |
| 352 | else |
| 353 | png_warning(png_ptr, |
| 354 | "Unknown freer parameter in png_data_freer."); |
| 355 | } |
| 356 | #endif |
| 357 | |
| 358 | void PNGAPI |
| 359 | png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, |
| 360 | int num) |
| 361 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 362 | png_debug(1, "in png_free_data"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 363 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 364 | if (png_ptr == NULL || info_ptr == NULL) |
| 365 | return; |
| 366 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 367 | #ifdef PNG_TEXT_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 368 | /* Free text item num or (if num == -1) all text items */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 369 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 370 | if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 371 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 372 | if (mask & PNG_FREE_TEXT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 373 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 374 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 375 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 392 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 393 | #endif |
| 394 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 395 | #ifdef PNG_tRNS_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 396 | /* Free any tRNS entry */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 397 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 398 | if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 399 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 400 | if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 401 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 402 | { |
| 403 | png_free(png_ptr, info_ptr->trans); |
| 404 | info_ptr->trans = NULL; |
| 405 | info_ptr->valid &= ~PNG_INFO_tRNS; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 406 | #ifndef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 407 | png_ptr->flags &= ~PNG_FLAG_FREE_TRNS; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 408 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 409 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 410 | #endif |
| 411 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 412 | #ifdef PNG_sCAL_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 413 | /* Free any sCAL entry */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 414 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 415 | if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 416 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 417 | if (mask & PNG_FREE_SCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 418 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 419 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 420 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 421 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 425 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 426 | info_ptr->valid &= ~PNG_INFO_sCAL; |
| 427 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 428 | #endif |
| 429 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 430 | #ifdef PNG_pCAL_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 431 | /* Free any pCAL entry */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 432 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 433 | if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 434 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 435 | if (mask & PNG_FREE_PCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 436 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 437 | { |
| 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 448 | info_ptr->pcal_params[i] = NULL; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 449 | } |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 455 | #endif |
| 456 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 457 | #ifdef PNG_iCCP_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 458 | /* Free any iCCP entry */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 459 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 460 | if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 461 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 462 | if (mask & PNG_FREE_ICCP) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 463 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 464 | { |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 471 | #endif |
| 472 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 473 | #ifdef PNG_sPLT_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 474 | /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 475 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 476 | if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 477 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 478 | if (mask & PNG_FREE_SPLT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 479 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 480 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 481 | if (num != -1) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 482 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 483 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 506 | #endif |
| 507 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 508 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 509 | 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 Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 514 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 515 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 516 | if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 517 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 518 | if (mask & PNG_FREE_UNKN) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 519 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 520 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 521 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 532 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 533 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 537 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 538 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 543 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 544 | #endif |
| 545 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 546 | #ifdef PNG_hIST_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 547 | /* Free any hIST entry */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 548 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 549 | if ((mask & PNG_FREE_HIST) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 550 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 551 | if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 552 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 553 | { |
| 554 | png_free(png_ptr, info_ptr->hist); |
| 555 | info_ptr->hist = NULL; |
| 556 | info_ptr->valid &= ~PNG_INFO_hIST; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 557 | #ifndef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 558 | png_ptr->flags &= ~PNG_FLAG_FREE_HIST; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 559 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 560 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 561 | #endif |
| 562 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 563 | /* Free any PLTE entry that was internally allocated */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 564 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 565 | if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 566 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 567 | if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 568 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 569 | { |
| 570 | png_zfree(png_ptr, info_ptr->palette); |
| 571 | info_ptr->palette = NULL; |
| 572 | info_ptr->valid &= ~PNG_INFO_PLTE; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 573 | #ifndef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 574 | png_ptr->flags &= ~PNG_FLAG_FREE_PLTE; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 575 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 576 | info_ptr->num_palette = 0; |
| 577 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 578 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 579 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 580 | /* Free any image bits attached to the info structure */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 581 | #ifdef PNG_FREE_ME_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 582 | if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 583 | #else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 584 | if (mask & PNG_FREE_ROWS) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 585 | #endif |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 586 | { |
| 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 593 | info_ptr->row_pointers[row] = NULL; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 594 | } |
| 595 | png_free(png_ptr, info_ptr->row_pointers); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 596 | info_ptr->row_pointers = NULL; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 597 | } |
| 598 | info_ptr->valid &= ~PNG_INFO_IDAT; |
| 599 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 600 | #endif |
| 601 | |
| 602 | #ifdef PNG_FREE_ME_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 603 | if (num == -1) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 604 | info_ptr->free_me &= ~mask; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 605 | else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 606 | info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 607 | #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 | */ |
| 614 | void /* PRIVATE */ |
| 615 | png_info_destroy(png_structp png_ptr, png_infop info_ptr) |
| 616 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 617 | png_debug(1, "in png_info_destroy"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 618 | |
| 619 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
| 620 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 621 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 622 | if (png_ptr->num_chunk_list) |
| 623 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 624 | png_free(png_ptr, png_ptr->chunk_list); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 625 | png_ptr->chunk_list = NULL; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 626 | png_ptr->num_chunk_list = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 627 | } |
| 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 | */ |
| 638 | png_voidp PNGAPI |
| 639 | png_get_io_ptr(png_structp png_ptr) |
| 640 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 641 | if (png_ptr == NULL) |
| 642 | return (NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 643 | return (png_ptr->io_ptr); |
| 644 | } |
| 645 | |
| 646 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 647 | #ifdef PNG_STDIO_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 648 | /* 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 | */ |
| 654 | void PNGAPI |
| 655 | png_init_io(png_structp png_ptr, png_FILE_p fp) |
| 656 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 657 | png_debug(1, "in png_init_io"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 658 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 659 | if (png_ptr == NULL) |
| 660 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 661 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 662 | png_ptr->io_ptr = (png_voidp)fp; |
| 663 | } |
| 664 | #endif |
| 665 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 666 | #ifdef PNG_TIME_RFC1123_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 667 | /* Convert the supplied time into an RFC 1123 string suitable for use in |
| 668 | * a "Creation Time" or other text-based time string. |
| 669 | */ |
| 670 | png_charp PNGAPI |
| 671 | png_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 Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 677 | if (png_ptr == NULL) |
| 678 | return (NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 679 | 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 685 | #ifdef _WIN32_WCE |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 686 | { |
| 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 692 | WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, |
| 693 | 29, NULL, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 694 | } |
| 695 | #else |
| 696 | #ifdef USE_FAR_KEYWORD |
| 697 | { |
| 698 | char near_time_buf[29]; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 699 | png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 700 | 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 Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 707 | png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000", |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 708 | 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 | |
| 719 | png_charp PNGAPI |
| 720 | png_get_copyright(png_structp png_ptr) |
| 721 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 722 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 723 | #ifdef PNG_STRING_COPYRIGHT |
| 724 | return PNG_STRING_COPYRIGHT |
| 725 | #else |
| 726 | #ifdef __STDC__ |
| 727 | return ((png_charp) PNG_STRING_NEWLINE \ |
Eric Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 728 | "libpng version 1.2.46 - July 9, 2011" PNG_STRING_NEWLINE \ |
| 729 | "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 730 | "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 Vannier | 66dce0d | 2011-07-20 17:03:29 -0700 | [diff] [blame] | 734 | return ((png_charp) "libpng version 1.2.46 - July 9, 2011\ |
| 735 | Copyright (c) 1998-2011 Glenn Randers-Pehrson\ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 736 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 740 | } |
| 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 | */ |
| 750 | png_charp PNGAPI |
| 751 | png_get_libpng_ver(png_structp png_ptr) |
| 752 | { |
| 753 | /* Version of *.c files used when building libpng */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 754 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 755 | return ((png_charp) PNG_LIBPNG_VER_STRING); |
| 756 | } |
| 757 | |
| 758 | png_charp PNGAPI |
| 759 | png_get_header_ver(png_structp png_ptr) |
| 760 | { |
| 761 | /* Version of *.h files used when building libpng */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 762 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 763 | return ((png_charp) PNG_LIBPNG_VER_STRING); |
| 764 | } |
| 765 | |
| 766 | png_charp PNGAPI |
| 767 | png_get_header_version(png_structp png_ptr) |
| 768 | { |
| 769 | /* Returns longer string containing both version and date */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 770 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 771 | #ifdef __STDC__ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 772 | return ((png_charp) PNG_HEADER_VERSION_STRING |
| 773 | #ifndef PNG_READ_SUPPORTED |
| 774 | " (NO READ SUPPORT)" |
| 775 | #endif |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 776 | PNG_STRING_NEWLINE); |
| 777 | #else |
| 778 | return ((png_charp) PNG_HEADER_VERSION_STRING); |
| 779 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
| 783 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 784 | int PNGAPI |
| 785 | png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name) |
| 786 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 787 | /* Check chunk_name and return "keep" value if it's on the list, else 0 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 788 | int i; |
| 789 | png_bytep p; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 790 | if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 791 | return 0; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 792 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 794 | if (!png_memcmp(chunk_name, p, 4)) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 795 | return ((int)*(p + 4)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | /* This function, added to libpng-1.0.6g, is untested. */ |
| 801 | int PNGAPI |
| 802 | png_reset_zstream(png_structp png_ptr) |
| 803 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 804 | if (png_ptr == NULL) |
| 805 | return Z_STREAM_ERROR; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 806 | 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 */ |
| 811 | png_uint_32 PNGAPI |
| 812 | png_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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 820 | #ifndef PNG_1_0_X |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 821 | /* This function was added to libpng 1.2.0 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 822 | int PNGAPI |
| 823 | png_mmx_support(void) |
| 824 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 825 | /* Obsolete, to be removed from libpng-1.4.0 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 826 | 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)); |
| 835 | png_size_t PNGAPI |
| 836 | png_convert_size(size_t size) |
| 837 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 838 | 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 841 | } |
| 842 | #endif /* PNG_SIZE_T */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 843 | |
| 844 | /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 845 | #ifdef PNG_cHRM_SUPPORTED |
| 846 | #ifdef PNG_CHECK_cHRM_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 847 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 848 | void /* PRIVATE */ |
| 849 | png_64bit_product (long v1, long v2, unsigned long *hi_product, |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 850 | unsigned long *lo_product) |
| 851 | { |
Geremy Condra | 14cab86 | 2012-06-05 15:17:48 -0700 | [diff] [blame] | 852 | 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 Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 855 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 856 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 857 | int /* PRIVATE */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 858 | png_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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 867 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 868 | 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 926 | #endif /* PNG_CHECK_cHRM_SUPPORTED */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 927 | #endif /* PNG_cHRM_SUPPORTED */ |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 928 | |
Narayan Kamath | 1a173d9 | 2015-04-03 14:15:07 +0100 | [diff] [blame^] | 929 | #ifdef __GNUC__ |
| 930 | /* This exists solely to work round a warning from GNU C. */ |
| 931 | static int /* PRIVATE */ |
| 932 | png_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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 940 | void /* PRIVATE */ |
| 941 | png_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 Kamath | 1a173d9 | 2015-04-03 14:15:07 +0100 | [diff] [blame^] | 954 | 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 993 | |
| 994 | if (height == 0) |
| 995 | { |
| 996 | png_warning(png_ptr, "Image height is zero in IHDR"); |
| 997 | error = 1; |
| 998 | } |
Narayan Kamath | 1a173d9 | 2015-04-03 14:15:07 +0100 | [diff] [blame^] | 999 | else if (height > PNG_UINT_31_MAX) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1000 | { |
| 1001 | png_warning(png_ptr, "Invalid image height in IHDR"); |
| 1002 | error = 1; |
| 1003 | } |
Narayan Kamath | 1a173d9 | 2015-04-03 14:15:07 +0100 | [diff] [blame^] | 1004 | 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1016 | |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1097 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |