OLD | NEW |
| 1 |
1 /* pngmem.c - stub functions for memory allocation | 2 /* pngmem.c - stub functions for memory allocation |
2 * | 3 * |
3 * Last changed in libpng 1.6.0 [February 14, 2013] | 4 * Last changed in libpng 1.6.15 [November 20, 2014] |
4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson |
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
7 * | 8 * |
8 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
9 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
10 * and license in png.h | 11 * and license in png.h |
11 * | 12 * |
12 * This file provides a location for all memory allocation. Users who | 13 * This file provides a location for all memory allocation. Users who |
13 * need special memory handling are expected to supply replacement | 14 * need special memory handling are expected to supply replacement |
14 * functions for png_malloc() and png_free(), and to use | 15 * functions for png_malloc() and png_free(), and to use |
(...skipping 21 matching lines...) Expand all Loading... |
36 png_free(&dummy_struct, png_ptr); | 37 png_free(&dummy_struct, png_ptr); |
37 | 38 |
38 # ifdef PNG_SETJMP_SUPPORTED | 39 # ifdef PNG_SETJMP_SUPPORTED |
39 /* We may have a jmp_buf left to deallocate. */ | 40 /* We may have a jmp_buf left to deallocate. */ |
40 png_free_jmpbuf(&dummy_struct); | 41 png_free_jmpbuf(&dummy_struct); |
41 # endif | 42 # endif |
42 } | 43 } |
43 } | 44 } |
44 | 45 |
45 /* Allocate memory. For reasonable files, size should never exceed | 46 /* Allocate memory. For reasonable files, size should never exceed |
46 * 64K. However, zlib may allocate more then 64K if you don't tell | 47 * 64K. However, zlib may allocate more than 64K if you don't tell |
47 * it not to. See zconf.h and png.h for more information. zlib does | 48 * 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 * need to allocate exactly 64K, so whatever you call here must |
49 * have the ability to do that. | 50 * have the ability to do that. |
50 */ | 51 */ |
51 PNG_FUNCTION(png_voidp,PNGAPI | 52 PNG_FUNCTION(png_voidp,PNGAPI |
52 png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) | 53 png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
53 { | 54 { |
54 png_voidp ret; | 55 png_voidp ret; |
55 | 56 |
56 ret = png_malloc(png_ptr, size); | 57 ret = png_malloc(png_ptr, size); |
(...skipping 11 matching lines...) Expand all Loading... |
68 */ | 69 */ |
69 PNG_FUNCTION(png_voidp /* PRIVATE */, | 70 PNG_FUNCTION(png_voidp /* PRIVATE */, |
70 png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), | 71 png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), |
71 PNG_ALLOCATED) | 72 PNG_ALLOCATED) |
72 { | 73 { |
73 /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS | 74 /* 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 * 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 * to implement a user memory handler. This checks to be sure it isn't |
76 * called with big numbers. | 77 * called with big numbers. |
77 */ | 78 */ |
78 #ifdef PNG_USER_MEM_SUPPORTED | 79 #ifndef PNG_USER_MEM_SUPPORTED |
79 PNG_UNUSED(png_ptr) | 80 PNG_UNUSED(png_ptr) |
80 #endif | 81 #endif |
| 82 |
| 83 /* Some compilers complain that this is always true. However, it |
| 84 * can be false when integer overflow happens. |
| 85 */ |
81 if (size > 0 && size <= PNG_SIZE_MAX | 86 if (size > 0 && size <= PNG_SIZE_MAX |
82 # ifdef PNG_MAX_MALLOC_64K | 87 # ifdef PNG_MAX_MALLOC_64K |
83 && size <= 65536U | 88 && size <= 65536U |
84 # endif | 89 # endif |
85 ) | 90 ) |
86 { | 91 { |
87 #ifdef PNG_USER_MEM_SUPPORTED | 92 #ifdef PNG_USER_MEM_SUPPORTED |
88 if (png_ptr != NULL && png_ptr->malloc_fn != NULL) | 93 if (png_ptr != NULL && png_ptr->malloc_fn != NULL) |
89 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); | 94 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); |
90 | 95 |
91 else | 96 else |
92 #endif | 97 #endif |
93 return FXMEM_DefaultAlloc((int)size, 0); | 98 return FXMEM_DefaultAlloc((int)size, 0); |
94 //return malloc((size_t)size); /* checked for truncation above */ | |
95 } | 99 } |
96 | 100 |
97 else | 101 else |
98 return NULL; | 102 return NULL; |
99 } | 103 } |
100 | 104 |
| 105 #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ |
| 106 defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) |
101 /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7 | 107 /* 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 | 108 * that arises because of the checks in png_realloc_array that are repeated in |
103 * png_malloc_array. | 109 * png_malloc_array. |
104 */ | 110 */ |
105 static png_voidp | 111 static png_voidp |
106 png_malloc_array_checked(png_const_structrp png_ptr, int nelements, | 112 png_malloc_array_checked(png_const_structrp png_ptr, int nelements, |
107 size_t element_size) | 113 size_t element_size) |
108 { | 114 { |
109 png_alloc_size_t req = nelements; /* known to be > 0 */ | 115 png_alloc_size_t req = nelements; /* known to be > 0 */ |
110 | 116 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 158 |
153 memset((char*)new_array + element_size*(unsigned)old_elements, 0, | 159 memset((char*)new_array + element_size*(unsigned)old_elements, 0, |
154 element_size*(unsigned)add_elements); | 160 element_size*(unsigned)add_elements); |
155 | 161 |
156 return new_array; | 162 return new_array; |
157 } | 163 } |
158 } | 164 } |
159 | 165 |
160 return NULL; /* error */ | 166 return NULL; /* error */ |
161 } | 167 } |
| 168 #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */ |
162 | 169 |
163 /* Various functions that have different error handling are derived from this. | 170 /* 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 | 171 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate |
165 * function png_malloc_default is also provided. | 172 * function png_malloc_default is also provided. |
166 */ | 173 */ |
167 PNG_FUNCTION(png_voidp,PNGAPI | 174 PNG_FUNCTION(png_voidp,PNGAPI |
168 png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) | 175 png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
169 { | 176 { |
170 png_voidp ret; | 177 png_voidp ret; |
171 | 178 |
(...skipping 19 matching lines...) Expand all Loading... |
191 return NULL; | 198 return NULL; |
192 | 199 |
193 /* Passing 'NULL' here bypasses the application provided memory handler. */ | 200 /* Passing 'NULL' here bypasses the application provided memory handler. */ |
194 ret = png_malloc_base(NULL/*use malloc*/, size); | 201 ret = png_malloc_base(NULL/*use malloc*/, size); |
195 | 202 |
196 if (ret == NULL) | 203 if (ret == NULL) |
197 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ | 204 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ |
198 | 205 |
199 return ret; | 206 return ret; |
200 } | 207 } |
201 #endif /* PNG_USER_MEM_SUPPORTED */ | 208 #endif /* USER_MEM */ |
202 | 209 |
203 /* This function was added at libpng version 1.2.3. The png_malloc_warn() | 210 /* 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 | 211 * function will issue a png_warning and return NULL instead of issuing a |
205 * png_error, if it fails to allocate the requested memory. | 212 * png_error, if it fails to allocate the requested memory. |
206 */ | 213 */ |
207 PNG_FUNCTION(png_voidp,PNGAPI | 214 PNG_FUNCTION(png_voidp,PNGAPI |
208 png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), | 215 png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), |
209 PNG_ALLOCATED) | 216 PNG_ALLOCATED) |
210 { | 217 { |
211 if (png_ptr != NULL) | 218 if (png_ptr != NULL) |
(...skipping 24 matching lines...) Expand all Loading... |
236 | 243 |
237 else | 244 else |
238 png_free_default(png_ptr, ptr); | 245 png_free_default(png_ptr, ptr); |
239 } | 246 } |
240 | 247 |
241 PNG_FUNCTION(void,PNGAPI | 248 PNG_FUNCTION(void,PNGAPI |
242 png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) | 249 png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) |
243 { | 250 { |
244 if (png_ptr == NULL || ptr == NULL) | 251 if (png_ptr == NULL || ptr == NULL) |
245 return; | 252 return; |
246 #endif /* PNG_USER_MEM_SUPPORTED */ | 253 #endif /* USER_MEM */ |
247 | 254 |
248 FXMEM_DefaultFree(ptr, 0); | 255 FXMEM_DefaultFree(ptr, 0); |
249 //free(ptr); | |
250 } | 256 } |
251 | 257 |
252 #ifdef PNG_USER_MEM_SUPPORTED | 258 #ifdef PNG_USER_MEM_SUPPORTED |
253 /* This function is called when the application wants to use another method | 259 /* This function is called when the application wants to use another method |
254 * of allocating and freeing memory. | 260 * of allocating and freeing memory. |
255 */ | 261 */ |
256 void PNGAPI | 262 void PNGAPI |
257 png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr | 263 png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
258 malloc_fn, png_free_ptr free_fn) | 264 malloc_fn, png_free_ptr free_fn) |
259 { | 265 { |
(...skipping 10 matching lines...) Expand all Loading... |
270 * pointer before png_write_destroy and png_read_destroy are called. | 276 * pointer before png_write_destroy and png_read_destroy are called. |
271 */ | 277 */ |
272 png_voidp PNGAPI | 278 png_voidp PNGAPI |
273 png_get_mem_ptr(png_const_structrp png_ptr) | 279 png_get_mem_ptr(png_const_structrp png_ptr) |
274 { | 280 { |
275 if (png_ptr == NULL) | 281 if (png_ptr == NULL) |
276 return NULL; | 282 return NULL; |
277 | 283 |
278 return png_ptr->mem_ptr; | 284 return png_ptr->mem_ptr; |
279 } | 285 } |
280 #endif /* PNG_USER_MEM_SUPPORTED */ | 286 #endif /* USER_MEM */ |
281 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ | 287 #endif /* READ || WRITE */ |
OLD | NEW |