Debian Codegraph @

PNG/libpng/pngmem.c

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