dbf-halloween2015

annotate libs/libpng/png.c @ 3:c37fe5d8a4ed

windows port
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:04:28 +0200
parents
children
rev   line source
nuclear@1 1
nuclear@1 2 /* png.c - location for general purpose libpng functions
nuclear@1 3 *
nuclear@1 4 * Last changed in libpng 1.2.30 [August 15, 2008]
nuclear@1 5 * For conditions of distribution and use, see copyright notice in png.h
nuclear@1 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
nuclear@1 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
nuclear@1 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
nuclear@1 9 */
nuclear@1 10
nuclear@1 11 #define PNG_INTERNAL
nuclear@1 12 #define PNG_NO_EXTERN
nuclear@1 13 #include "png.h"
nuclear@1 14
nuclear@1 15 /* Generate a compiler error if there is an old png.h in the search path. */
nuclear@1 16 typedef version_1_2_33 Your_png_h_is_not_version_1_2_33;
nuclear@1 17
nuclear@1 18 /* Version information for C files. This had better match the version
nuclear@1 19 * string defined in png.h. */
nuclear@1 20
nuclear@1 21 #ifdef PNG_USE_GLOBAL_ARRAYS
nuclear@1 22 /* png_libpng_ver was changed to a function in version 1.0.5c */
nuclear@1 23 PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
nuclear@1 24
nuclear@1 25 #ifdef PNG_READ_SUPPORTED
nuclear@1 26
nuclear@1 27 /* png_sig was changed to a function in version 1.0.5c */
nuclear@1 28 /* Place to hold the signature string for a PNG file. */
nuclear@1 29 PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
nuclear@1 30 #endif /* PNG_READ_SUPPORTED */
nuclear@1 31
nuclear@1 32 /* Invoke global declarations for constant strings for known chunk types */
nuclear@1 33 PNG_IHDR;
nuclear@1 34 PNG_IDAT;
nuclear@1 35 PNG_IEND;
nuclear@1 36 PNG_PLTE;
nuclear@1 37 PNG_bKGD;
nuclear@1 38 PNG_cHRM;
nuclear@1 39 PNG_gAMA;
nuclear@1 40 PNG_hIST;
nuclear@1 41 PNG_iCCP;
nuclear@1 42 PNG_iTXt;
nuclear@1 43 PNG_oFFs;
nuclear@1 44 PNG_pCAL;
nuclear@1 45 PNG_sCAL;
nuclear@1 46 PNG_pHYs;
nuclear@1 47 PNG_sBIT;
nuclear@1 48 PNG_sPLT;
nuclear@1 49 PNG_sRGB;
nuclear@1 50 PNG_tEXt;
nuclear@1 51 PNG_tIME;
nuclear@1 52 PNG_tRNS;
nuclear@1 53 PNG_zTXt;
nuclear@1 54
nuclear@1 55 #ifdef PNG_READ_SUPPORTED
nuclear@1 56 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@1 57
nuclear@1 58 /* start of interlace block */
nuclear@1 59 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
nuclear@1 60
nuclear@1 61 /* offset to next interlace block */
nuclear@1 62 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@1 63
nuclear@1 64 /* start of interlace block in the y direction */
nuclear@1 65 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
nuclear@1 66
nuclear@1 67 /* offset to next interlace block in the y direction */
nuclear@1 68 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
nuclear@1 69
nuclear@1 70 /* Height of interlace block. This is not currently used - if you need
nuclear@1 71 * it, uncomment it here and in png.h
nuclear@1 72 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@1 73 */
nuclear@1 74
nuclear@1 75 /* Mask to determine which pixels are valid in a pass */
nuclear@1 76 PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
nuclear@1 77
nuclear@1 78 /* Mask to determine which pixels to overwrite while displaying */
nuclear@1 79 PNG_CONST int FARDATA png_pass_dsp_mask[]
nuclear@1 80 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
nuclear@1 81
nuclear@1 82 #endif /* PNG_READ_SUPPORTED */
nuclear@1 83 #endif /* PNG_USE_GLOBAL_ARRAYS */
nuclear@1 84
nuclear@1 85 /* Tells libpng that we have already handled the first "num_bytes" bytes
nuclear@1 86 * of the PNG file signature. If the PNG data is embedded into another
nuclear@1 87 * stream we can set num_bytes = 8 so that libpng will not attempt to read
nuclear@1 88 * or write any of the magic bytes before it starts on the IHDR.
nuclear@1 89 */
nuclear@1 90
nuclear@1 91 #ifdef PNG_READ_SUPPORTED
nuclear@1 92 void PNGAPI
nuclear@1 93 png_set_sig_bytes(png_structp png_ptr, int num_bytes)
nuclear@1 94 {
nuclear@1 95 if (png_ptr == NULL) return;
nuclear@1 96 png_debug(1, "in png_set_sig_bytes\n");
nuclear@1 97 if (num_bytes > 8)
nuclear@1 98 png_error(png_ptr, "Too many bytes for PNG signature.");
nuclear@1 99
nuclear@1 100 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
nuclear@1 101 }
nuclear@1 102
nuclear@1 103 /* Checks whether the supplied bytes match the PNG signature. We allow
nuclear@1 104 * checking less than the full 8-byte signature so that those apps that
nuclear@1 105 * already read the first few bytes of a file to determine the file type
nuclear@1 106 * can simply check the remaining bytes for extra assurance. Returns
nuclear@1 107 * an integer less than, equal to, or greater than zero if sig is found,
nuclear@1 108 * respectively, to be less than, to match, or be greater than the correct
nuclear@1 109 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
nuclear@1 110 */
nuclear@1 111 int PNGAPI
nuclear@1 112 png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
nuclear@1 113 {
nuclear@1 114 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
nuclear@1 115 if (num_to_check > 8)
nuclear@1 116 num_to_check = 8;
nuclear@1 117 else if (num_to_check < 1)
nuclear@1 118 return (-1);
nuclear@1 119
nuclear@1 120 if (start > 7)
nuclear@1 121 return (-1);
nuclear@1 122
nuclear@1 123 if (start + num_to_check > 8)
nuclear@1 124 num_to_check = 8 - start;
nuclear@1 125
nuclear@1 126 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
nuclear@1 127 }
nuclear@1 128
nuclear@1 129 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
nuclear@1 130 /* (Obsolete) function to check signature bytes. It does not allow one
nuclear@1 131 * to check a partial signature. This function might be removed in the
nuclear@1 132 * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
nuclear@1 133 */
nuclear@1 134 int PNGAPI
nuclear@1 135 png_check_sig(png_bytep sig, int num)
nuclear@1 136 {
nuclear@1 137 return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
nuclear@1 138 }
nuclear@1 139 #endif
nuclear@1 140 #endif /* PNG_READ_SUPPORTED */
nuclear@1 141
nuclear@1 142 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@1 143 /* Function to allocate memory for zlib and clear it to 0. */
nuclear@1 144 #ifdef PNG_1_0_X
nuclear@1 145 voidpf PNGAPI
nuclear@1 146 #else
nuclear@1 147 voidpf /* private */
nuclear@1 148 #endif
nuclear@1 149 png_zalloc(voidpf png_ptr, uInt items, uInt size)
nuclear@1 150 {
nuclear@1 151 png_voidp ptr;
nuclear@1 152 png_structp p=(png_structp)png_ptr;
nuclear@1 153 png_uint_32 save_flags=p->flags;
nuclear@1 154 png_uint_32 num_bytes;
nuclear@1 155
nuclear@1 156 if (png_ptr == NULL) return (NULL);
nuclear@1 157 if (items > PNG_UINT_32_MAX/size)
nuclear@1 158 {
nuclear@1 159 png_warning (p, "Potential overflow in png_zalloc()");
nuclear@1 160 return (NULL);
nuclear@1 161 }
nuclear@1 162 num_bytes = (png_uint_32)items * size;
nuclear@1 163
nuclear@1 164 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
nuclear@1 165 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
nuclear@1 166 p->flags=save_flags;
nuclear@1 167
nuclear@1 168 #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
nuclear@1 169 if (ptr == NULL)
nuclear@1 170 return ((voidpf)ptr);
nuclear@1 171
nuclear@1 172 if (num_bytes > (png_uint_32)0x8000L)
nuclear@1 173 {
nuclear@1 174 png_memset(ptr, 0, (png_size_t)0x8000L);
nuclear@1 175 png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
nuclear@1 176 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
nuclear@1 177 }
nuclear@1 178 else
nuclear@1 179 {
nuclear@1 180 png_memset(ptr, 0, (png_size_t)num_bytes);
nuclear@1 181 }
nuclear@1 182 #endif
nuclear@1 183 return ((voidpf)ptr);
nuclear@1 184 }
nuclear@1 185
nuclear@1 186 /* function to free memory for zlib */
nuclear@1 187 #ifdef PNG_1_0_X
nuclear@1 188 void PNGAPI
nuclear@1 189 #else
nuclear@1 190 void /* private */
nuclear@1 191 #endif
nuclear@1 192 png_zfree(voidpf png_ptr, voidpf ptr)
nuclear@1 193 {
nuclear@1 194 png_free((png_structp)png_ptr, (png_voidp)ptr);
nuclear@1 195 }
nuclear@1 196
nuclear@1 197 /* Reset the CRC variable to 32 bits of 1's. Care must be taken
nuclear@1 198 * in case CRC is > 32 bits to leave the top bits 0.
nuclear@1 199 */
nuclear@1 200 void /* PRIVATE */
nuclear@1 201 png_reset_crc(png_structp png_ptr)
nuclear@1 202 {
nuclear@1 203 png_ptr->crc = crc32(0, Z_NULL, 0);
nuclear@1 204 }
nuclear@1 205
nuclear@1 206 /* Calculate the CRC over a section of data. We can only pass as
nuclear@1 207 * much data to this routine as the largest single buffer size. We
nuclear@1 208 * also check that this data will actually be used before going to the
nuclear@1 209 * trouble of calculating it.
nuclear@1 210 */
nuclear@1 211 void /* PRIVATE */
nuclear@1 212 png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
nuclear@1 213 {
nuclear@1 214 int need_crc = 1;
nuclear@1 215
nuclear@1 216 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
nuclear@1 217 {
nuclear@1 218 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
nuclear@1 219 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
nuclear@1 220 need_crc = 0;
nuclear@1 221 }
nuclear@1 222 else /* critical */
nuclear@1 223 {
nuclear@1 224 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
nuclear@1 225 need_crc = 0;
nuclear@1 226 }
nuclear@1 227
nuclear@1 228 if (need_crc)
nuclear@1 229 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
nuclear@1 230 }
nuclear@1 231
nuclear@1 232 /* Allocate the memory for an info_struct for the application. We don't
nuclear@1 233 * really need the png_ptr, but it could potentially be useful in the
nuclear@1 234 * future. This should be used in favour of malloc(png_sizeof(png_info))
nuclear@1 235 * and png_info_init() so that applications that want to use a shared
nuclear@1 236 * libpng don't have to be recompiled if png_info changes size.
nuclear@1 237 */
nuclear@1 238 png_infop PNGAPI
nuclear@1 239 png_create_info_struct(png_structp png_ptr)
nuclear@1 240 {
nuclear@1 241 png_infop info_ptr;
nuclear@1 242
nuclear@1 243 png_debug(1, "in png_create_info_struct\n");
nuclear@1 244 if (png_ptr == NULL) return (NULL);
nuclear@1 245 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@1 246 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
nuclear@1 247 png_ptr->malloc_fn, png_ptr->mem_ptr);
nuclear@1 248 #else
nuclear@1 249 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
nuclear@1 250 #endif
nuclear@1 251 if (info_ptr != NULL)
nuclear@1 252 png_info_init_3(&info_ptr, png_sizeof(png_info));
nuclear@1 253
nuclear@1 254 return (info_ptr);
nuclear@1 255 }
nuclear@1 256
nuclear@1 257 /* This function frees the memory associated with a single info struct.
nuclear@1 258 * Normally, one would use either png_destroy_read_struct() or
nuclear@1 259 * png_destroy_write_struct() to free an info struct, but this may be
nuclear@1 260 * useful for some applications.
nuclear@1 261 */
nuclear@1 262 void PNGAPI
nuclear@1 263 png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
nuclear@1 264 {
nuclear@1 265 png_infop info_ptr = NULL;
nuclear@1 266 if (png_ptr == NULL) return;
nuclear@1 267
nuclear@1 268 png_debug(1, "in png_destroy_info_struct\n");
nuclear@1 269 if (info_ptr_ptr != NULL)
nuclear@1 270 info_ptr = *info_ptr_ptr;
nuclear@1 271
nuclear@1 272 if (info_ptr != NULL)
nuclear@1 273 {
nuclear@1 274 png_info_destroy(png_ptr, info_ptr);
nuclear@1 275
nuclear@1 276 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@1 277 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
nuclear@1 278 png_ptr->mem_ptr);
nuclear@1 279 #else
nuclear@1 280 png_destroy_struct((png_voidp)info_ptr);
nuclear@1 281 #endif
nuclear@1 282 *info_ptr_ptr = NULL;
nuclear@1 283 }
nuclear@1 284 }
nuclear@1 285
nuclear@1 286 /* Initialize the info structure. This is now an internal function (0.89)
nuclear@1 287 * and applications using it are urged to use png_create_info_struct()
nuclear@1 288 * instead.
nuclear@1 289 */
nuclear@1 290 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
nuclear@1 291 #undef png_info_init
nuclear@1 292 void PNGAPI
nuclear@1 293 png_info_init(png_infop info_ptr)
nuclear@1 294 {
nuclear@1 295 /* We only come here via pre-1.0.12-compiled applications */
nuclear@1 296 png_info_init_3(&info_ptr, 0);
nuclear@1 297 }
nuclear@1 298 #endif
nuclear@1 299
nuclear@1 300 void PNGAPI
nuclear@1 301 png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
nuclear@1 302 {
nuclear@1 303 png_infop info_ptr = *ptr_ptr;
nuclear@1 304
nuclear@1 305 if (info_ptr == NULL) return;
nuclear@1 306
nuclear@1 307 png_debug(1, "in png_info_init_3\n");
nuclear@1 308
nuclear@1 309 if (png_sizeof(png_info) > png_info_struct_size)
nuclear@1 310 {
nuclear@1 311 png_destroy_struct(info_ptr);
nuclear@1 312 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
nuclear@1 313 *ptr_ptr = info_ptr;
nuclear@1 314 }
nuclear@1 315
nuclear@1 316 /* set everything to 0 */
nuclear@1 317 png_memset(info_ptr, 0, png_sizeof(png_info));
nuclear@1 318 }
nuclear@1 319
nuclear@1 320 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 321 void PNGAPI
nuclear@1 322 png_data_freer(png_structp png_ptr, png_infop info_ptr,
nuclear@1 323 int freer, png_uint_32 mask)
nuclear@1 324 {
nuclear@1 325 png_debug(1, "in png_data_freer\n");
nuclear@1 326 if (png_ptr == NULL || info_ptr == NULL)
nuclear@1 327 return;
nuclear@1 328 if (freer == PNG_DESTROY_WILL_FREE_DATA)
nuclear@1 329 info_ptr->free_me |= mask;
nuclear@1 330 else if (freer == PNG_USER_WILL_FREE_DATA)
nuclear@1 331 info_ptr->free_me &= ~mask;
nuclear@1 332 else
nuclear@1 333 png_warning(png_ptr,
nuclear@1 334 "Unknown freer parameter in png_data_freer.");
nuclear@1 335 }
nuclear@1 336 #endif
nuclear@1 337
nuclear@1 338 void PNGAPI
nuclear@1 339 png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
nuclear@1 340 int num)
nuclear@1 341 {
nuclear@1 342 png_debug(1, "in png_free_data\n");
nuclear@1 343 if (png_ptr == NULL || info_ptr == NULL)
nuclear@1 344 return;
nuclear@1 345
nuclear@1 346 #if defined(PNG_TEXT_SUPPORTED)
nuclear@1 347 /* free text item num or (if num == -1) all text items */
nuclear@1 348 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 349 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
nuclear@1 350 #else
nuclear@1 351 if (mask & PNG_FREE_TEXT)
nuclear@1 352 #endif
nuclear@1 353 {
nuclear@1 354 if (num != -1)
nuclear@1 355 {
nuclear@1 356 if (info_ptr->text && info_ptr->text[num].key)
nuclear@1 357 {
nuclear@1 358 png_free(png_ptr, info_ptr->text[num].key);
nuclear@1 359 info_ptr->text[num].key = NULL;
nuclear@1 360 }
nuclear@1 361 }
nuclear@1 362 else
nuclear@1 363 {
nuclear@1 364 int i;
nuclear@1 365 for (i = 0; i < info_ptr->num_text; i++)
nuclear@1 366 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
nuclear@1 367 png_free(png_ptr, info_ptr->text);
nuclear@1 368 info_ptr->text = NULL;
nuclear@1 369 info_ptr->num_text=0;
nuclear@1 370 }
nuclear@1 371 }
nuclear@1 372 #endif
nuclear@1 373
nuclear@1 374 #if defined(PNG_tRNS_SUPPORTED)
nuclear@1 375 /* free any tRNS entry */
nuclear@1 376 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 377 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
nuclear@1 378 #else
nuclear@1 379 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
nuclear@1 380 #endif
nuclear@1 381 {
nuclear@1 382 png_free(png_ptr, info_ptr->trans);
nuclear@1 383 info_ptr->trans = NULL;
nuclear@1 384 info_ptr->valid &= ~PNG_INFO_tRNS;
nuclear@1 385 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@1 386 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
nuclear@1 387 #endif
nuclear@1 388 }
nuclear@1 389 #endif
nuclear@1 390
nuclear@1 391 #if defined(PNG_sCAL_SUPPORTED)
nuclear@1 392 /* free any sCAL entry */
nuclear@1 393 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 394 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
nuclear@1 395 #else
nuclear@1 396 if (mask & PNG_FREE_SCAL)
nuclear@1 397 #endif
nuclear@1 398 {
nuclear@1 399 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
nuclear@1 400 png_free(png_ptr, info_ptr->scal_s_width);
nuclear@1 401 png_free(png_ptr, info_ptr->scal_s_height);
nuclear@1 402 info_ptr->scal_s_width = NULL;
nuclear@1 403 info_ptr->scal_s_height = NULL;
nuclear@1 404 #endif
nuclear@1 405 info_ptr->valid &= ~PNG_INFO_sCAL;
nuclear@1 406 }
nuclear@1 407 #endif
nuclear@1 408
nuclear@1 409 #if defined(PNG_pCAL_SUPPORTED)
nuclear@1 410 /* free any pCAL entry */
nuclear@1 411 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 412 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
nuclear@1 413 #else
nuclear@1 414 if (mask & PNG_FREE_PCAL)
nuclear@1 415 #endif
nuclear@1 416 {
nuclear@1 417 png_free(png_ptr, info_ptr->pcal_purpose);
nuclear@1 418 png_free(png_ptr, info_ptr->pcal_units);
nuclear@1 419 info_ptr->pcal_purpose = NULL;
nuclear@1 420 info_ptr->pcal_units = NULL;
nuclear@1 421 if (info_ptr->pcal_params != NULL)
nuclear@1 422 {
nuclear@1 423 int i;
nuclear@1 424 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
nuclear@1 425 {
nuclear@1 426 png_free(png_ptr, info_ptr->pcal_params[i]);
nuclear@1 427 info_ptr->pcal_params[i]=NULL;
nuclear@1 428 }
nuclear@1 429 png_free(png_ptr, info_ptr->pcal_params);
nuclear@1 430 info_ptr->pcal_params = NULL;
nuclear@1 431 }
nuclear@1 432 info_ptr->valid &= ~PNG_INFO_pCAL;
nuclear@1 433 }
nuclear@1 434 #endif
nuclear@1 435
nuclear@1 436 #if defined(PNG_iCCP_SUPPORTED)
nuclear@1 437 /* free any iCCP entry */
nuclear@1 438 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 439 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
nuclear@1 440 #else
nuclear@1 441 if (mask & PNG_FREE_ICCP)
nuclear@1 442 #endif
nuclear@1 443 {
nuclear@1 444 png_free(png_ptr, info_ptr->iccp_name);
nuclear@1 445 png_free(png_ptr, info_ptr->iccp_profile);
nuclear@1 446 info_ptr->iccp_name = NULL;
nuclear@1 447 info_ptr->iccp_profile = NULL;
nuclear@1 448 info_ptr->valid &= ~PNG_INFO_iCCP;
nuclear@1 449 }
nuclear@1 450 #endif
nuclear@1 451
nuclear@1 452 #if defined(PNG_sPLT_SUPPORTED)
nuclear@1 453 /* free a given sPLT entry, or (if num == -1) all sPLT entries */
nuclear@1 454 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 455 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
nuclear@1 456 #else
nuclear@1 457 if (mask & PNG_FREE_SPLT)
nuclear@1 458 #endif
nuclear@1 459 {
nuclear@1 460 if (num != -1)
nuclear@1 461 {
nuclear@1 462 if (info_ptr->splt_palettes)
nuclear@1 463 {
nuclear@1 464 png_free(png_ptr, info_ptr->splt_palettes[num].name);
nuclear@1 465 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
nuclear@1 466 info_ptr->splt_palettes[num].name = NULL;
nuclear@1 467 info_ptr->splt_palettes[num].entries = NULL;
nuclear@1 468 }
nuclear@1 469 }
nuclear@1 470 else
nuclear@1 471 {
nuclear@1 472 if (info_ptr->splt_palettes_num)
nuclear@1 473 {
nuclear@1 474 int i;
nuclear@1 475 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
nuclear@1 476 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
nuclear@1 477
nuclear@1 478 png_free(png_ptr, info_ptr->splt_palettes);
nuclear@1 479 info_ptr->splt_palettes = NULL;
nuclear@1 480 info_ptr->splt_palettes_num = 0;
nuclear@1 481 }
nuclear@1 482 info_ptr->valid &= ~PNG_INFO_sPLT;
nuclear@1 483 }
nuclear@1 484 }
nuclear@1 485 #endif
nuclear@1 486
nuclear@1 487 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@1 488 if (png_ptr->unknown_chunk.data)
nuclear@1 489 {
nuclear@1 490 png_free(png_ptr, png_ptr->unknown_chunk.data);
nuclear@1 491 png_ptr->unknown_chunk.data = NULL;
nuclear@1 492 }
nuclear@1 493
nuclear@1 494 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 495 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
nuclear@1 496 #else
nuclear@1 497 if (mask & PNG_FREE_UNKN)
nuclear@1 498 #endif
nuclear@1 499 {
nuclear@1 500 if (num != -1)
nuclear@1 501 {
nuclear@1 502 if (info_ptr->unknown_chunks)
nuclear@1 503 {
nuclear@1 504 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
nuclear@1 505 info_ptr->unknown_chunks[num].data = NULL;
nuclear@1 506 }
nuclear@1 507 }
nuclear@1 508 else
nuclear@1 509 {
nuclear@1 510 int i;
nuclear@1 511
nuclear@1 512 if (info_ptr->unknown_chunks_num)
nuclear@1 513 {
nuclear@1 514 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
nuclear@1 515 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
nuclear@1 516
nuclear@1 517 png_free(png_ptr, info_ptr->unknown_chunks);
nuclear@1 518 info_ptr->unknown_chunks = NULL;
nuclear@1 519 info_ptr->unknown_chunks_num = 0;
nuclear@1 520 }
nuclear@1 521 }
nuclear@1 522 }
nuclear@1 523 #endif
nuclear@1 524
nuclear@1 525 #if defined(PNG_hIST_SUPPORTED)
nuclear@1 526 /* free any hIST entry */
nuclear@1 527 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 528 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
nuclear@1 529 #else
nuclear@1 530 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
nuclear@1 531 #endif
nuclear@1 532 {
nuclear@1 533 png_free(png_ptr, info_ptr->hist);
nuclear@1 534 info_ptr->hist = NULL;
nuclear@1 535 info_ptr->valid &= ~PNG_INFO_hIST;
nuclear@1 536 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@1 537 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
nuclear@1 538 #endif
nuclear@1 539 }
nuclear@1 540 #endif
nuclear@1 541
nuclear@1 542 /* free any PLTE entry that was internally allocated */
nuclear@1 543 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 544 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
nuclear@1 545 #else
nuclear@1 546 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
nuclear@1 547 #endif
nuclear@1 548 {
nuclear@1 549 png_zfree(png_ptr, info_ptr->palette);
nuclear@1 550 info_ptr->palette = NULL;
nuclear@1 551 info_ptr->valid &= ~PNG_INFO_PLTE;
nuclear@1 552 #ifndef PNG_FREE_ME_SUPPORTED
nuclear@1 553 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
nuclear@1 554 #endif
nuclear@1 555 info_ptr->num_palette = 0;
nuclear@1 556 }
nuclear@1 557
nuclear@1 558 #if defined(PNG_INFO_IMAGE_SUPPORTED)
nuclear@1 559 /* free any image bits attached to the info structure */
nuclear@1 560 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 561 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
nuclear@1 562 #else
nuclear@1 563 if (mask & PNG_FREE_ROWS)
nuclear@1 564 #endif
nuclear@1 565 {
nuclear@1 566 if (info_ptr->row_pointers)
nuclear@1 567 {
nuclear@1 568 int row;
nuclear@1 569 for (row = 0; row < (int)info_ptr->height; row++)
nuclear@1 570 {
nuclear@1 571 png_free(png_ptr, info_ptr->row_pointers[row]);
nuclear@1 572 info_ptr->row_pointers[row]=NULL;
nuclear@1 573 }
nuclear@1 574 png_free(png_ptr, info_ptr->row_pointers);
nuclear@1 575 info_ptr->row_pointers=NULL;
nuclear@1 576 }
nuclear@1 577 info_ptr->valid &= ~PNG_INFO_IDAT;
nuclear@1 578 }
nuclear@1 579 #endif
nuclear@1 580
nuclear@1 581 #ifdef PNG_FREE_ME_SUPPORTED
nuclear@1 582 if (num == -1)
nuclear@1 583 info_ptr->free_me &= ~mask;
nuclear@1 584 else
nuclear@1 585 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
nuclear@1 586 #endif
nuclear@1 587 }
nuclear@1 588
nuclear@1 589 /* This is an internal routine to free any memory that the info struct is
nuclear@1 590 * pointing to before re-using it or freeing the struct itself. Recall
nuclear@1 591 * that png_free() checks for NULL pointers for us.
nuclear@1 592 */
nuclear@1 593 void /* PRIVATE */
nuclear@1 594 png_info_destroy(png_structp png_ptr, png_infop info_ptr)
nuclear@1 595 {
nuclear@1 596 png_debug(1, "in png_info_destroy\n");
nuclear@1 597
nuclear@1 598 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
nuclear@1 599
nuclear@1 600 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@1 601 if (png_ptr->num_chunk_list)
nuclear@1 602 {
nuclear@1 603 png_free(png_ptr, png_ptr->chunk_list);
nuclear@1 604 png_ptr->chunk_list=NULL;
nuclear@1 605 png_ptr->num_chunk_list = 0;
nuclear@1 606 }
nuclear@1 607 #endif
nuclear@1 608
nuclear@1 609 png_info_init_3(&info_ptr, png_sizeof(png_info));
nuclear@1 610 }
nuclear@1 611 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@1 612
nuclear@1 613 /* This function returns a pointer to the io_ptr associated with the user
nuclear@1 614 * functions. The application should free any memory associated with this
nuclear@1 615 * pointer before png_write_destroy() or png_read_destroy() are called.
nuclear@1 616 */
nuclear@1 617 png_voidp PNGAPI
nuclear@1 618 png_get_io_ptr(png_structp png_ptr)
nuclear@1 619 {
nuclear@1 620 if (png_ptr == NULL) return (NULL);
nuclear@1 621 return (png_ptr->io_ptr);
nuclear@1 622 }
nuclear@1 623
nuclear@1 624 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@1 625 #if !defined(PNG_NO_STDIO)
nuclear@1 626 /* Initialize the default input/output functions for the PNG file. If you
nuclear@1 627 * use your own read or write routines, you can call either png_set_read_fn()
nuclear@1 628 * or png_set_write_fn() instead of png_init_io(). If you have defined
nuclear@1 629 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
nuclear@1 630 * necessarily available.
nuclear@1 631 */
nuclear@1 632 void PNGAPI
nuclear@1 633 png_init_io(png_structp png_ptr, png_FILE_p fp)
nuclear@1 634 {
nuclear@1 635 png_debug(1, "in png_init_io\n");
nuclear@1 636 if (png_ptr == NULL) return;
nuclear@1 637 png_ptr->io_ptr = (png_voidp)fp;
nuclear@1 638 }
nuclear@1 639 #endif
nuclear@1 640
nuclear@1 641 #if defined(PNG_TIME_RFC1123_SUPPORTED)
nuclear@1 642 /* Convert the supplied time into an RFC 1123 string suitable for use in
nuclear@1 643 * a "Creation Time" or other text-based time string.
nuclear@1 644 */
nuclear@1 645 png_charp PNGAPI
nuclear@1 646 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
nuclear@1 647 {
nuclear@1 648 static PNG_CONST char short_months[12][4] =
nuclear@1 649 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
nuclear@1 650 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
nuclear@1 651
nuclear@1 652 if (png_ptr == NULL) return (NULL);
nuclear@1 653 if (png_ptr->time_buffer == NULL)
nuclear@1 654 {
nuclear@1 655 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
nuclear@1 656 png_sizeof(char)));
nuclear@1 657 }
nuclear@1 658
nuclear@1 659 #if defined(_WIN32_WCE)
nuclear@1 660 {
nuclear@1 661 wchar_t time_buf[29];
nuclear@1 662 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
nuclear@1 663 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@1 664 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@1 665 ptime->second % 61);
nuclear@1 666 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
nuclear@1 667 NULL, NULL);
nuclear@1 668 }
nuclear@1 669 #else
nuclear@1 670 #ifdef USE_FAR_KEYWORD
nuclear@1 671 {
nuclear@1 672 char near_time_buf[29];
nuclear@1 673 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
nuclear@1 674 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@1 675 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@1 676 ptime->second % 61);
nuclear@1 677 png_memcpy(png_ptr->time_buffer, near_time_buf,
nuclear@1 678 29*png_sizeof(char));
nuclear@1 679 }
nuclear@1 680 #else
nuclear@1 681 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
nuclear@1 682 ptime->day % 32, short_months[(ptime->month - 1) % 12],
nuclear@1 683 ptime->year, ptime->hour % 24, ptime->minute % 60,
nuclear@1 684 ptime->second % 61);
nuclear@1 685 #endif
nuclear@1 686 #endif /* _WIN32_WCE */
nuclear@1 687 return ((png_charp)png_ptr->time_buffer);
nuclear@1 688 }
nuclear@1 689 #endif /* PNG_TIME_RFC1123_SUPPORTED */
nuclear@1 690
nuclear@1 691 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@1 692
nuclear@1 693 png_charp PNGAPI
nuclear@1 694 png_get_copyright(png_structp png_ptr)
nuclear@1 695 {
nuclear@1 696 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@1 697 return ((png_charp) "\n libpng version 1.2.33 - October 31, 2008\n\
nuclear@1 698 Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
nuclear@1 699 Copyright (c) 1996-1997 Andreas Dilger\n\
nuclear@1 700 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
nuclear@1 701 }
nuclear@1 702
nuclear@1 703 /* The following return the library version as a short string in the
nuclear@1 704 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
nuclear@1 705 * used with your application, print out PNG_LIBPNG_VER_STRING, which
nuclear@1 706 * is defined in png.h.
nuclear@1 707 * Note: now there is no difference between png_get_libpng_ver() and
nuclear@1 708 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
nuclear@1 709 * it is guaranteed that png.c uses the correct version of png.h.
nuclear@1 710 */
nuclear@1 711 png_charp PNGAPI
nuclear@1 712 png_get_libpng_ver(png_structp png_ptr)
nuclear@1 713 {
nuclear@1 714 /* Version of *.c files used when building libpng */
nuclear@1 715 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@1 716 return ((png_charp) PNG_LIBPNG_VER_STRING);
nuclear@1 717 }
nuclear@1 718
nuclear@1 719 png_charp PNGAPI
nuclear@1 720 png_get_header_ver(png_structp png_ptr)
nuclear@1 721 {
nuclear@1 722 /* Version of *.h files used when building libpng */
nuclear@1 723 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@1 724 return ((png_charp) PNG_LIBPNG_VER_STRING);
nuclear@1 725 }
nuclear@1 726
nuclear@1 727 png_charp PNGAPI
nuclear@1 728 png_get_header_version(png_structp png_ptr)
nuclear@1 729 {
nuclear@1 730 /* Returns longer string containing both version and date */
nuclear@1 731 png_ptr = png_ptr; /* silence compiler warning about unused png_ptr */
nuclear@1 732 return ((png_charp) PNG_HEADER_VERSION_STRING
nuclear@1 733 #ifndef PNG_READ_SUPPORTED
nuclear@1 734 " (NO READ SUPPORT)"
nuclear@1 735 #endif
nuclear@1 736 "\n");
nuclear@1 737 }
nuclear@1 738
nuclear@1 739 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@1 740 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
nuclear@1 741 int PNGAPI
nuclear@1 742 png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
nuclear@1 743 {
nuclear@1 744 /* check chunk_name and return "keep" value if it's on the list, else 0 */
nuclear@1 745 int i;
nuclear@1 746 png_bytep p;
nuclear@1 747 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
nuclear@1 748 return 0;
nuclear@1 749 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
nuclear@1 750 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
nuclear@1 751 if (!png_memcmp(chunk_name, p, 4))
nuclear@1 752 return ((int)*(p + 4));
nuclear@1 753 return 0;
nuclear@1 754 }
nuclear@1 755 #endif
nuclear@1 756
nuclear@1 757 /* This function, added to libpng-1.0.6g, is untested. */
nuclear@1 758 int PNGAPI
nuclear@1 759 png_reset_zstream(png_structp png_ptr)
nuclear@1 760 {
nuclear@1 761 if (png_ptr == NULL) return Z_STREAM_ERROR;
nuclear@1 762 return (inflateReset(&png_ptr->zstream));
nuclear@1 763 }
nuclear@1 764 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
nuclear@1 765
nuclear@1 766 /* This function was added to libpng-1.0.7 */
nuclear@1 767 png_uint_32 PNGAPI
nuclear@1 768 png_access_version_number(void)
nuclear@1 769 {
nuclear@1 770 /* Version of *.c files used when building libpng */
nuclear@1 771 return((png_uint_32) PNG_LIBPNG_VER);
nuclear@1 772 }
nuclear@1 773
nuclear@1 774
nuclear@1 775 #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
nuclear@1 776 #if !defined(PNG_1_0_X)
nuclear@1 777 /* this function was added to libpng 1.2.0 */
nuclear@1 778 int PNGAPI
nuclear@1 779 png_mmx_support(void)
nuclear@1 780 {
nuclear@1 781 /* obsolete, to be removed from libpng-1.4.0 */
nuclear@1 782 return -1;
nuclear@1 783 }
nuclear@1 784 #endif /* PNG_1_0_X */
nuclear@1 785 #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
nuclear@1 786
nuclear@1 787 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@1 788 #ifdef PNG_SIZE_T
nuclear@1 789 /* Added at libpng version 1.2.6 */
nuclear@1 790 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
nuclear@1 791 png_size_t PNGAPI
nuclear@1 792 png_convert_size(size_t size)
nuclear@1 793 {
nuclear@1 794 if (size > (png_size_t)-1)
nuclear@1 795 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
nuclear@1 796 return ((png_size_t)size);
nuclear@1 797 }
nuclear@1 798 #endif /* PNG_SIZE_T */
nuclear@1 799 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */