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