blob: b9b3efb442282e62f11bb4c003dd21cf05b8f873 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngmem.c - stub functions for memory allocation
3 *
Chris Craikb50c2172013-07-29 15:28:30 -07004 * Last changed in libpng 1.6.0 [February 14, 2013]
5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * 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 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * 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
Chris Craikb50c2172013-07-29 15:28:30 -070020#include "pngpriv.h"
21
The Android Open Source Project893912b2009-03-03 19:30:05 -080022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Chris Craikb50c2172013-07-29 15:28:30 -070023/* Free a png_struct */
The Android Open Source Project893912b2009-03-03 19:30:05 -080024void /* PRIVATE */
Chris Craikb50c2172013-07-29 15:28:30 -070025png_destroy_png_struct(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -080026{
Chris Craikb50c2172013-07-29 15:28:30 -070027 if (png_ptr != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -080028 {
Chris Craikb50c2172013-07-29 15:28:30 -070029 /* png_free might call png_error and may certainly call
30 * png_get_mem_ptr, so fake a temporary png_struct to support this.
31 */
32 png_struct dummy_struct = *png_ptr;
33 memset(png_ptr, 0, (sizeof *png_ptr));
34 png_free(&dummy_struct, png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -080035
Chris Craikb50c2172013-07-29 15:28:30 -070036# ifdef PNG_SETJMP_SUPPORTED
37 /* We may have a jmp_buf left to deallocate. */
38 png_free_jmpbuf(&dummy_struct);
39# endif
The Android Open Source Project893912b2009-03-03 19:30:05 -080040 }
41}
42
43/* Allocate memory. For reasonable files, size should never exceed
Patrick Scotta0bb96c2009-07-22 11:50:02 -040044 * 64K. However, zlib may allocate more then 64K if you don't tell
45 * it not to. See zconf.h and png.h for more information. zlib does
46 * need to allocate exactly 64K, so whatever you call here must
47 * have the ability to do that.
48 */
Chris Craikb50c2172013-07-29 15:28:30 -070049PNG_FUNCTION(png_voidp,PNGAPI
50png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Patrick Scott5f6bd842010-06-28 16:55:16 -040051{
52 png_voidp ret;
53
Chris Craikb50c2172013-07-29 15:28:30 -070054 ret = png_malloc(png_ptr, size);
55
Patrick Scott5f6bd842010-06-28 16:55:16 -040056 if (ret != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -070057 memset(ret, 0, size);
58
59 return ret;
Patrick Scott5f6bd842010-06-28 16:55:16 -040060}
The Android Open Source Project893912b2009-03-03 19:30:05 -080061
Chris Craikb50c2172013-07-29 15:28:30 -070062/* 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 */,
68png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
69 PNG_ALLOCATED)
The Android Open Source Project893912b2009-03-03 19:30:05 -080070{
Chris Craikb50c2172013-07-29 15:28:30 -070071 /* 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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -080076#ifdef PNG_USER_MEM_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -070077 PNG_UNUSED(png_ptr)
78#endif
79 if (size > 0 && size <= PNG_SIZE_MAX
80# ifdef PNG_MAX_MALLOC_64K
81 && size <= 65536U
82# endif
83 )
The Android Open Source Project893912b2009-03-03 19:30:05 -080084 {
Chris Craikb50c2172013-07-29 15:28:30 -070085#ifdef PNG_USER_MEM_SUPPORTED
86 if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
87 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
88
The Android Open Source Project893912b2009-03-03 19:30:05 -080089 else
90#endif
Chris Craikb50c2172013-07-29 15:28:30 -070091 return malloc((size_t)size); /* checked for truncation above */
The Android Open Source Project893912b2009-03-03 19:30:05 -080092 }
The Android Open Source Project893912b2009-03-03 19:30:05 -080093
Patrick Scotta0bb96c2009-07-22 11:50:02 -040094 else
Chris Craikb50c2172013-07-29 15:28:30 -070095 return NULL;
96}
The Android Open Source Project893912b2009-03-03 19:30:05 -080097
Chris Craikb50c2172013-07-29 15:28:30 -070098/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
99 * that arises because of the checks in png_realloc_array that are repeated in
100 * png_malloc_array.
101 */
102static png_voidp
103png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
104 size_t element_size)
105{
106 png_alloc_size_t req = nelements; /* known to be > 0 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800107
Chris Craikb50c2172013-07-29 15:28:30 -0700108 if (req <= PNG_SIZE_MAX/element_size)
109 return png_malloc_base(png_ptr, req * element_size);
110
111 /* The failure case when the request is too large */
112 return NULL;
113}
114
115PNG_FUNCTION(png_voidp /* PRIVATE */,
116png_malloc_array,(png_const_structrp png_ptr, int nelements,
117 size_t element_size),PNG_ALLOCATED)
118{
119 if (nelements <= 0 || element_size == 0)
120 png_error(png_ptr, "internal error: array alloc");
121
122 return png_malloc_array_checked(png_ptr, nelements, element_size);
123}
124
125PNG_FUNCTION(png_voidp /* PRIVATE */,
126png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
127 int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
128{
129 /* These are internal errors: */
130 if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
131 (old_array == NULL && old_elements > 0))
132 png_error(png_ptr, "internal error: array realloc");
133
134 /* Check for overflow on the elements count (so the caller does not have to
135 * check.)
136 */
137 if (add_elements <= INT_MAX - old_elements)
138 {
139 png_voidp new_array = png_malloc_array_checked(png_ptr,
140 old_elements+add_elements, element_size);
141
142 if (new_array != NULL)
143 {
144 /* Because png_malloc_array worked the size calculations below cannot
145 * overflow.
146 */
147 if (old_elements > 0)
148 memcpy(new_array, old_array, element_size*(unsigned)old_elements);
149
150 memset((char*)new_array + element_size*(unsigned)old_elements, 0,
151 element_size*(unsigned)add_elements);
152
153 return new_array;
154 }
155 }
156
157 return NULL; /* error */
158}
159
160/* Various functions that have different error handling are derived from this.
161 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
162 * function png_malloc_default is also provided.
163 */
164PNG_FUNCTION(png_voidp,PNGAPI
165png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
166{
167 png_voidp ret;
168
169 if (png_ptr == NULL)
170 return NULL;
171
172 ret = png_malloc_base(png_ptr, size);
173
174 if (ret == NULL)
175 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
176
177 return ret;
178}
179
180#ifdef PNG_USER_MEM_SUPPORTED
181PNG_FUNCTION(png_voidp,PNGAPI
182png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
183 PNG_ALLOCATED PNG_DEPRECATED)
184{
185 png_voidp ret;
186
187 if (png_ptr == NULL)
188 return NULL;
189
190 /* Passing 'NULL' here bypasses the application provided memory handler. */
191 ret = png_malloc_base(NULL/*use malloc*/, size);
192
193 if (ret == NULL)
194 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
195
196 return ret;
197}
198#endif /* PNG_USER_MEM_SUPPORTED */
199
200/* This function was added at libpng version 1.2.3. The png_malloc_warn()
201 * function will issue a png_warning and return NULL instead of issuing a
202 * png_error, if it fails to allocate the requested memory.
203 */
204PNG_FUNCTION(png_voidp,PNGAPI
205png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
206 PNG_ALLOCATED)
207{
208 if (png_ptr != NULL)
209 {
210 png_voidp ret = png_malloc_base(png_ptr, size);
211
212 if (ret != NULL)
213 return ret;
214
215 png_warning(png_ptr, "Out of memory");
216 }
217
218 return NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800219}
220
221/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400222 * without taking any action.
223 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800224void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700225png_free(png_const_structrp png_ptr, png_voidp ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800226{
227 if (png_ptr == NULL || ptr == NULL)
228 return;
229
230#ifdef PNG_USER_MEM_SUPPORTED
231 if (png_ptr->free_fn != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700232 png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
233
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400234 else
235 png_free_default(png_ptr, ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800236}
Chris Craikb50c2172013-07-29 15:28:30 -0700237
238PNG_FUNCTION(void,PNGAPI
239png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800240{
241 if (png_ptr == NULL || ptr == NULL)
242 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800243#endif /* PNG_USER_MEM_SUPPORTED */
244
The Android Open Source Project893912b2009-03-03 19:30:05 -0800245 free(ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800246}
247
248#ifdef PNG_USER_MEM_SUPPORTED
249/* This function is called when the application wants to use another method
250 * of allocating and freeing memory.
251 */
252void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700253png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
The Android Open Source Project893912b2009-03-03 19:30:05 -0800254 malloc_fn, png_free_ptr free_fn)
255{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700256 if (png_ptr != NULL)
257 {
258 png_ptr->mem_ptr = mem_ptr;
259 png_ptr->malloc_fn = malloc_fn;
260 png_ptr->free_fn = free_fn;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800261 }
262}
263
264/* This function returns a pointer to the mem_ptr associated with the user
265 * functions. The application should free any memory associated with this
266 * pointer before png_write_destroy and png_read_destroy are called.
267 */
268png_voidp PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700269png_get_mem_ptr(png_const_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800270{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400271 if (png_ptr == NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700272 return NULL;
273
274 return png_ptr->mem_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800275}
276#endif /* PNG_USER_MEM_SUPPORTED */
277#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */