1/* pngmem.c - stub functions for memory allocation2*3* Last changed in libpng 1.6.26 [October 20, 2016]4* Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12* This file provides a location for all memory allocation. Users who13* need special memory handling are expected to supply replacement14* functions for png_malloc() and png_free(), and to use15* png_create_read_struct_2() and png_create_write_struct_2() to16* identify the replacement functions.17*/1819#include "pngpriv.h"2021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)22/* Free a png_struct */23void /* PRIVATE */24png_destroy_png_struct(png_structrp png_ptr)25{26if (png_ptr != NULL)27{28/* png_free might call png_error and may certainly call29* png_get_mem_ptr, so fake a temporary png_struct to support this.30*/31png_struct dummy_struct = *png_ptr;32memset(png_ptr, 0, (sizeof *png_ptr));33png_free(&dummy_struct, png_ptr);3435# ifdef PNG_SETJMP_SUPPORTED36/* We may have a jmp_buf left to deallocate. */37png_free_jmpbuf(&dummy_struct);38# endif39}40}4142/* Allocate memory. For reasonable files, size should never exceed43* 64K. However, zlib may allocate more than 64K if you don't tell44* it not to. See zconf.h and png.h for more information. zlib does45* need to allocate exactly 64K, so whatever you call here must46* have the ability to do that.47*/48PNG_FUNCTION(png_voidp,PNGAPI49png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)50{51png_voidp ret;5253ret = png_malloc(png_ptr, size);5455if (ret != NULL)56memset(ret, 0, size);5758return ret;59}6061/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of62* allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.63* Checking and error handling must happen outside this routine; it returns NULL64* if the allocation cannot be done (for any reason.)65*/66PNG_FUNCTION(png_voidp /* PRIVATE */,67png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),68PNG_ALLOCATED)69{70/* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS71* allocators have also been removed in 1.6.0, so any 16-bit system now has72* to implement a user memory handler. This checks to be sure it isn't73* called with big numbers.74*/75#ifndef PNG_USER_MEM_SUPPORTED76PNG_UNUSED(png_ptr)77#endif7879/* Some compilers complain that this is always true. However, it80* can be false when integer overflow happens.81*/82if (size > 0 && size <= PNG_SIZE_MAX83# ifdef PNG_MAX_MALLOC_64K84&& size <= 65536U85# endif86)87{88#ifdef PNG_USER_MEM_SUPPORTED89if (png_ptr != NULL && png_ptr->malloc_fn != NULL)90return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);9192else93#endif94return malloc((size_t)size); /* checked for truncation above */95}9697else98return NULL;99}100101#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\102defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)103/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7104* that arises because of the checks in png_realloc_array that are repeated in105* png_malloc_array.106*/107static png_voidp108png_malloc_array_checked(png_const_structrp png_ptr, int nelements,109size_t element_size)110{111png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */112113if (req <= PNG_SIZE_MAX/element_size)114return png_malloc_base(png_ptr, req * element_size);115116/* The failure case when the request is too large */117return NULL;118}119120PNG_FUNCTION(png_voidp /* PRIVATE */,121png_malloc_array,(png_const_structrp png_ptr, int nelements,122size_t element_size),PNG_ALLOCATED)123{124if (nelements <= 0 || element_size == 0)125png_error(png_ptr, "internal error: array alloc");126127return png_malloc_array_checked(png_ptr, nelements, element_size);128}129130PNG_FUNCTION(png_voidp /* PRIVATE */,131png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,132int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)133{134/* These are internal errors: */135if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||136(old_array == NULL && old_elements > 0))137png_error(png_ptr, "internal error: array realloc");138139/* Check for overflow on the elements count (so the caller does not have to140* check.)141*/142if (add_elements <= INT_MAX - old_elements)143{144png_voidp new_array = png_malloc_array_checked(png_ptr,145old_elements+add_elements, element_size);146147if (new_array != NULL)148{149/* Because png_malloc_array worked the size calculations below cannot150* overflow.151*/152if (old_elements > 0)153memcpy(new_array, old_array, element_size*(unsigned)old_elements);154155memset((char*)new_array + element_size*(unsigned)old_elements, 0,156element_size*(unsigned)add_elements);157158return new_array;159}160}161162return NULL; /* error */163}164#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */165166/* Various functions that have different error handling are derived from this.167* png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate168* function png_malloc_default is also provided.169*/170PNG_FUNCTION(png_voidp,PNGAPI171png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)172{173png_voidp ret;174175if (png_ptr == NULL)176return NULL;177178ret = png_malloc_base(png_ptr, size);179180if (ret == NULL)181png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */182183return ret;184}185186#ifdef PNG_USER_MEM_SUPPORTED187PNG_FUNCTION(png_voidp,PNGAPI188png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),189PNG_ALLOCATED PNG_DEPRECATED)190{191png_voidp ret;192193if (png_ptr == NULL)194return NULL;195196/* Passing 'NULL' here bypasses the application provided memory handler. */197ret = png_malloc_base(NULL/*use malloc*/, size);198199if (ret == NULL)200png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */201202return ret;203}204#endif /* USER_MEM */205206/* This function was added at libpng version 1.2.3. The png_malloc_warn()207* function will issue a png_warning and return NULL instead of issuing a208* png_error, if it fails to allocate the requested memory.209*/210PNG_FUNCTION(png_voidp,PNGAPI211png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),212PNG_ALLOCATED)213{214if (png_ptr != NULL)215{216png_voidp ret = png_malloc_base(png_ptr, size);217218if (ret != NULL)219return ret;220221png_warning(png_ptr, "Out of memory");222}223224return NULL;225}226227/* Free a pointer allocated by png_malloc(). If ptr is NULL, return228* without taking any action.229*/230void PNGAPI231png_free(png_const_structrp png_ptr, png_voidp ptr)232{233if (png_ptr == NULL || ptr == NULL)234return;235236#ifdef PNG_USER_MEM_SUPPORTED237if (png_ptr->free_fn != NULL)238png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);239240else241png_free_default(png_ptr, ptr);242}243244PNG_FUNCTION(void,PNGAPI245png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)246{247if (png_ptr == NULL || ptr == NULL)248return;249#endif /* USER_MEM */250251free(ptr);252}253254#ifdef PNG_USER_MEM_SUPPORTED255/* This function is called when the application wants to use another method256* of allocating and freeing memory.257*/258void PNGAPI259png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr260malloc_fn, png_free_ptr free_fn)261{262if (png_ptr != NULL)263{264png_ptr->mem_ptr = mem_ptr;265png_ptr->malloc_fn = malloc_fn;266png_ptr->free_fn = free_fn;267}268}269270/* This function returns a pointer to the mem_ptr associated with the user271* functions. The application should free any memory associated with this272* pointer before png_write_destroy and png_read_destroy are called.273*/274png_voidp PNGAPI275png_get_mem_ptr(png_const_structrp png_ptr)276{277if (png_ptr == NULL)278return NULL;279280return png_ptr->mem_ptr;281}282#endif /* USER_MEM */283#endif /* READ || WRITE */284285286