annotate lib/libpng/pngmem.c @ 2575:a76d408c5132 tip

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