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