blob: b9b3efb442282e62f11bb4c003dd21cf05b8f873 [file] [log] [blame]
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001
Guy Schalnat0f716451995-11-28 11:22:13 -06002/* pngmem.c - stub functions for memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004 * Last changed in libpng 1.6.0 [February 14, 2013]
5 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050013 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050014 * 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.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060018 */
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050020#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
John Bowlerd332c672011-12-21 17:36:12 -060023/* Free a png_struct */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060025png_destroy_png_struct(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -050026{
John Bowlerd332c672011-12-21 17:36:12 -060027 if (png_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060028 {
John Bowlerd332c672011-12-21 17:36:12 -060029 /* 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;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060033 memset(png_ptr, 0, (sizeof *png_ptr));
John Bowlerd332c672011-12-21 17:36:12 -060034 png_free(&dummy_struct, png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050035
John Bowlerd332c672011-12-21 17:36:12 -060036# ifdef PNG_SETJMP_SUPPORTED
37 /* We may have a jmp_buf left to deallocate. */
38 png_free_jmpbuf(&dummy_struct);
39# endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060040 }
Guy Schalnate5a37791996-06-05 15:50:50 -050041}
42
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060043/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050044 * 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 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050049PNG_FUNCTION(png_voidp,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060050png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060051{
52 png_voidp ret;
53
John Bowlerd332c672011-12-21 17:36:12 -060054 ret = png_malloc(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050055
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060056 if (ret != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060057 memset(ret, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050058
John Bowlerd332c672011-12-21 17:36:12 -060059 return ret;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060060}
61
John Bowlerd332c672011-12-21 17:36:12 -060062/* 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 */,
John Bowler5d567862011-12-24 09:12:00 -060068png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
John Bowler40b26032011-12-22 08:09:15 -060069 PNG_ALLOCATED)
John Bowlerd332c672011-12-21 17:36:12 -060070{
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 */
Glenn Randers-Pehrson3c1c9532011-12-21 18:11:51 -060076#ifdef PNG_USER_MEM_SUPPORTED
77 PNG_UNUSED(png_ptr)
78#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060079 if (size > 0 && size <= PNG_SIZE_MAX
John Bowlerd332c672011-12-21 17:36:12 -060080# ifdef PNG_MAX_MALLOC_64K
81 && size <= 65536U
82# endif
83 )
84 {
Glenn Randers-Pehrson3c1c9532011-12-21 18:11:51 -060085#ifdef PNG_USER_MEM_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -060086 if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
John Bowler5d567862011-12-24 09:12:00 -060087 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
John Bowlerd332c672011-12-21 17:36:12 -060088
89 else
Glenn Randers-Pehrson3c1c9532011-12-21 18:11:51 -060090#endif
John Bowlerd332c672011-12-21 17:36:12 -060091 return malloc((size_t)size); /* checked for truncation above */
92 }
93
94 else
95 return NULL;
96}
97
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060098/* 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 */
107
108 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
John Bowlerd332c672011-12-21 17:36:12 -0600160/* 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 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500164PNG_FUNCTION(png_voidp,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600165png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600166{
167 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500168
John Bowlerd332c672011-12-21 17:36:12 -0600169 if (png_ptr == NULL)
170 return NULL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171
John Bowlerd332c672011-12-21 17:36:12 -0600172 ret = png_malloc_base(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500173
John Bowlerd332c672011-12-21 17:36:12 -0600174 if (ret == NULL)
175 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500176
John Bowlerd332c672011-12-21 17:36:12 -0600177 return ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500178}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500179
John Bowlerd332c672011-12-21 17:36:12 -0600180#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500181PNG_FUNCTION(png_voidp,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600182png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
John Bowlerd332c672011-12-21 17:36:12 -0600183 PNG_ALLOCATED PNG_DEPRECATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500184{
185 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500186
John Bowlerd332c672011-12-21 17:36:12 -0600187 if (png_ptr == NULL)
188 return NULL;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500189
John Bowlerd332c672011-12-21 17:36:12 -0600190 /* 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
John Bowler5d567862011-12-24 09:12:00 -0600205png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
John Bowler40b26032011-12-22 08:09:15 -0600206 PNG_ALLOCATED)
John Bowlerd332c672011-12-21 17:36:12 -0600207{
208 if (png_ptr != NULL)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600209 {
John Bowlerd332c672011-12-21 17:36:12 -0600210 png_voidp ret = png_malloc_base(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500211
John Bowlerd332c672011-12-21 17:36:12 -0600212 if (ret != NULL)
213 return ret;
214
215 png_warning(png_ptr, "Out of memory");
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600216 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600217
John Bowlerd332c672011-12-21 17:36:12 -0600218 return NULL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219}
220
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500221/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500222 * without taking any action.
223 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500224void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600225png_free(png_const_structrp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600226{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600228 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500229
John Bowlerd332c672011-12-21 17:36:12 -0600230#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500231 if (png_ptr->free_fn != NULL)
John Bowler5d567862011-12-24 09:12:00 -0600232 png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500233
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500234 else
235 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500236}
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500237
John Bowlerd332c672011-12-21 17:36:12 -0600238PNG_FUNCTION(void,PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600239png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500240{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500241 if (png_ptr == NULL || ptr == NULL)
242 return;
John Bowlerd332c672011-12-21 17:36:12 -0600243#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500244
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500245 free(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500246}
Guy Schalnat6d764711995-12-19 03:22:19 -0600247
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500248#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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500252void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600253png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500254 malloc_fn, png_free_ptr free_fn)
255{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500256 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;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600261 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500262}
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500268png_voidp PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600269png_get_mem_ptr(png_const_structrp png_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500270{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500271 if (png_ptr == NULL)
John Bowlerd332c672011-12-21 17:36:12 -0600272 return NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500273
John Bowlerd332c672011-12-21 17:36:12 -0600274 return png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500275}
276#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600277#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */