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