VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.45/pngmem.c@ 108453

最後變更 在這個檔案從108453是 107813,由 vboxsync 提交於 2 月 前

libpng-1.6.45: Applied and adjusted our libpng changes to 1.6.45. bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 8.1 KB
 
1/* pngmem.c - stub functions for memory allocation
2 *
3 * Copyright (c) 2018 Cosmin Truta
4 * Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson
5 * Copyright (c) 1996-1997 Andreas Dilger
6 * 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#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{
26 if (png_ptr != NULL)
27 {
28 /* png_free might call png_error and may certainly call
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
35# ifdef PNG_SETJMP_SUPPORTED
36 /* We may have a jmp_buf left to deallocate. */
37 png_free_jmpbuf(&dummy_struct);
38# endif
39 }
40}
41
42/* Allocate memory. For reasonable files, size should never exceed
43 * 64K. However, zlib may allocate more than 64K if you don't tell
44 * it not to. See zconf.h and png.h for more information. zlib does
45 * need to allocate exactly 64K, so whatever you call here must
46 * have the ability to do that.
47 */
48PNG_FUNCTION(png_voidp,PNGAPI
49png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
50{
51 png_voidp ret;
52
53 ret = png_malloc(png_ptr, size);
54
55 if (ret != NULL)
56 memset(ret, 0, size);
57
58 return ret;
59}
60
61/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
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 */
66PNG_FUNCTION(png_voidp /* PRIVATE */,
67png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
68 PNG_ALLOCATED)
69{
70 /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
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#ifndef PNG_USER_MEM_SUPPORTED
76 PNG_UNUSED(png_ptr)
77#endif
78
79 /* Some compilers complain that this is always true. However, it
80 * can be false when integer overflow happens.
81 */
82 if (size > 0 && size <= PNG_SIZE_MAX
83# ifdef PNG_MAX_MALLOC_64K
84 && size <= 65536U
85# endif
86 )
87 {
88#ifdef PNG_USER_MEM_SUPPORTED
89 if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
90 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
91
92 else
93#endif
94 return malloc((size_t)size); /* checked for truncation above */
95 }
96
97 else
98 return NULL;
99}
100
101#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
102 defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
103/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
104 * that arises because of the checks in png_realloc_array that are repeated in
105 * png_malloc_array.
106 */
107static png_voidp
108png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
109 size_t element_size)
110{
111 png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */
112
113 if (req <= PNG_SIZE_MAX/element_size)
114 return png_malloc_base(png_ptr, req * element_size);
115
116 /* The failure case when the request is too large */
117 return NULL;
118}
119
120PNG_FUNCTION(png_voidp /* PRIVATE */,
121png_malloc_array,(png_const_structrp png_ptr, int nelements,
122 size_t element_size),PNG_ALLOCATED)
123{
124 if (nelements <= 0 || element_size == 0)
125 png_error(png_ptr, "internal error: array alloc");
126
127 return png_malloc_array_checked(png_ptr, nelements, element_size);
128}
129
130PNG_FUNCTION(png_voidp /* PRIVATE */,
131png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
132 int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
133{
134 /* These are internal errors: */
135 if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
136 (old_array == NULL && old_elements > 0))
137 png_error(png_ptr, "internal error: array realloc");
138
139 /* Check for overflow on the elements count (so the caller does not have to
140 * check.)
141 */
142 if (add_elements <= INT_MAX - old_elements)
143 {
144 png_voidp new_array = png_malloc_array_checked(png_ptr,
145 old_elements+add_elements, element_size);
146
147 if (new_array != NULL)
148 {
149 /* Because png_malloc_array worked the size calculations below cannot
150 * overflow.
151 */
152 if (old_elements > 0)
153 memcpy(new_array, old_array, element_size*(unsigned)old_elements);
154
155 memset((char*)new_array + element_size*(unsigned)old_elements, 0,
156 element_size*(unsigned)add_elements);
157
158 return new_array;
159 }
160 }
161
162 return NULL; /* error */
163}
164#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
165
166/* 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 separate
168 * function png_malloc_default is also provided.
169 */
170PNG_FUNCTION(png_voidp,PNGAPI
171png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
172{
173 png_voidp ret;
174
175 if (png_ptr == NULL)
176 return NULL;
177
178 ret = png_malloc_base(png_ptr, size);
179
180 if (ret == NULL)
181 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
182
183 return ret;
184}
185
186#ifdef PNG_USER_MEM_SUPPORTED
187PNG_FUNCTION(png_voidp,PNGAPI
188png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
189 PNG_ALLOCATED PNG_DEPRECATED)
190{
191 png_voidp ret;
192
193 if (png_ptr == NULL)
194 return NULL;
195
196 /* Passing 'NULL' here bypasses the application provided memory handler. */
197 ret = png_malloc_base(NULL/*use malloc*/, size);
198
199 if (ret == NULL)
200 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
201
202 return ret;
203}
204#endif /* USER_MEM */
205
206/* 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 a
208 * png_error, if it fails to allocate the requested memory.
209 */
210PNG_FUNCTION(png_voidp,PNGAPI
211png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
212 PNG_ALLOCATED)
213{
214 if (png_ptr != NULL)
215 {
216 png_voidp ret = png_malloc_base(png_ptr, size);
217
218 if (ret != NULL)
219 return ret;
220
221 png_warning(png_ptr, "Out of memory");
222 }
223
224 return NULL;
225}
226
227/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
228 * without taking any action.
229 */
230void PNGAPI
231png_free(png_const_structrp png_ptr, png_voidp ptr)
232{
233 if (png_ptr == NULL || ptr == NULL)
234 return;
235
236#ifdef PNG_USER_MEM_SUPPORTED
237 if (png_ptr->free_fn != NULL)
238 png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
239
240 else
241 png_free_default(png_ptr, ptr);
242}
243
244PNG_FUNCTION(void,PNGAPI
245png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
246{
247 if (png_ptr == NULL || ptr == NULL)
248 return;
249#endif /* USER_MEM */
250
251 free(ptr);
252}
253
254#ifdef PNG_USER_MEM_SUPPORTED
255/* This function is called when the application wants to use another method
256 * of allocating and freeing memory.
257 */
258void PNGAPI
259png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
260 malloc_fn, png_free_ptr free_fn)
261{
262 if (png_ptr != NULL)
263 {
264 png_ptr->mem_ptr = mem_ptr;
265 png_ptr->malloc_fn = malloc_fn;
266 png_ptr->free_fn = free_fn;
267 }
268}
269
270/* This function returns a pointer to the mem_ptr associated with the user
271 * functions. The application should free any memory associated with this
272 * pointer before png_write_destroy and png_read_destroy are called.
273 */
274png_voidp PNGAPI
275png_get_mem_ptr(png_const_structrp png_ptr)
276{
277 if (png_ptr == NULL)
278 return NULL;
279
280 return png_ptr->mem_ptr;
281}
282#endif /* USER_MEM */
283#endif /* READ || WRITE */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette