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 | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 4 | * libpng 1.2.2beta2 - February 24, 2002 |
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 |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 6 | * Copyright (c) 1998-2002 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 | * (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] | 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. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 26 | png_voidp /* PRIVATE */ |
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 |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 30 | return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* Alternate version of png_create_struct, for use with user-defined malloc. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 34 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 35 | png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 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 | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 46 | return (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 | { |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 51 | png_struct dummy_struct; |
| 52 | png_structp png_ptr = &dummy_struct; |
| 53 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 54 | struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 55 | } |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 56 | else |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 57 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 58 | struct_ptr = (png_voidp)farmalloc(size)); |
| 59 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 60 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 61 | return (struct_ptr); |
| 62 | } |
| 63 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 64 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 65 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 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 |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 69 | png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 73 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 74 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 75 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 76 | { |
| 77 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 78 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 79 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 80 | #ifdef PNG_USER_MEM_SUPPORTED |
| 81 | if(free_fn != NULL) |
| 82 | { |
| 83 | png_struct dummy_struct; |
| 84 | png_structp png_ptr = &dummy_struct; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 85 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 86 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 90 | farfree (struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 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 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 113 | png_voidp PNGAPI |
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 | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 120 | return (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) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 124 | { |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 125 | ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 126 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 127 | png_error(png_ptr, "Out of memory!"); |
| 128 | return (ret); |
| 129 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 130 | else |
| 131 | return png_malloc_default(png_ptr, size); |
| 132 | } |
| 133 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 134 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 135 | png_malloc_default(png_structp png_ptr, png_uint_32 size) |
| 136 | { |
| 137 | png_voidp ret; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 138 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 139 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 140 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 141 | if (size > (png_uint_32)65536L) |
| 142 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 143 | #endif |
| 144 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 145 | if (size == (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 146 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 147 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 148 | { |
| 149 | /* try to see if we need to do any of this fancy stuff */ |
| 150 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 151 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 152 | { |
| 153 | int num_blocks; |
| 154 | png_uint_32 total_size; |
| 155 | png_bytep table; |
| 156 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 157 | png_byte huge * hptr; |
| 158 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 159 | if (ret != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 160 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 161 | farfree(ret); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 162 | ret = NULL; |
| 163 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 164 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 165 | if(png_ptr->zlib_window_bits > 14) |
| 166 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 167 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 168 | num_blocks = 1; |
| 169 | if (png_ptr->zlib_mem_level >= 7) |
| 170 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 171 | else |
| 172 | num_blocks++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 173 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 174 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 175 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 176 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 177 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 178 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 179 | { |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 180 | if (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 181 | png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ |
| 182 | else |
| 183 | png_warning(png_ptr, "Out Of Memory."); |
| 184 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 185 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 186 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 187 | if ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 188 | { |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 189 | if (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 190 | png_error(png_ptr, |
| 191 | "Farmalloc didn't return normalized pointer"); |
| 192 | else |
| 193 | png_warning(png_ptr, |
| 194 | "Farmalloc didn't return normalized pointer"); |
| 195 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 196 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 197 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 198 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 199 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
| 200 | sizeof (png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 201 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 202 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 203 | { |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 204 | if (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 205 | png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */ |
| 206 | else |
| 207 | png_warning(png_ptr, "Out Of memory."); |
| 208 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 212 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 213 | { |
| 214 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 215 | hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 216 | } |
| 217 | for (i = 0; i < num_blocks; i++) |
| 218 | { |
| 219 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 220 | hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 221 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 222 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 223 | png_ptr->offset_table_number = num_blocks; |
| 224 | png_ptr->offset_table_count = 0; |
| 225 | png_ptr->offset_table_count_free = 0; |
| 226 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 227 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 228 | |
| 229 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 230 | { |
| 231 | if (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 232 | png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ |
| 233 | else |
| 234 | png_warning(png_ptr, "Out of Memory."); |
| 235 | return (NULL); |
| 236 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 237 | |
| 238 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 242 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 243 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 244 | { |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 245 | if (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 246 | png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ |
| 247 | else |
| 248 | png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 251 | return (ret); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 254 | /* free a pointer allocated by png_malloc(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 255 | configuration, png_ptr is not used, but is passed in case it |
| 256 | is needed. If ptr is NULL, return without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 257 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 258 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 259 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 260 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 261 | return; |
| 262 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 263 | #ifdef PNG_USER_MEM_SUPPORTED |
| 264 | if (png_ptr->free_fn != NULL) |
| 265 | { |
| 266 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | else png_free_default(png_ptr, ptr); |
| 270 | } |
| 271 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 272 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 273 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 274 | { |
| 275 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 276 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 278 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 279 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 280 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 281 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 282 | { |
| 283 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 284 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 285 | ptr = NULL; |
| 286 | png_ptr->offset_table_count_free++; |
| 287 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 288 | } |
| 289 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 290 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 291 | { |
| 292 | farfree(png_ptr->offset_table); |
| 293 | farfree(png_ptr->offset_table_ptr); |
| 294 | png_ptr->offset_table = NULL; |
| 295 | png_ptr->offset_table_ptr = NULL; |
| 296 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 297 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 298 | |
| 299 | if (ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 300 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 301 | farfree(ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 302 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #else /* Not the Borland DOS special memory handler */ |
| 306 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 307 | /* 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] | 308 | memset can be replaced by a single call to calloc() if this is thought |
| 309 | to improve performance noticably.*/ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 310 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 311 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 312 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 313 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 314 | return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* Allocate memory for a png_struct or a png_info. The malloc and |
| 318 | memset can be replaced by a single call to calloc() if this is thought |
| 319 | to improve performance noticably.*/ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 320 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 321 | png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 322 | { |
| 323 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 324 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 325 | png_voidp struct_ptr; |
| 326 | |
| 327 | if (type == PNG_STRUCT_INFO) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 328 | size = sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 329 | else if (type == PNG_STRUCT_PNG) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 330 | size = sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 331 | else |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 332 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 333 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 334 | #ifdef PNG_USER_MEM_SUPPORTED |
| 335 | if(malloc_fn != NULL) |
| 336 | { |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 337 | png_struct dummy_struct; |
| 338 | png_structp png_ptr = &dummy_struct; |
| 339 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 340 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 341 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 342 | png_memset(struct_ptr, 0, size); |
| 343 | return (struct_ptr); |
| 344 | } |
| 345 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 346 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 347 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 348 | if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) |
| 349 | #else |
| 350 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 351 | if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 352 | # else |
| 353 | if ((struct_ptr = (png_voidp)malloc(size)) != NULL) |
| 354 | # endif |
| 355 | #endif |
| 356 | { |
| 357 | png_memset(struct_ptr, 0, size); |
| 358 | } |
| 359 | |
| 360 | return (struct_ptr); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 365 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 366 | png_destroy_struct(png_voidp struct_ptr) |
| 367 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 368 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 369 | png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 373 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 374 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 375 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 376 | { |
| 377 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 378 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 379 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 380 | #ifdef PNG_USER_MEM_SUPPORTED |
| 381 | if(free_fn != NULL) |
| 382 | { |
| 383 | png_struct dummy_struct; |
| 384 | png_structp png_ptr = &dummy_struct; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 385 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 386 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 390 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 391 | farfree(struct_ptr); |
| 392 | #else |
| 393 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 394 | hfree(struct_ptr); |
| 395 | # else |
| 396 | free(struct_ptr); |
| 397 | # endif |
| 398 | #endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 399 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 403 | /* Allocate memory. For reasonable files, size should never exceed |
| 404 | 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] | 405 | 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] | 406 | need to allocate exactly 64K, so whatever you call here must |
| 407 | have the ability to do that. */ |
| 408 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 409 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 410 | png_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 411 | { |
| 412 | png_voidp ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 413 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 414 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 415 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 416 | #ifdef PNG_USER_MEM_SUPPORTED |
| 417 | if(png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 418 | { |
| 419 | ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size)); |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 420 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 421 | png_error(png_ptr, "Out of Memory!"); |
| 422 | return (ret); |
| 423 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 424 | else |
| 425 | return (png_malloc_default(png_ptr, size)); |
| 426 | } |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 427 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 428 | png_malloc_default(png_structp png_ptr, png_uint_32 size) |
| 429 | { |
| 430 | png_voidp ret; |
| 431 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 432 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 433 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 434 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 435 | { |
| 436 | if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 437 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 438 | else |
| 439 | return NULL; |
| 440 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 441 | #endif |
| 442 | |
| 443 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 444 | ret = farmalloc(size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 445 | #else |
| 446 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 447 | ret = halloc(size, 1); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 448 | # else |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 449 | ret = malloc((size_t)size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 450 | # endif |
| 451 | #endif |
| 452 | |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 453 | 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] | 454 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 455 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 456 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 457 | } |
| 458 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 459 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
| 460 | without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 461 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 462 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 463 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 464 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 465 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 466 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 467 | #ifdef PNG_USER_MEM_SUPPORTED |
| 468 | if (png_ptr->free_fn != NULL) |
| 469 | { |
| 470 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 471 | return; |
| 472 | } |
| 473 | else png_free_default(png_ptr, ptr); |
| 474 | } |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 475 | void /* PRIVATE */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 476 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 477 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 478 | if (png_ptr == NULL || ptr == NULL) |
| 479 | return; |
| 480 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 481 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 482 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 483 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 484 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 485 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 486 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 487 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 488 | # else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 489 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 490 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 491 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 494 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 495 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 496 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 497 | 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] | 498 | png_uint_32 length) |
| 499 | { |
| 500 | png_size_t size; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 501 | |
| 502 | size = (png_size_t)length; |
| 503 | if ((png_uint_32)size != length) |
| 504 | png_error(png_ptr,"Overflow in png_memcpy_check."); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 505 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 506 | return(png_memcpy (s1, s2, size)); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 507 | } |
| 508 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 509 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 510 | 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] | 511 | png_uint_32 length) |
| 512 | { |
| 513 | png_size_t size; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 514 | |
| 515 | size = (png_size_t)length; |
| 516 | if ((png_uint_32)size != length) |
| 517 | png_error(png_ptr,"Overflow in png_memset_check."); |
| 518 | |
| 519 | return (png_memset (s1, value, size)); |
| 520 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 521 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 522 | |
| 523 | #ifdef PNG_USER_MEM_SUPPORTED |
| 524 | /* This function is called when the application wants to use another method |
| 525 | * of allocating and freeing memory. |
| 526 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 527 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 528 | png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
| 529 | malloc_fn, png_free_ptr free_fn) |
| 530 | { |
| 531 | png_ptr->mem_ptr = mem_ptr; |
| 532 | png_ptr->malloc_fn = malloc_fn; |
| 533 | png_ptr->free_fn = free_fn; |
| 534 | } |
| 535 | |
| 536 | /* This function returns a pointer to the mem_ptr associated with the user |
| 537 | * functions. The application should free any memory associated with this |
| 538 | * pointer before png_write_destroy and png_read_destroy are called. |
| 539 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 540 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 541 | png_get_mem_ptr(png_structp png_ptr) |
| 542 | { |
| 543 | return ((png_voidp)png_ptr->mem_ptr); |
| 544 | } |
| 545 | #endif /* PNG_USER_MEM_SUPPORTED */ |