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