CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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