| OLD | NEW |
| (Empty) |
| 1 /* pngmem.c - stub functions for memory allocation | |
| 2 * | |
| 3 * Last changed in libpng 1.6.0 [February 14, 2013] | |
| 4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson | |
| 5 * (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 disclaimer | |
| 10 * and license in png.h | |
| 11 * | |
| 12 * This file provides a location for all memory allocation. Users who | |
| 13 * need special memory handling are expected to supply replacement | |
| 14 * functions for png_malloc() and png_free(), and to use | |
| 15 * png_create_read_struct_2() and png_create_write_struct_2() to | |
| 16 * identify the replacement functions. | |
| 17 */ | |
| 18 | |
| 19 #include "pngpriv.h" | |
| 20 | |
| 21 void* FXMEM_DefaultAlloc(int byte_size, int); | |
| 22 void FXMEM_DefaultFree(void* pointer, int); | |
| 23 | |
| 24 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) | |
| 25 /* Free a png_struct */ | |
| 26 void /* PRIVATE */ | |
| 27 png_destroy_png_struct(png_structrp png_ptr) | |
| 28 { | |
| 29 if (png_ptr != NULL) | |
| 30 { | |
| 31 /* png_free might call png_error and may certainly call | |
| 32 * png_get_mem_ptr, so fake a temporary png_struct to support this. | |
| 33 */ | |
| 34 png_struct dummy_struct = *png_ptr; | |
| 35 memset(png_ptr, 0, (sizeof *png_ptr)); | |
| 36 png_free(&dummy_struct, png_ptr); | |
| 37 | |
| 38 # ifdef PNG_SETJMP_SUPPORTED | |
| 39 /* We may have a jmp_buf left to deallocate. */ | |
| 40 png_free_jmpbuf(&dummy_struct); | |
| 41 # endif | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /* Allocate memory. For reasonable files, size should never exceed | |
| 46 * 64K. However, zlib may allocate more then 64K if you don't tell | |
| 47 * it not to. See zconf.h and png.h for more information. zlib does | |
| 48 * need to allocate exactly 64K, so whatever you call here must | |
| 49 * have the ability to do that. | |
| 50 */ | |
| 51 PNG_FUNCTION(png_voidp,PNGAPI | |
| 52 png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) | |
| 53 { | |
| 54 png_voidp ret; | |
| 55 | |
| 56 ret = png_malloc(png_ptr, size); | |
| 57 | |
| 58 if (ret != NULL) | |
| 59 memset(ret, 0, size); | |
| 60 | |
| 61 return ret; | |
| 62 } | |
| 63 | |
| 64 /* png_malloc_base, an internal function added at libpng 1.6.0, does the work of | |
| 65 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED. | |
| 66 * Checking and error handling must happen outside this routine; it returns NULL | |
| 67 * if the allocation cannot be done (for any reason.) | |
| 68 */ | |
| 69 PNG_FUNCTION(png_voidp /* PRIVATE */, | |
| 70 png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), | |
| 71 PNG_ALLOCATED) | |
| 72 { | |
| 73 /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS | |
| 74 * allocators have also been removed in 1.6.0, so any 16-bit system now has | |
| 75 * to implement a user memory handler. This checks to be sure it isn't | |
| 76 * called with big numbers. | |
| 77 */ | |
| 78 #ifdef PNG_USER_MEM_SUPPORTED | |
| 79 PNG_UNUSED(png_ptr) | |
| 80 #endif | |
| 81 if (size > 0 && size <= PNG_SIZE_MAX | |
| 82 # ifdef PNG_MAX_MALLOC_64K | |
| 83 && size <= 65536U | |
| 84 # endif | |
| 85 ) | |
| 86 { | |
| 87 #ifdef PNG_USER_MEM_SUPPORTED | |
| 88 if (png_ptr != NULL && png_ptr->malloc_fn != NULL) | |
| 89 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); | |
| 90 | |
| 91 else | |
| 92 #endif | |
| 93 return FXMEM_DefaultAlloc((int)size, 0); | |
| 94 //return malloc((size_t)size); /* checked for truncation above */ | |
| 95 } | |
| 96 | |
| 97 else | |
| 98 return NULL; | |
| 99 } | |
| 100 | |
| 101 /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7 | |
| 102 * that arises because of the checks in png_realloc_array that are repeated in | |
| 103 * png_malloc_array. | |
| 104 */ | |
| 105 static png_voidp | |
| 106 png_malloc_array_checked(png_const_structrp png_ptr, int nelements, | |
| 107 size_t element_size) | |
| 108 { | |
| 109 png_alloc_size_t req = nelements; /* known to be > 0 */ | |
| 110 | |
| 111 if (req <= PNG_SIZE_MAX/element_size) | |
| 112 return png_malloc_base(png_ptr, req * element_size); | |
| 113 | |
| 114 /* The failure case when the request is too large */ | |
| 115 return NULL; | |
| 116 } | |
| 117 | |
| 118 PNG_FUNCTION(png_voidp /* PRIVATE */, | |
| 119 png_malloc_array,(png_const_structrp png_ptr, int nelements, | |
| 120 size_t element_size),PNG_ALLOCATED) | |
| 121 { | |
| 122 if (nelements <= 0 || element_size == 0) | |
| 123 png_error(png_ptr, "internal error: array alloc"); | |
| 124 | |
| 125 return png_malloc_array_checked(png_ptr, nelements, element_size); | |
| 126 } | |
| 127 | |
| 128 PNG_FUNCTION(png_voidp /* PRIVATE */, | |
| 129 png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array, | |
| 130 int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED) | |
| 131 { | |
| 132 /* These are internal errors: */ | |
| 133 if (add_elements <= 0 || element_size == 0 || old_elements < 0 || | |
| 134 (old_array == NULL && old_elements > 0)) | |
| 135 png_error(png_ptr, "internal error: array realloc"); | |
| 136 | |
| 137 /* Check for overflow on the elements count (so the caller does not have to | |
| 138 * check.) | |
| 139 */ | |
| 140 if (add_elements <= INT_MAX - old_elements) | |
| 141 { | |
| 142 png_voidp new_array = png_malloc_array_checked(png_ptr, | |
| 143 old_elements+add_elements, element_size); | |
| 144 | |
| 145 if (new_array != NULL) | |
| 146 { | |
| 147 /* Because png_malloc_array worked the size calculations below cannot | |
| 148 * overflow. | |
| 149 */ | |
| 150 if (old_elements > 0) | |
| 151 memcpy(new_array, old_array, element_size*(unsigned)old_elements); | |
| 152 | |
| 153 memset((char*)new_array + element_size*(unsigned)old_elements, 0, | |
| 154 element_size*(unsigned)add_elements); | |
| 155 | |
| 156 return new_array; | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 return NULL; /* error */ | |
| 161 } | |
| 162 | |
| 163 /* Various functions that have different error handling are derived from this. | |
| 164 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate | |
| 165 * function png_malloc_default is also provided. | |
| 166 */ | |
| 167 PNG_FUNCTION(png_voidp,PNGAPI | |
| 168 png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) | |
| 169 { | |
| 170 png_voidp ret; | |
| 171 | |
| 172 if (png_ptr == NULL) | |
| 173 return NULL; | |
| 174 | |
| 175 ret = png_malloc_base(png_ptr, size); | |
| 176 | |
| 177 if (ret == NULL) | |
| 178 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */ | |
| 179 | |
| 180 return ret; | |
| 181 } | |
| 182 | |
| 183 #ifdef PNG_USER_MEM_SUPPORTED | |
| 184 PNG_FUNCTION(png_voidp,PNGAPI | |
| 185 png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size), | |
| 186 PNG_ALLOCATED PNG_DEPRECATED) | |
| 187 { | |
| 188 png_voidp ret; | |
| 189 | |
| 190 if (png_ptr == NULL) | |
| 191 return NULL; | |
| 192 | |
| 193 /* Passing 'NULL' here bypasses the application provided memory handler. */ | |
| 194 ret = png_malloc_base(NULL/*use malloc*/, size); | |
| 195 | |
| 196 if (ret == NULL) | |
| 197 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ | |
| 198 | |
| 199 return ret; | |
| 200 } | |
| 201 #endif /* PNG_USER_MEM_SUPPORTED */ | |
| 202 | |
| 203 /* This function was added at libpng version 1.2.3. The png_malloc_warn() | |
| 204 * function will issue a png_warning and return NULL instead of issuing a | |
| 205 * png_error, if it fails to allocate the requested memory. | |
| 206 */ | |
| 207 PNG_FUNCTION(png_voidp,PNGAPI | |
| 208 png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), | |
| 209 PNG_ALLOCATED) | |
| 210 { | |
| 211 if (png_ptr != NULL) | |
| 212 { | |
| 213 png_voidp ret = png_malloc_base(png_ptr, size); | |
| 214 | |
| 215 if (ret != NULL) | |
| 216 return ret; | |
| 217 | |
| 218 png_warning(png_ptr, "Out of memory"); | |
| 219 } | |
| 220 | |
| 221 return NULL; | |
| 222 } | |
| 223 | |
| 224 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return | |
| 225 * without taking any action. | |
| 226 */ | |
| 227 void PNGAPI | |
| 228 png_free(png_const_structrp png_ptr, png_voidp ptr) | |
| 229 { | |
| 230 if (png_ptr == NULL || ptr == NULL) | |
| 231 return; | |
| 232 | |
| 233 #ifdef PNG_USER_MEM_SUPPORTED | |
| 234 if (png_ptr->free_fn != NULL) | |
| 235 png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr); | |
| 236 | |
| 237 else | |
| 238 png_free_default(png_ptr, ptr); | |
| 239 } | |
| 240 | |
| 241 PNG_FUNCTION(void,PNGAPI | |
| 242 png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) | |
| 243 { | |
| 244 if (png_ptr == NULL || ptr == NULL) | |
| 245 return; | |
| 246 #endif /* PNG_USER_MEM_SUPPORTED */ | |
| 247 | |
| 248 FXMEM_DefaultFree(ptr, 0); | |
| 249 //free(ptr); | |
| 250 } | |
| 251 | |
| 252 #ifdef PNG_USER_MEM_SUPPORTED | |
| 253 /* This function is called when the application wants to use another method | |
| 254 * of allocating and freeing memory. | |
| 255 */ | |
| 256 void PNGAPI | |
| 257 png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr | |
| 258 malloc_fn, png_free_ptr free_fn) | |
| 259 { | |
| 260 if (png_ptr != NULL) | |
| 261 { | |
| 262 png_ptr->mem_ptr = mem_ptr; | |
| 263 png_ptr->malloc_fn = malloc_fn; | |
| 264 png_ptr->free_fn = free_fn; | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 /* This function returns a pointer to the mem_ptr associated with the user | |
| 269 * functions. The application should free any memory associated with this | |
| 270 * pointer before png_write_destroy and png_read_destroy are called. | |
| 271 */ | |
| 272 png_voidp PNGAPI | |
| 273 png_get_mem_ptr(png_const_structrp png_ptr) | |
| 274 { | |
| 275 if (png_ptr == NULL) | |
| 276 return NULL; | |
| 277 | |
| 278 return png_ptr->mem_ptr; | |
| 279 } | |
| 280 #endif /* PNG_USER_MEM_SUPPORTED */ | |
| 281 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ | |
| OLD | NEW |