Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 2 | /* pngmem.c - stub functions for memory allocation |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 13831bc | 2011-12-21 08:28:28 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2011 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 13 | * This file provides a location for all memory allocation. Users who |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 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. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 18 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 19 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 20 | #include "pngpriv.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 21 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 22 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 23 | /* Allocate memory for a png_struct or a png_info. The malloc and |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 24 | memset can be replaced by a single call to calloc() if this is thought |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 25 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 26 | PNG_FUNCTION(png_voidp /* PRIVATE */, |
| 27 | png_create_struct,(int type),PNG_ALLOCATED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 28 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 29 | # ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 30 | return (png_create_struct_2(type, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* Allocate memory for a png_struct or a png_info. The malloc and |
| 34 | memset can be replaced by a single call to calloc() if this is thought |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 35 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 36 | PNG_FUNCTION(png_voidp /* PRIVATE */, |
| 37 | png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr), |
| 38 | PNG_ALLOCATED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 39 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 40 | # endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 41 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 42 | png_voidp struct_ptr; |
| 43 | |
| 44 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 45 | size = png_sizeof(png_info); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 46 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 47 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 48 | size = png_sizeof(png_struct); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 49 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 50 | else |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 51 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 52 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 53 | # ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 54 | if (malloc_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 55 | { |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 56 | png_struct dummy_struct; |
| 57 | png_structp png_ptr = &dummy_struct; |
| 58 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 59 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 60 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 61 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 62 | png_memset(struct_ptr, 0, size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 63 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 64 | return (struct_ptr); |
| 65 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 66 | # endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 67 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 68 | # if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 69 | struct_ptr = (png_voidp)farmalloc(size); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 70 | # else |
| 71 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 72 | struct_ptr = (png_voidp)halloc(size, 1); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 73 | # else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 74 | struct_ptr = (png_voidp)malloc(size); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 75 | # endif |
| 76 | # endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 77 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 78 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 79 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 80 | |
| 81 | return (struct_ptr); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 86 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 87 | png_destroy_struct(png_voidp struct_ptr) |
| 88 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 89 | # ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 90 | png_destroy_struct_2(struct_ptr, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 94 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 95 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 96 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 97 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 98 | # endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 99 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 100 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 101 | # ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 102 | if (free_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 103 | { |
| 104 | png_struct dummy_struct; |
| 105 | png_structp png_ptr = &dummy_struct; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 106 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 107 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 108 | return; |
| 109 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 110 | # endif /* PNG_USER_MEM_SUPPORTED */ |
| 111 | # if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 112 | farfree(struct_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 113 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 114 | # else |
| 115 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 116 | hfree(struct_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 117 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 118 | # else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 119 | free(struct_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 120 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 121 | # endif |
| 122 | # endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 123 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 126 | /* Allocate memory. For reasonable files, size should never exceed |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 127 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 128 | * it not to. See zconf.h and png.h for more information. zlib does |
| 129 | * need to allocate exactly 64K, so whatever you call here must |
| 130 | * have the ability to do that. |
| 131 | */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 132 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 133 | PNG_FUNCTION(png_voidp,PNGAPI |
| 134 | png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 135 | { |
| 136 | png_voidp ret; |
| 137 | |
| 138 | ret = (png_malloc(png_ptr, size)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 139 | |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 140 | if (ret != NULL) |
| 141 | png_memset(ret,0,(png_size_t)size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 142 | |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 143 | return (ret); |
| 144 | } |
| 145 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 146 | PNG_FUNCTION(png_voidp,PNGAPI |
| 147 | png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 148 | { |
| 149 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 150 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 151 | # ifdef PNG_USER_MEM_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 152 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 153 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 154 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 155 | if (png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 156 | ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 157 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 158 | else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 159 | ret = (png_malloc_default(png_ptr, size)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 160 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 161 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 162 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 163 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 164 | return (ret); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 165 | } |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 166 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 167 | PNG_FUNCTION(png_voidp,PNGAPI |
| 168 | png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 169 | { |
| 170 | png_voidp ret; |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 171 | # endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 172 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 173 | if (png_ptr == NULL || size == 0) |
| 174 | return (NULL); |
| 175 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 176 | # ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 177 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 178 | { |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 179 | # ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 180 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 181 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 182 | |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 183 | else |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 184 | # endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 187 | # endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 188 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 189 | /* Check for overflow */ |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 190 | # if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 191 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 192 | if (size != (unsigned long)size) |
| 193 | ret = NULL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 194 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 195 | else |
| 196 | ret = farmalloc(size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 197 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 198 | # else |
| 199 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 200 | if (size != (unsigned long)size) |
| 201 | ret = NULL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 202 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 203 | else |
| 204 | ret = halloc(size, 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 205 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 206 | # else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 207 | if (size != (size_t)size) |
| 208 | ret = NULL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 209 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 210 | else |
| 211 | ret = malloc((size_t)size); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 212 | # endif |
| 213 | # endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 214 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 215 | # ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 216 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 217 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 218 | # endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 219 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 220 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 223 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 224 | * without taking any action. |
| 225 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 226 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 227 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 228 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 229 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 230 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 231 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 232 | # ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 233 | if (png_ptr->free_fn != NULL) |
| 234 | { |
| 235 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 236 | return; |
| 237 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 238 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 239 | else |
| 240 | png_free_default(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 241 | } |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 242 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 243 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 244 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 245 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 246 | if (png_ptr == NULL || ptr == NULL) |
| 247 | return; |
| 248 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 249 | # endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 250 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 251 | # if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 252 | farfree(ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 253 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 254 | # else |
| 255 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 256 | hfree(ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 257 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 258 | # else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 259 | free(ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 260 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 261 | # endif |
| 262 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 263 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 264 | |
Glenn Randers-Pehrson | 2ae022d | 2002-07-01 22:23:46 -0500 | [diff] [blame] | 265 | /* This function was added at libpng version 1.2.3. The png_malloc_warn() |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 266 | * function will set up png_malloc() to issue a png_warning and return NULL |
| 267 | * instead of issuing a png_error, if it fails to allocate the requested |
| 268 | * memory. |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 269 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 270 | PNG_FUNCTION(png_voidp,PNGAPI |
| 271 | png_malloc_warn,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 272 | { |
| 273 | png_voidp ptr; |
Glenn Randers-Pehrson | ae4bd5c | 2006-11-16 20:35:49 -0600 | [diff] [blame] | 274 | png_uint_32 save_flags; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 275 | if (png_ptr == NULL) |
| 276 | return (NULL); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 277 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 278 | save_flags = png_ptr->flags; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 279 | png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 280 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); |
| 281 | png_ptr->flags=save_flags; |
| 282 | return(ptr); |
| 283 | } |
| 284 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 285 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 286 | #ifdef PNG_USER_MEM_SUPPORTED |
| 287 | /* This function is called when the application wants to use another method |
| 288 | * of allocating and freeing memory. |
| 289 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 290 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 291 | png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
| 292 | malloc_fn, png_free_ptr free_fn) |
| 293 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 294 | if (png_ptr != NULL) |
| 295 | { |
| 296 | png_ptr->mem_ptr = mem_ptr; |
| 297 | png_ptr->malloc_fn = malloc_fn; |
| 298 | png_ptr->free_fn = free_fn; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 299 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* This function returns a pointer to the mem_ptr associated with the user |
| 303 | * functions. The application should free any memory associated with this |
| 304 | * pointer before png_write_destroy and png_read_destroy are called. |
| 305 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 306 | png_voidp PNGAPI |
John Bowler | 0a5c9c0 | 2011-01-22 17:36:34 -0600 | [diff] [blame] | 307 | png_get_mem_ptr(png_const_structp png_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 308 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 309 | if (png_ptr == NULL) |
| 310 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 311 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 312 | return ((png_voidp)png_ptr->mem_ptr); |
| 313 | } |
| 314 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 315 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |