Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 2 | /* pngmem.c - stub functions for memory allocation |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | e8ef689 | 2014-11-20 09:33:25 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.15 [November 20, 2014] |
Glenn Randers-Pehrson | 4d8de33 | 2015-12-13 22:41:17 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (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-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 13 | * This file provides a location for all memory allocation. Users who |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 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. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 18 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 19 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 20 | #include "pngpriv.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 21 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 22 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 23 | /* Free a png_struct */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 24 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 25 | png_destroy_png_struct(png_structrp png_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 26 | { |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 27 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 28 | { |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 29 | /* 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-Pehrson | 871b1d0 | 2013-03-02 14:58:22 -0600 | [diff] [blame] | 33 | memset(png_ptr, 0, (sizeof *png_ptr)); |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 34 | png_free(&dummy_struct, png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 35 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 36 | # ifdef PNG_SETJMP_SUPPORTED |
| 37 | /* We may have a jmp_buf left to deallocate. */ |
| 38 | png_free_jmpbuf(&dummy_struct); |
| 39 | # endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 40 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 43 | /* Allocate memory. For reasonable files, size should never exceed |
Glenn Randers-Pehrson | bbe2be3 | 2015-03-07 13:13:11 -0600 | [diff] [blame] | 44 | * 64K. However, zlib may allocate more than 64K if you don't tell |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 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-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 49 | PNG_FUNCTION(png_voidp,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 50 | png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 51 | { |
| 52 | png_voidp ret; |
| 53 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 54 | ret = png_malloc(png_ptr, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 55 | |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 56 | if (ret != NULL) |
Glenn Randers-Pehrson | 871b1d0 | 2013-03-02 14:58:22 -0600 | [diff] [blame] | 57 | memset(ret, 0, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 58 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 59 | return ret; |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 60 | } |
| 61 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 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 | */ |
| 67 | PNG_FUNCTION(png_voidp /* PRIVATE */, |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 68 | png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 69 | PNG_ALLOCATED) |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 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 | */ |
Glenn Randers-Pehrson | c912050 | 2013-11-22 14:58:04 -0600 | [diff] [blame] | 76 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3c1c953 | 2011-12-21 18:11:51 -0600 | [diff] [blame] | 77 | PNG_UNUSED(png_ptr) |
| 78 | #endif |
Glenn Randers-Pehrson | c912050 | 2013-11-22 14:58:04 -0600 | [diff] [blame] | 79 | |
Glenn Randers-Pehrson | c861dc8 | 2015-04-01 12:06:01 -0500 | [diff] [blame] | 80 | /* Some compilers complain that this is always true. However, it |
| 81 | * can be false when integer overflow happens. |
| 82 | */ |
Glenn Randers-Pehrson | 871b1d0 | 2013-03-02 14:58:22 -0600 | [diff] [blame] | 83 | if (size > 0 && size <= PNG_SIZE_MAX |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 84 | # ifdef PNG_MAX_MALLOC_64K |
| 85 | && size <= 65536U |
| 86 | # endif |
| 87 | ) |
| 88 | { |
Glenn Randers-Pehrson | 3c1c953 | 2011-12-21 18:11:51 -0600 | [diff] [blame] | 89 | #ifdef PNG_USER_MEM_SUPPORTED |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 90 | if (png_ptr != NULL && png_ptr->malloc_fn != NULL) |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 91 | return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 92 | |
| 93 | else |
Glenn Randers-Pehrson | 3c1c953 | 2011-12-21 18:11:51 -0600 | [diff] [blame] | 94 | #endif |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 95 | return malloc((size_t)size); /* checked for truncation above */ |
| 96 | } |
| 97 | |
| 98 | else |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Glenn Randers-Pehrson | c912050 | 2013-11-22 14:58:04 -0600 | [diff] [blame] | 102 | #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ |
| 103 | defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 871b1d0 | 2013-03-02 14:58:22 -0600 | [diff] [blame] | 104 | /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7 |
| 105 | * that arises because of the checks in png_realloc_array that are repeated in |
| 106 | * png_malloc_array. |
| 107 | */ |
| 108 | static png_voidp |
| 109 | png_malloc_array_checked(png_const_structrp png_ptr, int nelements, |
| 110 | size_t element_size) |
| 111 | { |
| 112 | png_alloc_size_t req = nelements; /* known to be > 0 */ |
| 113 | |
| 114 | if (req <= PNG_SIZE_MAX/element_size) |
| 115 | return png_malloc_base(png_ptr, req * element_size); |
| 116 | |
| 117 | /* The failure case when the request is too large */ |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | PNG_FUNCTION(png_voidp /* PRIVATE */, |
| 122 | png_malloc_array,(png_const_structrp png_ptr, int nelements, |
| 123 | size_t element_size),PNG_ALLOCATED) |
| 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 | |
| 131 | PNG_FUNCTION(png_voidp /* PRIVATE */, |
| 132 | png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array, |
| 133 | int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED) |
| 134 | { |
| 135 | /* These are internal errors: */ |
| 136 | if (add_elements <= 0 || element_size == 0 || old_elements < 0 || |
| 137 | (old_array == NULL && old_elements > 0)) |
| 138 | png_error(png_ptr, "internal error: array realloc"); |
| 139 | |
| 140 | /* Check for overflow on the elements count (so the caller does not have to |
| 141 | * check.) |
| 142 | */ |
| 143 | if (add_elements <= INT_MAX - old_elements) |
| 144 | { |
| 145 | png_voidp new_array = png_malloc_array_checked(png_ptr, |
| 146 | old_elements+add_elements, element_size); |
| 147 | |
| 148 | if (new_array != NULL) |
| 149 | { |
| 150 | /* Because png_malloc_array worked the size calculations below cannot |
| 151 | * overflow. |
| 152 | */ |
| 153 | if (old_elements > 0) |
| 154 | memcpy(new_array, old_array, element_size*(unsigned)old_elements); |
| 155 | |
| 156 | memset((char*)new_array + element_size*(unsigned)old_elements, 0, |
| 157 | element_size*(unsigned)add_elements); |
| 158 | |
| 159 | return new_array; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return NULL; /* error */ |
| 164 | } |
Glenn Randers-Pehrson | c912050 | 2013-11-22 14:58:04 -0600 | [diff] [blame] | 165 | #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */ |
Glenn Randers-Pehrson | 871b1d0 | 2013-03-02 14:58:22 -0600 | [diff] [blame] | 166 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 167 | /* Various functions that have different error handling are derived from this. |
| 168 | * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate |
| 169 | * function png_malloc_default is also provided. |
| 170 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 171 | PNG_FUNCTION(png_voidp,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 172 | png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 173 | { |
| 174 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 175 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 176 | if (png_ptr == NULL) |
| 177 | return NULL; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 178 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 179 | ret = png_malloc_base(png_ptr, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 180 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 181 | if (ret == NULL) |
| 182 | png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */ |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 183 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 184 | return ret; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 185 | } |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 186 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 187 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 188 | PNG_FUNCTION(png_voidp,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 189 | png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size), |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 190 | PNG_ALLOCATED PNG_DEPRECATED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 191 | { |
| 192 | png_voidp ret; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 193 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 194 | if (png_ptr == NULL) |
| 195 | return NULL; |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 196 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 197 | /* Passing 'NULL' here bypasses the application provided memory handler. */ |
| 198 | ret = png_malloc_base(NULL/*use malloc*/, size); |
| 199 | |
| 200 | if (ret == NULL) |
| 201 | png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ |
| 202 | |
| 203 | return ret; |
| 204 | } |
Glenn Randers-Pehrson | cda68df | 2014-11-06 22:11:39 -0600 | [diff] [blame] | 205 | #endif /* USER_MEM */ |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 206 | |
| 207 | /* This function was added at libpng version 1.2.3. The png_malloc_warn() |
| 208 | * function will issue a png_warning and return NULL instead of issuing a |
| 209 | * png_error, if it fails to allocate the requested memory. |
| 210 | */ |
| 211 | PNG_FUNCTION(png_voidp,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 212 | png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 213 | PNG_ALLOCATED) |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 214 | { |
| 215 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 216 | { |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 217 | png_voidp ret = png_malloc_base(png_ptr, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 218 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 219 | if (ret != NULL) |
| 220 | return ret; |
| 221 | |
| 222 | png_warning(png_ptr, "Out of memory"); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 223 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 224 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 225 | return NULL; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 226 | } |
| 227 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 228 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 229 | * without taking any action. |
| 230 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 231 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 232 | png_free(png_const_structrp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 233 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 234 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 235 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 236 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 237 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 238 | if (png_ptr->free_fn != NULL) |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 239 | png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 240 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 241 | else |
| 242 | png_free_default(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 243 | } |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 244 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 245 | PNG_FUNCTION(void,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 246 | png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 247 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 248 | if (png_ptr == NULL || ptr == NULL) |
| 249 | return; |
Glenn Randers-Pehrson | cda68df | 2014-11-06 22:11:39 -0600 | [diff] [blame] | 250 | #endif /* USER_MEM */ |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 251 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 252 | free(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 253 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 254 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 255 | #ifdef PNG_USER_MEM_SUPPORTED |
| 256 | /* This function is called when the application wants to use another method |
| 257 | * of allocating and freeing memory. |
| 258 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 259 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 260 | png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 261 | malloc_fn, png_free_ptr free_fn) |
| 262 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 263 | if (png_ptr != NULL) |
| 264 | { |
| 265 | png_ptr->mem_ptr = mem_ptr; |
| 266 | png_ptr->malloc_fn = malloc_fn; |
| 267 | png_ptr->free_fn = free_fn; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 268 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* This function returns a pointer to the mem_ptr associated with the user |
| 272 | * functions. The application should free any memory associated with this |
| 273 | * pointer before png_write_destroy and png_read_destroy are called. |
| 274 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 275 | png_voidp PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 276 | png_get_mem_ptr(png_const_structrp png_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 277 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 278 | if (png_ptr == NULL) |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 279 | return NULL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 280 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 281 | return png_ptr->mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 282 | } |
Glenn Randers-Pehrson | cda68df | 2014-11-06 22:11:39 -0600 | [diff] [blame] | 283 | #endif /* USER_MEM */ |
| 284 | #endif /* READ || WRITE */ |