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