ReactOS 0.4.16-dev-2613-g9533ad7
pngmem.c
Go to the documentation of this file.
1/* pngmem.c - stub functions for memory allocation
2 *
3 * Copyright (c) 2018-2025 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 */
51{
53
54 ret = png_malloc(png_ptr, size);
55
56 if (ret != NULL)
57 memset(ret, 0, size);
58
59 return ret;
60}
61
62/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
63 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
64 * Checking and error handling must happen outside this routine; it returns NULL
65 * if the allocation cannot be done (for any reason.)
66 */
67PNG_FUNCTION(png_voidp /* PRIVATE */,
70{
71 /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
72 * allocators have also been removed in 1.6.0, so any 16-bit system now has
73 * to implement a user memory handler. This checks to be sure it isn't
74 * called with big numbers.
75 */
76# ifdef PNG_MAX_MALLOC_64K
77 /* This is support for legacy systems which had segmented addressing
78 * limiting the maximum allocation size to 65536. It takes precedence
79 * over PNG_SIZE_MAX which is set to 65535 on true 16-bit systems.
80 *
81 * TODO: libpng-1.8: finally remove both cases.
82 */
83 if (size > 65536U) return NULL;
84# endif
85
86 /* This is checked too because the system malloc call below takes a (size_t).
87 */
88 if (size > PNG_SIZE_MAX) return NULL;
89
90# ifdef PNG_USER_MEM_SUPPORTED
91 if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
92 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
93# else
95# endif
96
97 /* Use the system malloc */
98 return malloc((size_t)/*SAFE*/size); /* checked for truncation above */
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),
124{
125 if (nelements <= 0 || element_size == 0)
126 png_error(png_ptr, "internal error: array alloc");
127
128 return png_malloc_array_checked(png_ptr, nelements, element_size);
129}
130
131PNG_FUNCTION(png_voidp /* PRIVATE */,
132png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
133 int old_elements, int add_elements, size_t element_size),
135{
136 /* These are internal errors: */
137 if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
138 (old_array == NULL && old_elements > 0))
139 png_error(png_ptr, "internal error: array realloc");
140
141 /* Check for overflow on the elements count (so the caller does not have to
142 * check.)
143 */
144 if (add_elements <= INT_MAX - old_elements)
145 {
146 png_voidp new_array = png_malloc_array_checked(png_ptr,
147 old_elements+add_elements, element_size);
148
149 if (new_array != NULL)
150 {
151 /* Because png_malloc_array worked the size calculations below cannot
152 * overflow.
153 */
154 if (old_elements > 0)
155 memcpy(new_array, old_array, element_size*(unsigned)old_elements);
156
157 memset((char*)new_array + element_size*(unsigned)old_elements, 0,
158 element_size*(unsigned)add_elements);
159
160 return new_array;
161 }
162 }
163
164 return NULL; /* error */
165}
166#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
167
168/* Various functions that have different error handling are derived from this.
169 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
170 * function png_malloc_default is also provided.
171 */
175{
177
178 if (png_ptr == NULL)
179 return NULL;
180
181 ret = png_malloc_base(png_ptr, size);
182
183 if (ret == NULL)
184 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
185
186 return ret;
187}
188
189#ifdef PNG_USER_MEM_SUPPORTED
191png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
193{
195
196 if (png_ptr == NULL)
197 return NULL;
198
199 /* Passing 'NULL' here bypasses the application provided memory handler. */
200 ret = png_malloc_base(NULL/*use malloc*/, size);
201
202 if (ret == NULL)
203 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
204
205 return ret;
206}
207#endif /* USER_MEM */
208
209/* This function was added at libpng version 1.2.3. The png_malloc_warn()
210 * function will issue a png_warning and return NULL instead of issuing a
211 * png_error, if it fails to allocate the requested memory.
212 */
216{
217 if (png_ptr != NULL)
218 {
219 png_voidp ret = png_malloc_base(png_ptr, size);
220
221 if (ret != NULL)
222 return ret;
223
224 png_warning(png_ptr, "Out of memory");
225 }
226
227 return NULL;
228}
229
230/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
231 * without taking any action.
232 */
233void PNGAPI
235{
236 if (png_ptr == NULL || ptr == NULL)
237 return;
238
239#ifdef PNG_USER_MEM_SUPPORTED
240 if (png_ptr->free_fn != NULL)
242
243 else
244 png_free_default(png_ptr, ptr);
245}
246
248png_free_default,(png_const_structrp png_ptr, png_voidp ptr),
250{
251 if (png_ptr == NULL || ptr == NULL)
252 return;
253#endif /* USER_MEM */
254
255 free(ptr);
256}
257
258#ifdef PNG_USER_MEM_SUPPORTED
259/* This function is called when the application wants to use another method
260 * of allocating and freeing memory.
261 */
262void PNGAPI
263png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr,
264 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
265{
266 if (png_ptr != NULL)
267 {
268 png_ptr->mem_ptr = mem_ptr;
269 png_ptr->malloc_fn = malloc_fn;
270 png_ptr->free_fn = free_fn;
271 }
272}
273
274/* This function returns a pointer to the mem_ptr associated with the user
275 * functions. The application should free any memory associated with this
276 * pointer before png_write_destroy and png_read_destroy are called.
277 */
279png_get_mem_ptr(png_const_structrp png_ptr)
280{
281 if (png_ptr == NULL)
282 return NULL;
283
284 return png_ptr->mem_ptr;
285}
286#endif /* USER_MEM */
287#endif /* READ || WRITE */
size_t const element_size
Definition: debug_heap.cpp:510
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define INT_MAX
Definition: limits.h:26
return ret
Definition: mutex.c:146
GLsizeiptr size
Definition: glext.h:5919
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static PVOID ptr
Definition: dispmode.c:27
png_structrp png_ptr
Definition: png.h:1122
#define PNG_SIZE_MAX
Definition: png.h:646
const png_struct *PNG_RESTRICT png_const_structrp
Definition: png.h:465
png_struct *PNG_RESTRICT png_structrp
Definition: png.h:464
#define PNG_FUNCTION(type, name, args, attributes)
Definition: pngconf.h:274
size_t png_alloc_size_t
Definition: pngconf.h:544
#define PNGAPI
Definition: pngconf.h:248
#define PNG_ALLOCATED
Definition: pngconf.h:426
#define PNG_UNUSED(param)
Definition: pngpriv.h:479
#define png_constcast(type, value)
Definition: pngpriv.h:536
#define PNG_DEPRECATED
Definition: pngpriv.h:373
#define memset(x, y, z)
Definition: compat.h:39