istereo2

annotate libs/libpng/pngmem.c @ 20:2b85d05df3f2

animation controls for easier screenshot grabbing
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 Oct 2015 04:54:55 +0300
parents
children
rev   line source
nuclear@2 1
nuclear@2 2 /* pngmem.c - stub functions for memory allocation
nuclear@2 3 *
nuclear@2 4 * Last changed in libpng 1.2.30 [August 15, 2008]
nuclear@2 5 * For conditions of distribution and use, see copyright notice in png.h
nuclear@2 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
nuclear@2 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
nuclear@2 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
nuclear@2 9 *
nuclear@2 10 * This file provides a location for all memory allocation. Users who
nuclear@2 11 * need special memory handling are expected to supply replacement
nuclear@2 12 * functions for png_malloc() and png_free(), and to use
nuclear@2 13 * png_create_read_struct_2() and png_create_write_struct_2() to
nuclear@2 14 * identify the replacement functions.
nuclear@2 15 */
nuclear@2 16
nuclear@2 17 #define PNG_INTERNAL
nuclear@2 18 #include "png.h"
nuclear@2 19 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
nuclear@2 20
nuclear@2 21 /* Borland DOS special memory handler */
nuclear@2 22 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
nuclear@2 23 /* if you change this, be sure to change the one in png.h also */
nuclear@2 24
nuclear@2 25 /* Allocate memory for a png_struct. The malloc and memset can be replaced
nuclear@2 26 by a single call to calloc() if this is thought to improve performance. */
nuclear@2 27 png_voidp /* PRIVATE */
nuclear@2 28 png_create_struct(int type)
nuclear@2 29 {
nuclear@2 30 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 31 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
nuclear@2 32 }
nuclear@2 33
nuclear@2 34 /* Alternate version of png_create_struct, for use with user-defined malloc. */
nuclear@2 35 png_voidp /* PRIVATE */
nuclear@2 36 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
nuclear@2 37 {
nuclear@2 38 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 39 png_size_t size;
nuclear@2 40 png_voidp struct_ptr;
nuclear@2 41
nuclear@2 42 if (type == PNG_STRUCT_INFO)
nuclear@2 43 size = png_sizeof(png_info);
nuclear@2 44 else if (type == PNG_STRUCT_PNG)
nuclear@2 45 size = png_sizeof(png_struct);
nuclear@2 46 else
nuclear@2 47 return (png_get_copyright(NULL));
nuclear@2 48
nuclear@2 49 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 50 if (malloc_fn != NULL)
nuclear@2 51 {
nuclear@2 52 png_struct dummy_struct;
nuclear@2 53 png_structp png_ptr = &dummy_struct;
nuclear@2 54 png_ptr->mem_ptr=mem_ptr;
nuclear@2 55 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
nuclear@2 56 }
nuclear@2 57 else
nuclear@2 58 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 59 struct_ptr = (png_voidp)farmalloc(size);
nuclear@2 60 if (struct_ptr != NULL)
nuclear@2 61 png_memset(struct_ptr, 0, size);
nuclear@2 62 return (struct_ptr);
nuclear@2 63 }
nuclear@2 64
nuclear@2 65 /* Free memory allocated by a png_create_struct() call */
nuclear@2 66 void /* PRIVATE */
nuclear@2 67 png_destroy_struct(png_voidp struct_ptr)
nuclear@2 68 {
nuclear@2 69 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 70 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
nuclear@2 71 }
nuclear@2 72
nuclear@2 73 /* Free memory allocated by a png_create_struct() call */
nuclear@2 74 void /* PRIVATE */
nuclear@2 75 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
nuclear@2 76 png_voidp mem_ptr)
nuclear@2 77 {
nuclear@2 78 #endif
nuclear@2 79 if (struct_ptr != NULL)
nuclear@2 80 {
nuclear@2 81 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 82 if (free_fn != NULL)
nuclear@2 83 {
nuclear@2 84 png_struct dummy_struct;
nuclear@2 85 png_structp png_ptr = &dummy_struct;
nuclear@2 86 png_ptr->mem_ptr=mem_ptr;
nuclear@2 87 (*(free_fn))(png_ptr, struct_ptr);
nuclear@2 88 return;
nuclear@2 89 }
nuclear@2 90 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 91 farfree (struct_ptr);
nuclear@2 92 }
nuclear@2 93 }
nuclear@2 94
nuclear@2 95 /* Allocate memory. For reasonable files, size should never exceed
nuclear@2 96 * 64K. However, zlib may allocate more then 64K if you don't tell
nuclear@2 97 * it not to. See zconf.h and png.h for more information. zlib does
nuclear@2 98 * need to allocate exactly 64K, so whatever you call here must
nuclear@2 99 * have the ability to do that.
nuclear@2 100 *
nuclear@2 101 * Borland seems to have a problem in DOS mode for exactly 64K.
nuclear@2 102 * It gives you a segment with an offset of 8 (perhaps to store its
nuclear@2 103 * memory stuff). zlib doesn't like this at all, so we have to
nuclear@2 104 * detect and deal with it. This code should not be needed in
nuclear@2 105 * Windows or OS/2 modes, and only in 16 bit mode. This code has
nuclear@2 106 * been updated by Alexander Lehmann for version 0.89 to waste less
nuclear@2 107 * memory.
nuclear@2 108 *
nuclear@2 109 * Note that we can't use png_size_t for the "size" declaration,
nuclear@2 110 * since on some systems a png_size_t is a 16-bit quantity, and as a
nuclear@2 111 * result, we would be truncating potentially larger memory requests
nuclear@2 112 * (which should cause a fatal error) and introducing major problems.
nuclear@2 113 */
nuclear@2 114
nuclear@2 115 png_voidp PNGAPI
nuclear@2 116 png_malloc(png_structp png_ptr, png_uint_32 size)
nuclear@2 117 {
nuclear@2 118 png_voidp ret;
nuclear@2 119
nuclear@2 120 if (png_ptr == NULL || size == 0)
nuclear@2 121 return (NULL);
nuclear@2 122
nuclear@2 123 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 124 if (png_ptr->malloc_fn != NULL)
nuclear@2 125 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
nuclear@2 126 else
nuclear@2 127 ret = (png_malloc_default(png_ptr, size));
nuclear@2 128 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 129 png_error(png_ptr, "Out of memory!");
nuclear@2 130 return (ret);
nuclear@2 131 }
nuclear@2 132
nuclear@2 133 png_voidp PNGAPI
nuclear@2 134 png_malloc_default(png_structp png_ptr, png_uint_32 size)
nuclear@2 135 {
nuclear@2 136 png_voidp ret;
nuclear@2 137 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 138
nuclear@2 139 if (png_ptr == NULL || size == 0)
nuclear@2 140 return (NULL);
nuclear@2 141
nuclear@2 142 #ifdef PNG_MAX_MALLOC_64K
nuclear@2 143 if (size > (png_uint_32)65536L)
nuclear@2 144 {
nuclear@2 145 png_warning(png_ptr, "Cannot Allocate > 64K");
nuclear@2 146 ret = NULL;
nuclear@2 147 }
nuclear@2 148 else
nuclear@2 149 #endif
nuclear@2 150
nuclear@2 151 if (size != (size_t)size)
nuclear@2 152 ret = NULL;
nuclear@2 153 else if (size == (png_uint_32)65536L)
nuclear@2 154 {
nuclear@2 155 if (png_ptr->offset_table == NULL)
nuclear@2 156 {
nuclear@2 157 /* try to see if we need to do any of this fancy stuff */
nuclear@2 158 ret = farmalloc(size);
nuclear@2 159 if (ret == NULL || ((png_size_t)ret & 0xffff))
nuclear@2 160 {
nuclear@2 161 int num_blocks;
nuclear@2 162 png_uint_32 total_size;
nuclear@2 163 png_bytep table;
nuclear@2 164 int i;
nuclear@2 165 png_byte huge * hptr;
nuclear@2 166
nuclear@2 167 if (ret != NULL)
nuclear@2 168 {
nuclear@2 169 farfree(ret);
nuclear@2 170 ret = NULL;
nuclear@2 171 }
nuclear@2 172
nuclear@2 173 if (png_ptr->zlib_window_bits > 14)
nuclear@2 174 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
nuclear@2 175 else
nuclear@2 176 num_blocks = 1;
nuclear@2 177 if (png_ptr->zlib_mem_level >= 7)
nuclear@2 178 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
nuclear@2 179 else
nuclear@2 180 num_blocks++;
nuclear@2 181
nuclear@2 182 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
nuclear@2 183
nuclear@2 184 table = farmalloc(total_size);
nuclear@2 185
nuclear@2 186 if (table == NULL)
nuclear@2 187 {
nuclear@2 188 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 189 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 190 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
nuclear@2 191 else
nuclear@2 192 png_warning(png_ptr, "Out Of Memory.");
nuclear@2 193 #endif
nuclear@2 194 return (NULL);
nuclear@2 195 }
nuclear@2 196
nuclear@2 197 if ((png_size_t)table & 0xfff0)
nuclear@2 198 {
nuclear@2 199 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 200 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 201 png_error(png_ptr,
nuclear@2 202 "Farmalloc didn't return normalized pointer");
nuclear@2 203 else
nuclear@2 204 png_warning(png_ptr,
nuclear@2 205 "Farmalloc didn't return normalized pointer");
nuclear@2 206 #endif
nuclear@2 207 return (NULL);
nuclear@2 208 }
nuclear@2 209
nuclear@2 210 png_ptr->offset_table = table;
nuclear@2 211 png_ptr->offset_table_ptr = farmalloc(num_blocks *
nuclear@2 212 png_sizeof(png_bytep));
nuclear@2 213
nuclear@2 214 if (png_ptr->offset_table_ptr == NULL)
nuclear@2 215 {
nuclear@2 216 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 217 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 218 png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
nuclear@2 219 else
nuclear@2 220 png_warning(png_ptr, "Out Of memory.");
nuclear@2 221 #endif
nuclear@2 222 return (NULL);
nuclear@2 223 }
nuclear@2 224
nuclear@2 225 hptr = (png_byte huge *)table;
nuclear@2 226 if ((png_size_t)hptr & 0xf)
nuclear@2 227 {
nuclear@2 228 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
nuclear@2 229 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
nuclear@2 230 }
nuclear@2 231 for (i = 0; i < num_blocks; i++)
nuclear@2 232 {
nuclear@2 233 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
nuclear@2 234 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
nuclear@2 235 }
nuclear@2 236
nuclear@2 237 png_ptr->offset_table_number = num_blocks;
nuclear@2 238 png_ptr->offset_table_count = 0;
nuclear@2 239 png_ptr->offset_table_count_free = 0;
nuclear@2 240 }
nuclear@2 241 }
nuclear@2 242
nuclear@2 243 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
nuclear@2 244 {
nuclear@2 245 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 246 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 247 png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
nuclear@2 248 else
nuclear@2 249 png_warning(png_ptr, "Out of Memory.");
nuclear@2 250 #endif
nuclear@2 251 return (NULL);
nuclear@2 252 }
nuclear@2 253
nuclear@2 254 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
nuclear@2 255 }
nuclear@2 256 else
nuclear@2 257 ret = farmalloc(size);
nuclear@2 258
nuclear@2 259 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 260 if (ret == NULL)
nuclear@2 261 {
nuclear@2 262 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 263 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
nuclear@2 264 else
nuclear@2 265 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
nuclear@2 266 }
nuclear@2 267 #endif
nuclear@2 268
nuclear@2 269 return (ret);
nuclear@2 270 }
nuclear@2 271
nuclear@2 272 /* free a pointer allocated by png_malloc(). In the default
nuclear@2 273 configuration, png_ptr is not used, but is passed in case it
nuclear@2 274 is needed. If ptr is NULL, return without taking any action. */
nuclear@2 275
nuclear@2 276 void PNGAPI
nuclear@2 277 png_free(png_structp png_ptr, png_voidp ptr)
nuclear@2 278 {
nuclear@2 279 if (png_ptr == NULL || ptr == NULL)
nuclear@2 280 return;
nuclear@2 281
nuclear@2 282 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 283 if (png_ptr->free_fn != NULL)
nuclear@2 284 {
nuclear@2 285 (*(png_ptr->free_fn))(png_ptr, ptr);
nuclear@2 286 return;
nuclear@2 287 }
nuclear@2 288 else png_free_default(png_ptr, ptr);
nuclear@2 289 }
nuclear@2 290
nuclear@2 291 void PNGAPI
nuclear@2 292 png_free_default(png_structp png_ptr, png_voidp ptr)
nuclear@2 293 {
nuclear@2 294 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 295
nuclear@2 296 if (png_ptr == NULL || ptr == NULL) return;
nuclear@2 297
nuclear@2 298 if (png_ptr->offset_table != NULL)
nuclear@2 299 {
nuclear@2 300 int i;
nuclear@2 301
nuclear@2 302 for (i = 0; i < png_ptr->offset_table_count; i++)
nuclear@2 303 {
nuclear@2 304 if (ptr == png_ptr->offset_table_ptr[i])
nuclear@2 305 {
nuclear@2 306 ptr = NULL;
nuclear@2 307 png_ptr->offset_table_count_free++;
nuclear@2 308 break;
nuclear@2 309 }
nuclear@2 310 }
nuclear@2 311 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
nuclear@2 312 {
nuclear@2 313 farfree(png_ptr->offset_table);
nuclear@2 314 farfree(png_ptr->offset_table_ptr);
nuclear@2 315 png_ptr->offset_table = NULL;
nuclear@2 316 png_ptr->offset_table_ptr = NULL;
nuclear@2 317 }
nuclear@2 318 }
nuclear@2 319
nuclear@2 320 if (ptr != NULL)
nuclear@2 321 {
nuclear@2 322 farfree(ptr);
nuclear@2 323 }
nuclear@2 324 }
nuclear@2 325
nuclear@2 326 #else /* Not the Borland DOS special memory handler */
nuclear@2 327
nuclear@2 328 /* Allocate memory for a png_struct or a png_info. The malloc and
nuclear@2 329 memset can be replaced by a single call to calloc() if this is thought
nuclear@2 330 to improve performance noticably. */
nuclear@2 331 png_voidp /* PRIVATE */
nuclear@2 332 png_create_struct(int type)
nuclear@2 333 {
nuclear@2 334 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 335 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
nuclear@2 336 }
nuclear@2 337
nuclear@2 338 /* Allocate memory for a png_struct or a png_info. The malloc and
nuclear@2 339 memset can be replaced by a single call to calloc() if this is thought
nuclear@2 340 to improve performance noticably. */
nuclear@2 341 png_voidp /* PRIVATE */
nuclear@2 342 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
nuclear@2 343 {
nuclear@2 344 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 345 png_size_t size;
nuclear@2 346 png_voidp struct_ptr;
nuclear@2 347
nuclear@2 348 if (type == PNG_STRUCT_INFO)
nuclear@2 349 size = png_sizeof(png_info);
nuclear@2 350 else if (type == PNG_STRUCT_PNG)
nuclear@2 351 size = png_sizeof(png_struct);
nuclear@2 352 else
nuclear@2 353 return (NULL);
nuclear@2 354
nuclear@2 355 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 356 if (malloc_fn != NULL)
nuclear@2 357 {
nuclear@2 358 png_struct dummy_struct;
nuclear@2 359 png_structp png_ptr = &dummy_struct;
nuclear@2 360 png_ptr->mem_ptr=mem_ptr;
nuclear@2 361 struct_ptr = (*(malloc_fn))(png_ptr, size);
nuclear@2 362 if (struct_ptr != NULL)
nuclear@2 363 png_memset(struct_ptr, 0, size);
nuclear@2 364 return (struct_ptr);
nuclear@2 365 }
nuclear@2 366 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 367
nuclear@2 368 #if defined(__TURBOC__) && !defined(__FLAT__)
nuclear@2 369 struct_ptr = (png_voidp)farmalloc(size);
nuclear@2 370 #else
nuclear@2 371 # if defined(_MSC_VER) && defined(MAXSEG_64K)
nuclear@2 372 struct_ptr = (png_voidp)halloc(size, 1);
nuclear@2 373 # else
nuclear@2 374 struct_ptr = (png_voidp)malloc(size);
nuclear@2 375 # endif
nuclear@2 376 #endif
nuclear@2 377 if (struct_ptr != NULL)
nuclear@2 378 png_memset(struct_ptr, 0, size);
nuclear@2 379
nuclear@2 380 return (struct_ptr);
nuclear@2 381 }
nuclear@2 382
nuclear@2 383
nuclear@2 384 /* Free memory allocated by a png_create_struct() call */
nuclear@2 385 void /* PRIVATE */
nuclear@2 386 png_destroy_struct(png_voidp struct_ptr)
nuclear@2 387 {
nuclear@2 388 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 389 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
nuclear@2 390 }
nuclear@2 391
nuclear@2 392 /* Free memory allocated by a png_create_struct() call */
nuclear@2 393 void /* PRIVATE */
nuclear@2 394 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
nuclear@2 395 png_voidp mem_ptr)
nuclear@2 396 {
nuclear@2 397 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 398 if (struct_ptr != NULL)
nuclear@2 399 {
nuclear@2 400 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 401 if (free_fn != NULL)
nuclear@2 402 {
nuclear@2 403 png_struct dummy_struct;
nuclear@2 404 png_structp png_ptr = &dummy_struct;
nuclear@2 405 png_ptr->mem_ptr=mem_ptr;
nuclear@2 406 (*(free_fn))(png_ptr, struct_ptr);
nuclear@2 407 return;
nuclear@2 408 }
nuclear@2 409 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 410 #if defined(__TURBOC__) && !defined(__FLAT__)
nuclear@2 411 farfree(struct_ptr);
nuclear@2 412 #else
nuclear@2 413 # if defined(_MSC_VER) && defined(MAXSEG_64K)
nuclear@2 414 hfree(struct_ptr);
nuclear@2 415 # else
nuclear@2 416 free(struct_ptr);
nuclear@2 417 # endif
nuclear@2 418 #endif
nuclear@2 419 }
nuclear@2 420 }
nuclear@2 421
nuclear@2 422 /* Allocate memory. For reasonable files, size should never exceed
nuclear@2 423 64K. However, zlib may allocate more then 64K if you don't tell
nuclear@2 424 it not to. See zconf.h and png.h for more information. zlib does
nuclear@2 425 need to allocate exactly 64K, so whatever you call here must
nuclear@2 426 have the ability to do that. */
nuclear@2 427
nuclear@2 428 png_voidp PNGAPI
nuclear@2 429 png_malloc(png_structp png_ptr, png_uint_32 size)
nuclear@2 430 {
nuclear@2 431 png_voidp ret;
nuclear@2 432
nuclear@2 433 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 434 if (png_ptr == NULL || size == 0)
nuclear@2 435 return (NULL);
nuclear@2 436
nuclear@2 437 if (png_ptr->malloc_fn != NULL)
nuclear@2 438 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
nuclear@2 439 else
nuclear@2 440 ret = (png_malloc_default(png_ptr, size));
nuclear@2 441 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 442 png_error(png_ptr, "Out of Memory!");
nuclear@2 443 return (ret);
nuclear@2 444 }
nuclear@2 445
nuclear@2 446 png_voidp PNGAPI
nuclear@2 447 png_malloc_default(png_structp png_ptr, png_uint_32 size)
nuclear@2 448 {
nuclear@2 449 png_voidp ret;
nuclear@2 450 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 451
nuclear@2 452 if (png_ptr == NULL || size == 0)
nuclear@2 453 return (NULL);
nuclear@2 454
nuclear@2 455 #ifdef PNG_MAX_MALLOC_64K
nuclear@2 456 if (size > (png_uint_32)65536L)
nuclear@2 457 {
nuclear@2 458 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 459 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 460 png_error(png_ptr, "Cannot Allocate > 64K");
nuclear@2 461 else
nuclear@2 462 #endif
nuclear@2 463 return NULL;
nuclear@2 464 }
nuclear@2 465 #endif
nuclear@2 466
nuclear@2 467 /* Check for overflow */
nuclear@2 468 #if defined(__TURBOC__) && !defined(__FLAT__)
nuclear@2 469 if (size != (unsigned long)size)
nuclear@2 470 ret = NULL;
nuclear@2 471 else
nuclear@2 472 ret = farmalloc(size);
nuclear@2 473 #else
nuclear@2 474 # if defined(_MSC_VER) && defined(MAXSEG_64K)
nuclear@2 475 if (size != (unsigned long)size)
nuclear@2 476 ret = NULL;
nuclear@2 477 else
nuclear@2 478 ret = halloc(size, 1);
nuclear@2 479 # else
nuclear@2 480 if (size != (size_t)size)
nuclear@2 481 ret = NULL;
nuclear@2 482 else
nuclear@2 483 ret = malloc((size_t)size);
nuclear@2 484 # endif
nuclear@2 485 #endif
nuclear@2 486
nuclear@2 487 #ifndef PNG_USER_MEM_SUPPORTED
nuclear@2 488 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
nuclear@2 489 png_error(png_ptr, "Out of Memory");
nuclear@2 490 #endif
nuclear@2 491
nuclear@2 492 return (ret);
nuclear@2 493 }
nuclear@2 494
nuclear@2 495 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
nuclear@2 496 without taking any action. */
nuclear@2 497 void PNGAPI
nuclear@2 498 png_free(png_structp png_ptr, png_voidp ptr)
nuclear@2 499 {
nuclear@2 500 if (png_ptr == NULL || ptr == NULL)
nuclear@2 501 return;
nuclear@2 502
nuclear@2 503 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 504 if (png_ptr->free_fn != NULL)
nuclear@2 505 {
nuclear@2 506 (*(png_ptr->free_fn))(png_ptr, ptr);
nuclear@2 507 return;
nuclear@2 508 }
nuclear@2 509 else png_free_default(png_ptr, ptr);
nuclear@2 510 }
nuclear@2 511 void PNGAPI
nuclear@2 512 png_free_default(png_structp png_ptr, png_voidp ptr)
nuclear@2 513 {
nuclear@2 514 if (png_ptr == NULL || ptr == NULL)
nuclear@2 515 return;
nuclear@2 516
nuclear@2 517 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 518
nuclear@2 519 #if defined(__TURBOC__) && !defined(__FLAT__)
nuclear@2 520 farfree(ptr);
nuclear@2 521 #else
nuclear@2 522 # if defined(_MSC_VER) && defined(MAXSEG_64K)
nuclear@2 523 hfree(ptr);
nuclear@2 524 # else
nuclear@2 525 free(ptr);
nuclear@2 526 # endif
nuclear@2 527 #endif
nuclear@2 528 }
nuclear@2 529
nuclear@2 530 #endif /* Not Borland DOS special memory handler */
nuclear@2 531
nuclear@2 532 #if defined(PNG_1_0_X)
nuclear@2 533 # define png_malloc_warn png_malloc
nuclear@2 534 #else
nuclear@2 535 /* This function was added at libpng version 1.2.3. The png_malloc_warn()
nuclear@2 536 * function will set up png_malloc() to issue a png_warning and return NULL
nuclear@2 537 * instead of issuing a png_error, if it fails to allocate the requested
nuclear@2 538 * memory.
nuclear@2 539 */
nuclear@2 540 png_voidp PNGAPI
nuclear@2 541 png_malloc_warn(png_structp png_ptr, png_uint_32 size)
nuclear@2 542 {
nuclear@2 543 png_voidp ptr;
nuclear@2 544 png_uint_32 save_flags;
nuclear@2 545 if (png_ptr == NULL) return (NULL);
nuclear@2 546
nuclear@2 547 save_flags = png_ptr->flags;
nuclear@2 548 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
nuclear@2 549 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
nuclear@2 550 png_ptr->flags=save_flags;
nuclear@2 551 return(ptr);
nuclear@2 552 }
nuclear@2 553 #endif
nuclear@2 554
nuclear@2 555 png_voidp PNGAPI
nuclear@2 556 png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
nuclear@2 557 png_uint_32 length)
nuclear@2 558 {
nuclear@2 559 png_size_t size;
nuclear@2 560
nuclear@2 561 size = (png_size_t)length;
nuclear@2 562 if ((png_uint_32)size != length)
nuclear@2 563 png_error(png_ptr, "Overflow in png_memcpy_check.");
nuclear@2 564
nuclear@2 565 return(png_memcpy (s1, s2, size));
nuclear@2 566 }
nuclear@2 567
nuclear@2 568 png_voidp PNGAPI
nuclear@2 569 png_memset_check (png_structp png_ptr, png_voidp s1, int value,
nuclear@2 570 png_uint_32 length)
nuclear@2 571 {
nuclear@2 572 png_size_t size;
nuclear@2 573
nuclear@2 574 size = (png_size_t)length;
nuclear@2 575 if ((png_uint_32)size != length)
nuclear@2 576 png_error(png_ptr, "Overflow in png_memset_check.");
nuclear@2 577
nuclear@2 578 return (png_memset (s1, value, size));
nuclear@2 579
nuclear@2 580 }
nuclear@2 581
nuclear@2 582 #ifdef PNG_USER_MEM_SUPPORTED
nuclear@2 583 /* This function is called when the application wants to use another method
nuclear@2 584 * of allocating and freeing memory.
nuclear@2 585 */
nuclear@2 586 void PNGAPI
nuclear@2 587 png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
nuclear@2 588 malloc_fn, png_free_ptr free_fn)
nuclear@2 589 {
nuclear@2 590 if (png_ptr != NULL)
nuclear@2 591 {
nuclear@2 592 png_ptr->mem_ptr = mem_ptr;
nuclear@2 593 png_ptr->malloc_fn = malloc_fn;
nuclear@2 594 png_ptr->free_fn = free_fn;
nuclear@2 595 }
nuclear@2 596 }
nuclear@2 597
nuclear@2 598 /* This function returns a pointer to the mem_ptr associated with the user
nuclear@2 599 * functions. The application should free any memory associated with this
nuclear@2 600 * pointer before png_write_destroy and png_read_destroy are called.
nuclear@2 601 */
nuclear@2 602 png_voidp PNGAPI
nuclear@2 603 png_get_mem_ptr(png_structp png_ptr)
nuclear@2 604 {
nuclear@2 605 if (png_ptr == NULL) return (NULL);
nuclear@2 606 return ((png_voidp)png_ptr->mem_ptr);
nuclear@2 607 }
nuclear@2 608 #endif /* PNG_USER_MEM_SUPPORTED */
nuclear@2 609 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */