nuclear@26: nuclear@26: /* pngmem.c - stub functions for memory allocation nuclear@26: * nuclear@26: * Last changed in libpng 1.2.30 [August 15, 2008] nuclear@26: * For conditions of distribution and use, see copyright notice in png.h nuclear@26: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@26: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@26: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@26: * nuclear@26: * This file provides a location for all memory allocation. Users who nuclear@26: * need special memory handling are expected to supply replacement nuclear@26: * functions for png_malloc() and png_free(), and to use nuclear@26: * png_create_read_struct_2() and png_create_write_struct_2() to nuclear@26: * identify the replacement functions. nuclear@26: */ nuclear@26: nuclear@26: #define PNG_INTERNAL nuclear@26: #include "png.h" nuclear@26: #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) nuclear@26: nuclear@26: /* Borland DOS special memory handler */ nuclear@26: #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) nuclear@26: /* if you change this, be sure to change the one in png.h also */ nuclear@26: nuclear@26: /* Allocate memory for a png_struct. The malloc and memset can be replaced nuclear@26: by a single call to calloc() if this is thought to improve performance. */ nuclear@26: png_voidp /* PRIVATE */ nuclear@26: png_create_struct(int type) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); nuclear@26: } nuclear@26: nuclear@26: /* Alternate version of png_create_struct, for use with user-defined malloc. */ nuclear@26: png_voidp /* PRIVATE */ nuclear@26: png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) nuclear@26: { nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: png_size_t size; nuclear@26: png_voidp struct_ptr; nuclear@26: nuclear@26: if (type == PNG_STRUCT_INFO) nuclear@26: size = png_sizeof(png_info); nuclear@26: else if (type == PNG_STRUCT_PNG) nuclear@26: size = png_sizeof(png_struct); nuclear@26: else nuclear@26: return (png_get_copyright(NULL)); nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (malloc_fn != NULL) nuclear@26: { nuclear@26: png_struct dummy_struct; nuclear@26: png_structp png_ptr = &dummy_struct; nuclear@26: png_ptr->mem_ptr=mem_ptr; nuclear@26: struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); nuclear@26: } nuclear@26: else nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: struct_ptr = (png_voidp)farmalloc(size); nuclear@26: if (struct_ptr != NULL) nuclear@26: png_memset(struct_ptr, 0, size); nuclear@26: return (struct_ptr); nuclear@26: } nuclear@26: nuclear@26: /* Free memory allocated by a png_create_struct() call */ nuclear@26: void /* PRIVATE */ nuclear@26: png_destroy_struct(png_voidp struct_ptr) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); nuclear@26: } nuclear@26: nuclear@26: /* Free memory allocated by a png_create_struct() call */ nuclear@26: void /* PRIVATE */ nuclear@26: png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, nuclear@26: png_voidp mem_ptr) nuclear@26: { nuclear@26: #endif nuclear@26: if (struct_ptr != NULL) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (free_fn != NULL) nuclear@26: { nuclear@26: png_struct dummy_struct; nuclear@26: png_structp png_ptr = &dummy_struct; nuclear@26: png_ptr->mem_ptr=mem_ptr; nuclear@26: (*(free_fn))(png_ptr, struct_ptr); nuclear@26: return; nuclear@26: } nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: farfree (struct_ptr); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* Allocate memory. For reasonable files, size should never exceed nuclear@26: * 64K. However, zlib may allocate more then 64K if you don't tell nuclear@26: * it not to. See zconf.h and png.h for more information. zlib does nuclear@26: * need to allocate exactly 64K, so whatever you call here must nuclear@26: * have the ability to do that. nuclear@26: * nuclear@26: * Borland seems to have a problem in DOS mode for exactly 64K. nuclear@26: * It gives you a segment with an offset of 8 (perhaps to store its nuclear@26: * memory stuff). zlib doesn't like this at all, so we have to nuclear@26: * detect and deal with it. This code should not be needed in nuclear@26: * Windows or OS/2 modes, and only in 16 bit mode. This code has nuclear@26: * been updated by Alexander Lehmann for version 0.89 to waste less nuclear@26: * memory. nuclear@26: * nuclear@26: * Note that we can't use png_size_t for the "size" declaration, nuclear@26: * since on some systems a png_size_t is a 16-bit quantity, and as a nuclear@26: * result, we would be truncating potentially larger memory requests nuclear@26: * (which should cause a fatal error) and introducing major problems. nuclear@26: */ nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_malloc(png_structp png_ptr, png_uint_32 size) nuclear@26: { nuclear@26: png_voidp ret; nuclear@26: nuclear@26: if (png_ptr == NULL || size == 0) nuclear@26: return (NULL); nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (png_ptr->malloc_fn != NULL) nuclear@26: ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); nuclear@26: else nuclear@26: ret = (png_malloc_default(png_ptr, size)); nuclear@26: if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out of memory!"); nuclear@26: return (ret); nuclear@26: } nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_malloc_default(png_structp png_ptr, png_uint_32 size) nuclear@26: { nuclear@26: png_voidp ret; nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: nuclear@26: if (png_ptr == NULL || size == 0) nuclear@26: return (NULL); nuclear@26: nuclear@26: #ifdef PNG_MAX_MALLOC_64K nuclear@26: if (size > (png_uint_32)65536L) nuclear@26: { nuclear@26: png_warning(png_ptr, "Cannot Allocate > 64K"); nuclear@26: ret = NULL; nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: nuclear@26: if (size != (size_t)size) nuclear@26: ret = NULL; nuclear@26: else if (size == (png_uint_32)65536L) nuclear@26: { nuclear@26: if (png_ptr->offset_table == NULL) nuclear@26: { nuclear@26: /* try to see if we need to do any of this fancy stuff */ nuclear@26: ret = farmalloc(size); nuclear@26: if (ret == NULL || ((png_size_t)ret & 0xffff)) nuclear@26: { nuclear@26: int num_blocks; nuclear@26: png_uint_32 total_size; nuclear@26: png_bytep table; nuclear@26: int i; nuclear@26: png_byte huge * hptr; nuclear@26: nuclear@26: if (ret != NULL) nuclear@26: { nuclear@26: farfree(ret); nuclear@26: ret = NULL; nuclear@26: } nuclear@26: nuclear@26: if (png_ptr->zlib_window_bits > 14) nuclear@26: num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); nuclear@26: else nuclear@26: num_blocks = 1; nuclear@26: if (png_ptr->zlib_mem_level >= 7) nuclear@26: num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); nuclear@26: else nuclear@26: num_blocks++; nuclear@26: nuclear@26: total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; nuclear@26: nuclear@26: table = farmalloc(total_size); nuclear@26: nuclear@26: if (table == NULL) nuclear@26: { nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ nuclear@26: else nuclear@26: png_warning(png_ptr, "Out Of Memory."); nuclear@26: #endif nuclear@26: return (NULL); nuclear@26: } nuclear@26: nuclear@26: if ((png_size_t)table & 0xfff0) nuclear@26: { nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, nuclear@26: "Farmalloc didn't return normalized pointer"); nuclear@26: else nuclear@26: png_warning(png_ptr, nuclear@26: "Farmalloc didn't return normalized pointer"); nuclear@26: #endif nuclear@26: return (NULL); nuclear@26: } nuclear@26: nuclear@26: png_ptr->offset_table = table; nuclear@26: png_ptr->offset_table_ptr = farmalloc(num_blocks * nuclear@26: png_sizeof(png_bytep)); nuclear@26: nuclear@26: if (png_ptr->offset_table_ptr == NULL) nuclear@26: { nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */ nuclear@26: else nuclear@26: png_warning(png_ptr, "Out Of memory."); nuclear@26: #endif nuclear@26: return (NULL); nuclear@26: } nuclear@26: nuclear@26: hptr = (png_byte huge *)table; nuclear@26: if ((png_size_t)hptr & 0xf) nuclear@26: { nuclear@26: hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); nuclear@26: hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ nuclear@26: } nuclear@26: for (i = 0; i < num_blocks; i++) nuclear@26: { nuclear@26: png_ptr->offset_table_ptr[i] = (png_bytep)hptr; nuclear@26: hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ nuclear@26: } nuclear@26: nuclear@26: png_ptr->offset_table_number = num_blocks; nuclear@26: png_ptr->offset_table_count = 0; nuclear@26: png_ptr->offset_table_count_free = 0; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: if (png_ptr->offset_table_count >= png_ptr->offset_table_number) nuclear@26: { nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ nuclear@26: else nuclear@26: png_warning(png_ptr, "Out of Memory."); nuclear@26: #endif nuclear@26: return (NULL); nuclear@26: } nuclear@26: nuclear@26: ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; nuclear@26: } nuclear@26: else nuclear@26: ret = farmalloc(size); nuclear@26: nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if (ret == NULL) nuclear@26: { nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ nuclear@26: else nuclear@26: png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */ nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: return (ret); nuclear@26: } nuclear@26: nuclear@26: /* free a pointer allocated by png_malloc(). In the default nuclear@26: configuration, png_ptr is not used, but is passed in case it nuclear@26: is needed. If ptr is NULL, return without taking any action. */ nuclear@26: nuclear@26: void PNGAPI nuclear@26: png_free(png_structp png_ptr, png_voidp ptr) nuclear@26: { nuclear@26: if (png_ptr == NULL || ptr == NULL) nuclear@26: return; nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (png_ptr->free_fn != NULL) nuclear@26: { nuclear@26: (*(png_ptr->free_fn))(png_ptr, ptr); nuclear@26: return; nuclear@26: } nuclear@26: else png_free_default(png_ptr, ptr); nuclear@26: } nuclear@26: nuclear@26: void PNGAPI nuclear@26: png_free_default(png_structp png_ptr, png_voidp ptr) nuclear@26: { nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: nuclear@26: if (png_ptr == NULL || ptr == NULL) return; nuclear@26: nuclear@26: if (png_ptr->offset_table != NULL) nuclear@26: { nuclear@26: int i; nuclear@26: nuclear@26: for (i = 0; i < png_ptr->offset_table_count; i++) nuclear@26: { nuclear@26: if (ptr == png_ptr->offset_table_ptr[i]) nuclear@26: { nuclear@26: ptr = NULL; nuclear@26: png_ptr->offset_table_count_free++; nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) nuclear@26: { nuclear@26: farfree(png_ptr->offset_table); nuclear@26: farfree(png_ptr->offset_table_ptr); nuclear@26: png_ptr->offset_table = NULL; nuclear@26: png_ptr->offset_table_ptr = NULL; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: if (ptr != NULL) nuclear@26: { nuclear@26: farfree(ptr); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: #else /* Not the Borland DOS special memory handler */ nuclear@26: nuclear@26: /* Allocate memory for a png_struct or a png_info. The malloc and nuclear@26: memset can be replaced by a single call to calloc() if this is thought nuclear@26: to improve performance noticably. */ nuclear@26: png_voidp /* PRIVATE */ nuclear@26: png_create_struct(int type) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); nuclear@26: } nuclear@26: nuclear@26: /* Allocate memory for a png_struct or a png_info. The malloc and nuclear@26: memset can be replaced by a single call to calloc() if this is thought nuclear@26: to improve performance noticably. */ nuclear@26: png_voidp /* PRIVATE */ nuclear@26: png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) nuclear@26: { nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: png_size_t size; nuclear@26: png_voidp struct_ptr; nuclear@26: nuclear@26: if (type == PNG_STRUCT_INFO) nuclear@26: size = png_sizeof(png_info); nuclear@26: else if (type == PNG_STRUCT_PNG) nuclear@26: size = png_sizeof(png_struct); nuclear@26: else nuclear@26: return (NULL); nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (malloc_fn != NULL) nuclear@26: { nuclear@26: png_struct dummy_struct; nuclear@26: png_structp png_ptr = &dummy_struct; nuclear@26: png_ptr->mem_ptr=mem_ptr; nuclear@26: struct_ptr = (*(malloc_fn))(png_ptr, size); nuclear@26: if (struct_ptr != NULL) nuclear@26: png_memset(struct_ptr, 0, size); nuclear@26: return (struct_ptr); nuclear@26: } nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: nuclear@26: #if defined(__TURBOC__) && !defined(__FLAT__) nuclear@26: struct_ptr = (png_voidp)farmalloc(size); nuclear@26: #else nuclear@26: # if defined(_MSC_VER) && defined(MAXSEG_64K) nuclear@26: struct_ptr = (png_voidp)halloc(size, 1); nuclear@26: # else nuclear@26: struct_ptr = (png_voidp)malloc(size); nuclear@26: # endif nuclear@26: #endif nuclear@26: if (struct_ptr != NULL) nuclear@26: png_memset(struct_ptr, 0, size); nuclear@26: nuclear@26: return (struct_ptr); nuclear@26: } nuclear@26: nuclear@26: nuclear@26: /* Free memory allocated by a png_create_struct() call */ nuclear@26: void /* PRIVATE */ nuclear@26: png_destroy_struct(png_voidp struct_ptr) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); nuclear@26: } nuclear@26: nuclear@26: /* Free memory allocated by a png_create_struct() call */ nuclear@26: void /* PRIVATE */ nuclear@26: png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, nuclear@26: png_voidp mem_ptr) nuclear@26: { nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: if (struct_ptr != NULL) nuclear@26: { nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (free_fn != NULL) nuclear@26: { nuclear@26: png_struct dummy_struct; nuclear@26: png_structp png_ptr = &dummy_struct; nuclear@26: png_ptr->mem_ptr=mem_ptr; nuclear@26: (*(free_fn))(png_ptr, struct_ptr); nuclear@26: return; nuclear@26: } nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: #if defined(__TURBOC__) && !defined(__FLAT__) nuclear@26: farfree(struct_ptr); nuclear@26: #else nuclear@26: # if defined(_MSC_VER) && defined(MAXSEG_64K) nuclear@26: hfree(struct_ptr); nuclear@26: # else nuclear@26: free(struct_ptr); nuclear@26: # endif nuclear@26: #endif nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* Allocate memory. For reasonable files, size should never exceed nuclear@26: 64K. However, zlib may allocate more then 64K if you don't tell nuclear@26: it not to. See zconf.h and png.h for more information. zlib does nuclear@26: need to allocate exactly 64K, so whatever you call here must nuclear@26: have the ability to do that. */ nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_malloc(png_structp png_ptr, png_uint_32 size) nuclear@26: { nuclear@26: png_voidp ret; nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (png_ptr == NULL || size == 0) nuclear@26: return (NULL); nuclear@26: nuclear@26: if (png_ptr->malloc_fn != NULL) nuclear@26: ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); nuclear@26: else nuclear@26: ret = (png_malloc_default(png_ptr, size)); nuclear@26: if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out of Memory!"); nuclear@26: return (ret); nuclear@26: } nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_malloc_default(png_structp png_ptr, png_uint_32 size) nuclear@26: { nuclear@26: png_voidp ret; nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: nuclear@26: if (png_ptr == NULL || size == 0) nuclear@26: return (NULL); nuclear@26: nuclear@26: #ifdef PNG_MAX_MALLOC_64K nuclear@26: if (size > (png_uint_32)65536L) nuclear@26: { nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Cannot Allocate > 64K"); nuclear@26: else nuclear@26: #endif nuclear@26: return NULL; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: /* Check for overflow */ nuclear@26: #if defined(__TURBOC__) && !defined(__FLAT__) nuclear@26: if (size != (unsigned long)size) nuclear@26: ret = NULL; nuclear@26: else nuclear@26: ret = farmalloc(size); nuclear@26: #else nuclear@26: # if defined(_MSC_VER) && defined(MAXSEG_64K) nuclear@26: if (size != (unsigned long)size) nuclear@26: ret = NULL; nuclear@26: else nuclear@26: ret = halloc(size, 1); nuclear@26: # else nuclear@26: if (size != (size_t)size) nuclear@26: ret = NULL; nuclear@26: else nuclear@26: ret = malloc((size_t)size); nuclear@26: # endif nuclear@26: #endif nuclear@26: nuclear@26: #ifndef PNG_USER_MEM_SUPPORTED nuclear@26: if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) nuclear@26: png_error(png_ptr, "Out of Memory"); nuclear@26: #endif nuclear@26: nuclear@26: return (ret); nuclear@26: } nuclear@26: nuclear@26: /* Free a pointer allocated by png_malloc(). If ptr is NULL, return nuclear@26: without taking any action. */ nuclear@26: void PNGAPI nuclear@26: png_free(png_structp png_ptr, png_voidp ptr) nuclear@26: { nuclear@26: if (png_ptr == NULL || ptr == NULL) nuclear@26: return; nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: if (png_ptr->free_fn != NULL) nuclear@26: { nuclear@26: (*(png_ptr->free_fn))(png_ptr, ptr); nuclear@26: return; nuclear@26: } nuclear@26: else png_free_default(png_ptr, ptr); nuclear@26: } nuclear@26: void PNGAPI nuclear@26: png_free_default(png_structp png_ptr, png_voidp ptr) nuclear@26: { nuclear@26: if (png_ptr == NULL || ptr == NULL) nuclear@26: return; nuclear@26: nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: nuclear@26: #if defined(__TURBOC__) && !defined(__FLAT__) nuclear@26: farfree(ptr); nuclear@26: #else nuclear@26: # if defined(_MSC_VER) && defined(MAXSEG_64K) nuclear@26: hfree(ptr); nuclear@26: # else nuclear@26: free(ptr); nuclear@26: # endif nuclear@26: #endif nuclear@26: } nuclear@26: nuclear@26: #endif /* Not Borland DOS special memory handler */ nuclear@26: nuclear@26: #if defined(PNG_1_0_X) nuclear@26: # define png_malloc_warn png_malloc nuclear@26: #else nuclear@26: /* This function was added at libpng version 1.2.3. The png_malloc_warn() nuclear@26: * function will set up png_malloc() to issue a png_warning and return NULL nuclear@26: * instead of issuing a png_error, if it fails to allocate the requested nuclear@26: * memory. nuclear@26: */ nuclear@26: png_voidp PNGAPI nuclear@26: png_malloc_warn(png_structp png_ptr, png_uint_32 size) nuclear@26: { nuclear@26: png_voidp ptr; nuclear@26: png_uint_32 save_flags; nuclear@26: if (png_ptr == NULL) return (NULL); nuclear@26: nuclear@26: save_flags = png_ptr->flags; nuclear@26: png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; nuclear@26: ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); nuclear@26: png_ptr->flags=save_flags; nuclear@26: return(ptr); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, nuclear@26: png_uint_32 length) nuclear@26: { nuclear@26: png_size_t size; nuclear@26: nuclear@26: size = (png_size_t)length; nuclear@26: if ((png_uint_32)size != length) nuclear@26: png_error(png_ptr, "Overflow in png_memcpy_check."); nuclear@26: nuclear@26: return(png_memcpy (s1, s2, size)); nuclear@26: } nuclear@26: nuclear@26: png_voidp PNGAPI nuclear@26: png_memset_check (png_structp png_ptr, png_voidp s1, int value, nuclear@26: png_uint_32 length) nuclear@26: { nuclear@26: png_size_t size; nuclear@26: nuclear@26: size = (png_size_t)length; nuclear@26: if ((png_uint_32)size != length) nuclear@26: png_error(png_ptr, "Overflow in png_memset_check."); nuclear@26: nuclear@26: return (png_memset (s1, value, size)); nuclear@26: nuclear@26: } nuclear@26: nuclear@26: #ifdef PNG_USER_MEM_SUPPORTED nuclear@26: /* This function is called when the application wants to use another method nuclear@26: * of allocating and freeing memory. nuclear@26: */ nuclear@26: void PNGAPI nuclear@26: png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr nuclear@26: malloc_fn, png_free_ptr free_fn) nuclear@26: { nuclear@26: if (png_ptr != NULL) nuclear@26: { nuclear@26: png_ptr->mem_ptr = mem_ptr; nuclear@26: png_ptr->malloc_fn = malloc_fn; nuclear@26: png_ptr->free_fn = free_fn; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* This function returns a pointer to the mem_ptr associated with the user nuclear@26: * functions. The application should free any memory associated with this nuclear@26: * pointer before png_write_destroy and png_read_destroy are called. nuclear@26: */ nuclear@26: png_voidp PNGAPI nuclear@26: png_get_mem_ptr(png_structp png_ptr) nuclear@26: { nuclear@26: if (png_ptr == NULL) return (NULL); nuclear@26: return ((png_voidp)png_ptr->mem_ptr); nuclear@26: } nuclear@26: #endif /* PNG_USER_MEM_SUPPORTED */ nuclear@26: #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */