1 |
william |
31 |
|
2 |
|
|
/* pngmem.c - stub functions for memory allocation |
3 |
|
|
* |
4 |
|
|
* Last changed in libpng 1.2.37 [June 4, 2009] |
5 |
|
|
* Copyright (c) 1998-2009 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 |
|
|
#define PNG_INTERNAL |
21 |
|
|
#include "png.h" |
22 |
|
|
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
23 |
|
|
|
24 |
|
|
/* Borland DOS special memory handler */ |
25 |
|
|
#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) |
26 |
|
|
/* If you change this, be sure to change the one in png.h also */ |
27 |
|
|
|
28 |
|
|
/* Allocate memory for a png_struct. The malloc and memset can be replaced |
29 |
|
|
by a single call to calloc() if this is thought to improve performance. */ |
30 |
|
|
png_voidp /* PRIVATE */ |
31 |
|
|
png_create_struct(int type) |
32 |
|
|
{ |
33 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
34 |
|
|
return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
/* Alternate version of png_create_struct, for use with user-defined malloc. */ |
38 |
|
|
png_voidp /* PRIVATE */ |
39 |
|
|
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
40 |
|
|
{ |
41 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
42 |
|
|
png_size_t size; |
43 |
|
|
png_voidp struct_ptr; |
44 |
|
|
|
45 |
|
|
if (type == PNG_STRUCT_INFO) |
46 |
|
|
size = png_sizeof(png_info); |
47 |
|
|
else if (type == PNG_STRUCT_PNG) |
48 |
|
|
size = png_sizeof(png_struct); |
49 |
|
|
else |
50 |
|
|
return (png_get_copyright(NULL)); |
51 |
|
|
|
52 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
53 |
|
|
if (malloc_fn != NULL) |
54 |
|
|
{ |
55 |
|
|
png_struct dummy_struct; |
56 |
|
|
png_structp png_ptr = &dummy_struct; |
57 |
|
|
png_ptr->mem_ptr=mem_ptr; |
58 |
|
|
struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); |
59 |
|
|
} |
60 |
|
|
else |
61 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
62 |
|
|
struct_ptr = (png_voidp)farmalloc(size); |
63 |
|
|
if (struct_ptr != NULL) |
64 |
|
|
png_memset(struct_ptr, 0, size); |
65 |
|
|
return (struct_ptr); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/* Free memory allocated by a png_create_struct() call */ |
69 |
|
|
void /* PRIVATE */ |
70 |
|
|
png_destroy_struct(png_voidp struct_ptr) |
71 |
|
|
{ |
72 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
73 |
|
|
png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/* Free memory allocated by a png_create_struct() call */ |
77 |
|
|
void /* PRIVATE */ |
78 |
|
|
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
79 |
|
|
png_voidp mem_ptr) |
80 |
|
|
{ |
81 |
|
|
#endif |
82 |
|
|
if (struct_ptr != NULL) |
83 |
|
|
{ |
84 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
85 |
|
|
if (free_fn != NULL) |
86 |
|
|
{ |
87 |
|
|
png_struct dummy_struct; |
88 |
|
|
png_structp png_ptr = &dummy_struct; |
89 |
|
|
png_ptr->mem_ptr=mem_ptr; |
90 |
|
|
(*(free_fn))(png_ptr, struct_ptr); |
91 |
|
|
return; |
92 |
|
|
} |
93 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
94 |
|
|
farfree (struct_ptr); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/* Allocate memory. For reasonable files, size should never exceed |
99 |
|
|
* 64K. However, zlib may allocate more then 64K if you don't tell |
100 |
|
|
* it not to. See zconf.h and png.h for more information. zlib does |
101 |
|
|
* need to allocate exactly 64K, so whatever you call here must |
102 |
|
|
* have the ability to do that. |
103 |
|
|
* |
104 |
|
|
* Borland seems to have a problem in DOS mode for exactly 64K. |
105 |
|
|
* It gives you a segment with an offset of 8 (perhaps to store its |
106 |
|
|
* memory stuff). zlib doesn't like this at all, so we have to |
107 |
|
|
* detect and deal with it. This code should not be needed in |
108 |
|
|
* Windows or OS/2 modes, and only in 16 bit mode. This code has |
109 |
|
|
* been updated by Alexander Lehmann for version 0.89 to waste less |
110 |
|
|
* memory. |
111 |
|
|
* |
112 |
|
|
* Note that we can't use png_size_t for the "size" declaration, |
113 |
|
|
* since on some systems a png_size_t is a 16-bit quantity, and as a |
114 |
|
|
* result, we would be truncating potentially larger memory requests |
115 |
|
|
* (which should cause a fatal error) and introducing major problems. |
116 |
|
|
*/ |
117 |
|
|
|
118 |
|
|
png_voidp PNGAPI |
119 |
|
|
png_malloc(png_structp png_ptr, png_uint_32 size) |
120 |
|
|
{ |
121 |
|
|
png_voidp ret; |
122 |
|
|
|
123 |
|
|
if (png_ptr == NULL || size == 0) |
124 |
|
|
return (NULL); |
125 |
|
|
|
126 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
127 |
|
|
if (png_ptr->malloc_fn != NULL) |
128 |
|
|
ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
129 |
|
|
else |
130 |
|
|
ret = (png_malloc_default(png_ptr, size)); |
131 |
|
|
if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
132 |
|
|
png_error(png_ptr, "Out of memory!"); |
133 |
|
|
return (ret); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
png_voidp PNGAPI |
137 |
|
|
png_malloc_default(png_structp png_ptr, png_uint_32 size) |
138 |
|
|
{ |
139 |
|
|
png_voidp ret; |
140 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
141 |
|
|
|
142 |
|
|
if (png_ptr == NULL || size == 0) |
143 |
|
|
return (NULL); |
144 |
|
|
|
145 |
|
|
#ifdef PNG_MAX_MALLOC_64K |
146 |
|
|
if (size > (png_uint_32)65536L) |
147 |
|
|
{ |
148 |
|
|
png_warning(png_ptr, "Cannot Allocate > 64K"); |
149 |
|
|
ret = NULL; |
150 |
|
|
} |
151 |
|
|
else |
152 |
|
|
#endif |
153 |
|
|
|
154 |
|
|
if (size != (size_t)size) |
155 |
|
|
ret = NULL; |
156 |
|
|
else if (size == (png_uint_32)65536L) |
157 |
|
|
{ |
158 |
|
|
if (png_ptr->offset_table == NULL) |
159 |
|
|
{ |
160 |
|
|
/* Try to see if we need to do any of this fancy stuff */ |
161 |
|
|
ret = farmalloc(size); |
162 |
|
|
if (ret == NULL || ((png_size_t)ret & 0xffff)) |
163 |
|
|
{ |
164 |
|
|
int num_blocks; |
165 |
|
|
png_uint_32 total_size; |
166 |
|
|
png_bytep table; |
167 |
|
|
int i; |
168 |
|
|
png_byte huge * hptr; |
169 |
|
|
|
170 |
|
|
if (ret != NULL) |
171 |
|
|
{ |
172 |
|
|
farfree(ret); |
173 |
|
|
ret = NULL; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
if (png_ptr->zlib_window_bits > 14) |
177 |
|
|
num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
178 |
|
|
else |
179 |
|
|
num_blocks = 1; |
180 |
|
|
if (png_ptr->zlib_mem_level >= 7) |
181 |
|
|
num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
182 |
|
|
else |
183 |
|
|
num_blocks++; |
184 |
|
|
|
185 |
|
|
total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
186 |
|
|
|
187 |
|
|
table = farmalloc(total_size); |
188 |
|
|
|
189 |
|
|
if (table == NULL) |
190 |
|
|
{ |
191 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
192 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
193 |
|
|
png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ |
194 |
|
|
else |
195 |
|
|
png_warning(png_ptr, "Out Of Memory."); |
196 |
|
|
#endif |
197 |
|
|
return (NULL); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
if ((png_size_t)table & 0xfff0) |
201 |
|
|
{ |
202 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
203 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
204 |
|
|
png_error(png_ptr, |
205 |
|
|
"Farmalloc didn't return normalized pointer"); |
206 |
|
|
else |
207 |
|
|
png_warning(png_ptr, |
208 |
|
|
"Farmalloc didn't return normalized pointer"); |
209 |
|
|
#endif |
210 |
|
|
return (NULL); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
png_ptr->offset_table = table; |
214 |
|
|
png_ptr->offset_table_ptr = farmalloc(num_blocks * |
215 |
|
|
png_sizeof(png_bytep)); |
216 |
|
|
|
217 |
|
|
if (png_ptr->offset_table_ptr == NULL) |
218 |
|
|
{ |
219 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
220 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
221 |
|
|
png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */ |
222 |
|
|
else |
223 |
|
|
png_warning(png_ptr, "Out Of memory."); |
224 |
|
|
#endif |
225 |
|
|
return (NULL); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
hptr = (png_byte huge *)table; |
229 |
|
|
if ((png_size_t)hptr & 0xf) |
230 |
|
|
{ |
231 |
|
|
hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
232 |
|
|
hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ |
233 |
|
|
} |
234 |
|
|
for (i = 0; i < num_blocks; i++) |
235 |
|
|
{ |
236 |
|
|
png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
237 |
|
|
hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
png_ptr->offset_table_number = num_blocks; |
241 |
|
|
png_ptr->offset_table_count = 0; |
242 |
|
|
png_ptr->offset_table_count_free = 0; |
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
247 |
|
|
{ |
248 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
249 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
250 |
|
|
png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ |
251 |
|
|
else |
252 |
|
|
png_warning(png_ptr, "Out of Memory."); |
253 |
|
|
#endif |
254 |
|
|
return (NULL); |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
258 |
|
|
} |
259 |
|
|
else |
260 |
|
|
ret = farmalloc(size); |
261 |
|
|
|
262 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
263 |
|
|
if (ret == NULL) |
264 |
|
|
{ |
265 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
266 |
|
|
png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ |
267 |
|
|
else |
268 |
|
|
png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */ |
269 |
|
|
} |
270 |
|
|
#endif |
271 |
|
|
|
272 |
|
|
return (ret); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
/* Free a pointer allocated by png_malloc(). In the default |
276 |
|
|
* configuration, png_ptr is not used, but is passed in case it |
277 |
|
|
* is needed. If ptr is NULL, return without taking any action. |
278 |
|
|
*/ |
279 |
|
|
void PNGAPI |
280 |
|
|
png_free(png_structp png_ptr, png_voidp ptr) |
281 |
|
|
{ |
282 |
|
|
if (png_ptr == NULL || ptr == NULL) |
283 |
|
|
return; |
284 |
|
|
|
285 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
286 |
|
|
if (png_ptr->free_fn != NULL) |
287 |
|
|
{ |
288 |
|
|
(*(png_ptr->free_fn))(png_ptr, ptr); |
289 |
|
|
return; |
290 |
|
|
} |
291 |
|
|
else |
292 |
|
|
png_free_default(png_ptr, ptr); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
void PNGAPI |
296 |
|
|
png_free_default(png_structp png_ptr, png_voidp ptr) |
297 |
|
|
{ |
298 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
299 |
|
|
|
300 |
|
|
if (png_ptr == NULL || ptr == NULL) |
301 |
|
|
return; |
302 |
|
|
|
303 |
|
|
if (png_ptr->offset_table != NULL) |
304 |
|
|
{ |
305 |
|
|
int i; |
306 |
|
|
|
307 |
|
|
for (i = 0; i < png_ptr->offset_table_count; i++) |
308 |
|
|
{ |
309 |
|
|
if (ptr == png_ptr->offset_table_ptr[i]) |
310 |
|
|
{ |
311 |
|
|
ptr = NULL; |
312 |
|
|
png_ptr->offset_table_count_free++; |
313 |
|
|
break; |
314 |
|
|
} |
315 |
|
|
} |
316 |
|
|
if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
317 |
|
|
{ |
318 |
|
|
farfree(png_ptr->offset_table); |
319 |
|
|
farfree(png_ptr->offset_table_ptr); |
320 |
|
|
png_ptr->offset_table = NULL; |
321 |
|
|
png_ptr->offset_table_ptr = NULL; |
322 |
|
|
} |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
if (ptr != NULL) |
326 |
|
|
{ |
327 |
|
|
farfree(ptr); |
328 |
|
|
} |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
#else /* Not the Borland DOS special memory handler */ |
332 |
|
|
|
333 |
|
|
/* Allocate memory for a png_struct or a png_info. The malloc and |
334 |
|
|
memset can be replaced by a single call to calloc() if this is thought |
335 |
|
|
to improve performance noticably. */ |
336 |
|
|
png_voidp /* PRIVATE */ |
337 |
|
|
png_create_struct(int type) |
338 |
|
|
{ |
339 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
340 |
|
|
return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
/* Allocate memory for a png_struct or a png_info. The malloc and |
344 |
|
|
memset can be replaced by a single call to calloc() if this is thought |
345 |
|
|
to improve performance noticably. */ |
346 |
|
|
png_voidp /* PRIVATE */ |
347 |
|
|
png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
348 |
|
|
{ |
349 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
350 |
|
|
png_size_t size; |
351 |
|
|
png_voidp struct_ptr; |
352 |
|
|
|
353 |
|
|
if (type == PNG_STRUCT_INFO) |
354 |
|
|
size = png_sizeof(png_info); |
355 |
|
|
else if (type == PNG_STRUCT_PNG) |
356 |
|
|
size = png_sizeof(png_struct); |
357 |
|
|
else |
358 |
|
|
return (NULL); |
359 |
|
|
|
360 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
361 |
|
|
if (malloc_fn != NULL) |
362 |
|
|
{ |
363 |
|
|
png_struct dummy_struct; |
364 |
|
|
png_structp png_ptr = &dummy_struct; |
365 |
|
|
png_ptr->mem_ptr=mem_ptr; |
366 |
|
|
struct_ptr = (*(malloc_fn))(png_ptr, size); |
367 |
|
|
if (struct_ptr != NULL) |
368 |
|
|
png_memset(struct_ptr, 0, size); |
369 |
|
|
return (struct_ptr); |
370 |
|
|
} |
371 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
372 |
|
|
|
373 |
|
|
#if defined(__TURBOC__) && !defined(__FLAT__) |
374 |
|
|
struct_ptr = (png_voidp)farmalloc(size); |
375 |
|
|
#else |
376 |
|
|
# if defined(_MSC_VER) && defined(MAXSEG_64K) |
377 |
|
|
struct_ptr = (png_voidp)halloc(size, 1); |
378 |
|
|
# else |
379 |
|
|
struct_ptr = (png_voidp)malloc(size); |
380 |
|
|
# endif |
381 |
|
|
#endif |
382 |
|
|
if (struct_ptr != NULL) |
383 |
|
|
png_memset(struct_ptr, 0, size); |
384 |
|
|
|
385 |
|
|
return (struct_ptr); |
386 |
|
|
} |
387 |
|
|
|
388 |
|
|
|
389 |
|
|
/* Free memory allocated by a png_create_struct() call */ |
390 |
|
|
void /* PRIVATE */ |
391 |
|
|
png_destroy_struct(png_voidp struct_ptr) |
392 |
|
|
{ |
393 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
394 |
|
|
png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
/* Free memory allocated by a png_create_struct() call */ |
398 |
|
|
void /* PRIVATE */ |
399 |
|
|
png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
400 |
|
|
png_voidp mem_ptr) |
401 |
|
|
{ |
402 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
403 |
|
|
if (struct_ptr != NULL) |
404 |
|
|
{ |
405 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
406 |
|
|
if (free_fn != NULL) |
407 |
|
|
{ |
408 |
|
|
png_struct dummy_struct; |
409 |
|
|
png_structp png_ptr = &dummy_struct; |
410 |
|
|
png_ptr->mem_ptr=mem_ptr; |
411 |
|
|
(*(free_fn))(png_ptr, struct_ptr); |
412 |
|
|
return; |
413 |
|
|
} |
414 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
415 |
|
|
#if defined(__TURBOC__) && !defined(__FLAT__) |
416 |
|
|
farfree(struct_ptr); |
417 |
|
|
#else |
418 |
|
|
# if defined(_MSC_VER) && defined(MAXSEG_64K) |
419 |
|
|
hfree(struct_ptr); |
420 |
|
|
# else |
421 |
|
|
free(struct_ptr); |
422 |
|
|
# endif |
423 |
|
|
#endif |
424 |
|
|
} |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
/* Allocate memory. For reasonable files, size should never exceed |
428 |
|
|
* 64K. However, zlib may allocate more then 64K if you don't tell |
429 |
|
|
* it not to. See zconf.h and png.h for more information. zlib does |
430 |
|
|
* need to allocate exactly 64K, so whatever you call here must |
431 |
|
|
* have the ability to do that. |
432 |
|
|
*/ |
433 |
|
|
|
434 |
|
|
|
435 |
|
|
png_voidp PNGAPI |
436 |
|
|
png_malloc(png_structp png_ptr, png_uint_32 size) |
437 |
|
|
{ |
438 |
|
|
png_voidp ret; |
439 |
|
|
|
440 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
441 |
|
|
if (png_ptr == NULL || size == 0) |
442 |
|
|
return (NULL); |
443 |
|
|
|
444 |
|
|
if (png_ptr->malloc_fn != NULL) |
445 |
|
|
ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
446 |
|
|
else |
447 |
|
|
ret = (png_malloc_default(png_ptr, size)); |
448 |
|
|
if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
449 |
|
|
png_error(png_ptr, "Out of Memory!"); |
450 |
|
|
return (ret); |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
png_voidp PNGAPI |
454 |
|
|
png_malloc_default(png_structp png_ptr, png_uint_32 size) |
455 |
|
|
{ |
456 |
|
|
png_voidp ret; |
457 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
458 |
|
|
|
459 |
|
|
if (png_ptr == NULL || size == 0) |
460 |
|
|
return (NULL); |
461 |
|
|
|
462 |
|
|
#ifdef PNG_MAX_MALLOC_64K |
463 |
|
|
if (size > (png_uint_32)65536L) |
464 |
|
|
{ |
465 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
466 |
|
|
if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
467 |
|
|
png_error(png_ptr, "Cannot Allocate > 64K"); |
468 |
|
|
else |
469 |
|
|
#endif |
470 |
|
|
return NULL; |
471 |
|
|
} |
472 |
|
|
#endif |
473 |
|
|
|
474 |
|
|
/* Check for overflow */ |
475 |
|
|
#if defined(__TURBOC__) && !defined(__FLAT__) |
476 |
|
|
if (size != (unsigned long)size) |
477 |
|
|
ret = NULL; |
478 |
|
|
else |
479 |
|
|
ret = farmalloc(size); |
480 |
|
|
#else |
481 |
|
|
# if defined(_MSC_VER) && defined(MAXSEG_64K) |
482 |
|
|
if (size != (unsigned long)size) |
483 |
|
|
ret = NULL; |
484 |
|
|
else |
485 |
|
|
ret = halloc(size, 1); |
486 |
|
|
# else |
487 |
|
|
if (size != (size_t)size) |
488 |
|
|
ret = NULL; |
489 |
|
|
else |
490 |
|
|
ret = malloc((size_t)size); |
491 |
|
|
# endif |
492 |
|
|
#endif |
493 |
|
|
|
494 |
|
|
#ifndef PNG_USER_MEM_SUPPORTED |
495 |
|
|
if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
496 |
|
|
png_error(png_ptr, "Out of Memory"); |
497 |
|
|
#endif |
498 |
|
|
|
499 |
|
|
return (ret); |
500 |
|
|
} |
501 |
|
|
|
502 |
|
|
/* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
503 |
|
|
* without taking any action. |
504 |
|
|
*/ |
505 |
|
|
void PNGAPI |
506 |
|
|
png_free(png_structp png_ptr, png_voidp ptr) |
507 |
|
|
{ |
508 |
|
|
if (png_ptr == NULL || ptr == NULL) |
509 |
|
|
return; |
510 |
|
|
|
511 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
512 |
|
|
if (png_ptr->free_fn != NULL) |
513 |
|
|
{ |
514 |
|
|
(*(png_ptr->free_fn))(png_ptr, ptr); |
515 |
|
|
return; |
516 |
|
|
} |
517 |
|
|
else |
518 |
|
|
png_free_default(png_ptr, ptr); |
519 |
|
|
} |
520 |
|
|
void PNGAPI |
521 |
|
|
png_free_default(png_structp png_ptr, png_voidp ptr) |
522 |
|
|
{ |
523 |
|
|
if (png_ptr == NULL || ptr == NULL) |
524 |
|
|
return; |
525 |
|
|
|
526 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
527 |
|
|
|
528 |
|
|
#if defined(__TURBOC__) && !defined(__FLAT__) |
529 |
|
|
farfree(ptr); |
530 |
|
|
#else |
531 |
|
|
# if defined(_MSC_VER) && defined(MAXSEG_64K) |
532 |
|
|
hfree(ptr); |
533 |
|
|
# else |
534 |
|
|
free(ptr); |
535 |
|
|
# endif |
536 |
|
|
#endif |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
#endif /* Not Borland DOS special memory handler */ |
540 |
|
|
|
541 |
|
|
#if defined(PNG_1_0_X) |
542 |
|
|
# define png_malloc_warn png_malloc |
543 |
|
|
#else |
544 |
|
|
/* This function was added at libpng version 1.2.3. The png_malloc_warn() |
545 |
|
|
* function will set up png_malloc() to issue a png_warning and return NULL |
546 |
|
|
* instead of issuing a png_error, if it fails to allocate the requested |
547 |
|
|
* memory. |
548 |
|
|
*/ |
549 |
|
|
png_voidp PNGAPI |
550 |
|
|
png_malloc_warn(png_structp png_ptr, png_uint_32 size) |
551 |
|
|
{ |
552 |
|
|
png_voidp ptr; |
553 |
|
|
png_uint_32 save_flags; |
554 |
|
|
if (png_ptr == NULL) |
555 |
|
|
return (NULL); |
556 |
|
|
|
557 |
|
|
save_flags = png_ptr->flags; |
558 |
|
|
png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
559 |
|
|
ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); |
560 |
|
|
png_ptr->flags=save_flags; |
561 |
|
|
return(ptr); |
562 |
|
|
} |
563 |
|
|
#endif |
564 |
|
|
|
565 |
|
|
png_voidp PNGAPI |
566 |
|
|
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, |
567 |
|
|
png_uint_32 length) |
568 |
|
|
{ |
569 |
|
|
png_size_t size; |
570 |
|
|
|
571 |
|
|
size = (png_size_t)length; |
572 |
|
|
if ((png_uint_32)size != length) |
573 |
|
|
png_error(png_ptr, "Overflow in png_memcpy_check."); |
574 |
|
|
|
575 |
|
|
return(png_memcpy (s1, s2, size)); |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
png_voidp PNGAPI |
579 |
|
|
png_memset_check (png_structp png_ptr, png_voidp s1, int value, |
580 |
|
|
png_uint_32 length) |
581 |
|
|
{ |
582 |
|
|
png_size_t size; |
583 |
|
|
|
584 |
|
|
size = (png_size_t)length; |
585 |
|
|
if ((png_uint_32)size != length) |
586 |
|
|
png_error(png_ptr, "Overflow in png_memset_check."); |
587 |
|
|
|
588 |
|
|
return (png_memset (s1, value, size)); |
589 |
|
|
|
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
#ifdef PNG_USER_MEM_SUPPORTED |
593 |
|
|
/* This function is called when the application wants to use another method |
594 |
|
|
* of allocating and freeing memory. |
595 |
|
|
*/ |
596 |
|
|
void PNGAPI |
597 |
|
|
png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
598 |
|
|
malloc_fn, png_free_ptr free_fn) |
599 |
|
|
{ |
600 |
|
|
if (png_ptr != NULL) |
601 |
|
|
{ |
602 |
|
|
png_ptr->mem_ptr = mem_ptr; |
603 |
|
|
png_ptr->malloc_fn = malloc_fn; |
604 |
|
|
png_ptr->free_fn = free_fn; |
605 |
|
|
} |
606 |
|
|
} |
607 |
|
|
|
608 |
|
|
/* This function returns a pointer to the mem_ptr associated with the user |
609 |
|
|
* functions. The application should free any memory associated with this |
610 |
|
|
* pointer before png_write_destroy and png_read_destroy are called. |
611 |
|
|
*/ |
612 |
|
|
png_voidp PNGAPI |
613 |
|
|
png_get_mem_ptr(png_structp png_ptr) |
614 |
|
|
{ |
615 |
|
|
if (png_ptr == NULL) |
616 |
|
|
return (NULL); |
617 |
|
|
return ((png_voidp)png_ptr->mem_ptr); |
618 |
|
|
} |
619 |
|
|
#endif /* PNG_USER_MEM_SUPPORTED */ |
620 |
|
|
#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |