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