dbf-halloween2015

annotate libs/libpng/pngmem.c @ 1:c3f5c32cb210

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