blob: 5c6733f4bbd31f0f4f4954857716eacb662f068c [file] [log] [blame]
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001
Guy Schalnat0f716451995-11-28 11:22:13 -06002/* pngmem.c - stub functions for memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05004 * libpng 1.2.4beta2 - June 20, 2002
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06006 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (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-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050010 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050011 * 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-Pehrsonb6ce43d1998-01-01 07:13:13 -060015 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
17#define PNG_INTERNAL
18#include "png.h"
19
Guy Schalnat4ee97b01996-01-16 01:51:56 -060020/* 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 Schalnate5a37791996-06-05 15:50:50 -050024/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060025 by a single call to calloc() if this is thought to improve performance. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050026png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060027png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050028{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050029#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050030 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050031}
32
33/* Alternate version of png_create_struct, for use with user-defined malloc. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050034png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050035png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050036{
37#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050039 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-Pehrson73d57cb2002-03-25 18:49:08 -060046 return (png_get_copyright());
Guy Schalnate5a37791996-06-05 15:50:50 -050047
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050048#ifdef PNG_USER_MEM_SUPPORTED
49 if(malloc_fn != NULL)
50 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050051 png_struct dummy_struct;
52 png_structp png_ptr = &dummy_struct;
53 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -060054 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050055 }
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050056 else
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050057#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050058 struct_ptr = (png_voidp)farmalloc(size));
59 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050060 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050061 return (struct_ptr);
62}
63
Guy Schalnate5a37791996-06-05 15:50:50 -050064/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050065void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050066png_destroy_struct(png_voidp struct_ptr)
67{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050068#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050069 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050070}
71
72/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050073void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050074png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
75 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050076{
77#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060079 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050080#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-Pehrson8b6a8892001-05-18 04:54:50 -050085 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050086 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050087 return;
88 }
89#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050090 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060091 }
Guy Schalnate5a37791996-06-05 15:50:50 -050092}
93
Guy Schalnat0d580581995-07-20 02:43:20 -050094/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 * 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-Pehrson8686fff1998-05-21 09:27:50 -0500101 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 * 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-Pehrson07748d12002-05-25 11:12:10 -0500113
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500114png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500115png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600116{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600117 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500118
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500120 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600121
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500122#ifdef PNG_USER_MEM_SUPPORTED
123 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500124 {
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600125 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600126 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500127 png_error(png_ptr, "Out of memory!");
128 return (ret);
129 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500130 else
131 return png_malloc_default(png_ptr, size);
132}
133
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500134png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500135png_malloc_default(png_structp png_ptr, png_uint_32 size)
136{
137 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600138#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500139
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600140#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 if (size > (png_uint_32)65536L)
142 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600143#endif
144
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600145 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600146 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600148 {
149 /* try to see if we need to do any of this fancy stuff */
150 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600152 {
153 int num_blocks;
154 png_uint_32 total_size;
155 png_bytep table;
156 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600157 png_byte huge * hptr;
158
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600160 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600161 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600162 ret = NULL;
163 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600164
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500165 if(png_ptr->zlib_window_bits > 14)
166 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
167 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600168 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 Schalnat4ee97b01996-01-16 01:51:56 -0600173
Guy Schalnate5a37791996-06-05 15:50:50 -0500174 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600175
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600176 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600177
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 {
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600180 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 Schalnatb2e01bd1996-01-26 01:38:47 -0600185 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600186
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 {
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600189 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 Schalnatb2e01bd1996-01-26 01:38:47 -0600196 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600197
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 png_ptr->offset_table_ptr = farmalloc(num_blocks *
200 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500201
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 {
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600204 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 Schalnate5a37791996-06-05 15:50:50 -0500209 }
210
211 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500213 {
214 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500215 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600216 }
217 for (i = 0; i < num_blocks; i++)
218 {
219 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500220 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600221 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600222
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600223 png_ptr->offset_table_number = num_blocks;
224 png_ptr->offset_table_count = 0;
225 png_ptr->offset_table_count_free = 0;
226 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500228
229 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600230 {
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 Schalnate5a37791996-06-05 15:50:50 -0500237
238 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600239 }
240 else
241 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500242
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500243 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 {
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600245 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 Schalnat0d580581995-07-20 02:43:20 -0500249 }
250
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600251 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500252}
253
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500254/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600255 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-Pehrson75294572000-05-06 14:09:57 -0500257void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500258png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500259{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600261 return;
262
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500263#ifdef PNG_USER_MEM_SUPPORTED
264 if (png_ptr->free_fn != NULL)
265 {
266 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500267 return;
268 }
269 else png_free_default(png_ptr, ptr);
270}
271
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500272void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500273png_free_default(png_structp png_ptr, png_voidp ptr)
274{
275#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600276
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600278 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500279 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600280
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281 for (i = 0; i < png_ptr->offset_table_count; i++)
282 {
283 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600284 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500285 ptr = NULL;
286 png_ptr->offset_table_count_free++;
287 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600288 }
289 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290 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 Schalnatb2e01bd1996-01-26 01:38:47 -0600297 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298
299 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600300 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600302 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600303}
304
305#else /* Not the Borland DOS special memory handler */
306
Guy Schalnate5a37791996-06-05 15:50:50 -0500307/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600308 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500309 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500310png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600311png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500312{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500313#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500314 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500315}
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
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500319 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500320png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500321png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500322{
323#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500325 png_voidp struct_ptr;
326
327 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500329 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500330 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500331 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500332 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500333
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500334#ifdef PNG_USER_MEM_SUPPORTED
335 if(malloc_fn != NULL)
336 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600337 png_struct dummy_struct;
338 png_structp png_ptr = &dummy_struct;
339 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600340 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500341 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500342 png_memset(struct_ptr, 0, size);
343 return (struct_ptr);
344 }
345#endif /* PNG_USER_MEM_SUPPORTED */
346
Guy Schalnate5a37791996-06-05 15:50:50 -0500347#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 Schalnatc21f90c1996-06-17 16:24:45 -0500351 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500352# 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-Pehrson75294572000-05-06 14:09:57 -0500365void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500366png_destroy_struct(png_voidp struct_ptr)
367{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500368#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500369 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500370}
371
372/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500373void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500374png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
375 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500376{
377#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600379 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500380#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-Pehrson8b6a8892001-05-18 04:54:50 -0500385 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500386 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500387 return;
388 }
389#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500390#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-Pehrson46f61e21998-01-30 21:45:12 -0600399 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500400}
401
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600402/* Allocate memory. For reasonable files, size should never exceed
403 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600405 need to allocate exactly 64K, so whatever you call here must
406 have the ability to do that. */
407
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500408png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500409png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600410{
411 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500412
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500414 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600415
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500416#ifdef PNG_USER_MEM_SUPPORTED
417 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500418 {
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500419 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600420 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500421 png_error(png_ptr, "Out of Memory!");
422 return (ret);
423 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424 else
425 return (png_malloc_default(png_ptr, size));
426}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500427
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600428png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500429png_malloc_default(png_structp png_ptr, png_uint_32 size)
430{
431 png_voidp ret;
432#endif /* PNG_USER_MEM_SUPPORTED */
433
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600434#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600435 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600436 {
437 if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
438 png_error(png_ptr, "Cannot Allocate > 64K");
439 else
440 return NULL;
441 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600442#endif
443
444#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600445 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600446#else
447# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600448 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600449# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600450 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451# endif
452#endif
453
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600454 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455 png_error(png_ptr, "Out of Memory");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600456
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600457 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458}
459
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500460/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
461 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500462void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500463png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600464{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500465 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600466 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500467
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500468#ifdef PNG_USER_MEM_SUPPORTED
469 if (png_ptr->free_fn != NULL)
470 {
471 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500472 return;
473 }
474 else png_free_default(png_ptr, ptr);
475}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600476void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500477png_free_default(png_structp png_ptr, png_voidp ptr)
478{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500479 if (png_ptr == NULL || ptr == NULL)
480 return;
481
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500482#endif /* PNG_USER_MEM_SUPPORTED */
483
Guy Schalnat6d764711995-12-19 03:22:19 -0600484#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500485 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500486#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600487# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600489# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600491# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500492#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500493}
494
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600495#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600496
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500497/* This function was added at libpng version 1.3.0. The png_malloc_warn()
498 * function will issue a png_warning and return NULL instead of issuing a
499 * png_error, if it fails to allocate the requested memory.
500 */
501png_voidp PNGAPI
502png_malloc_warn(png_structp png_ptr, png_uint_32 size)
503{
504 png_voidp ptr;
505 png_uint_32 save_flags=png_ptr->flags;
506
507 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
508 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
509 png_ptr->flags=save_flags;
510 return(ptr);
511}
512
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600513png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600514png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600515 png_uint_32 length)
516{
517 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600518
519 size = (png_size_t)length;
520 if ((png_uint_32)size != length)
521 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600522
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600523 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600524}
525
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600526png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600527png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600528 png_uint_32 length)
529{
530 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600531
532 size = (png_size_t)length;
533 if ((png_uint_32)size != length)
534 png_error(png_ptr,"Overflow in png_memset_check.");
535
536 return (png_memset (s1, value, size));
537
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600538}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500539
540#ifdef PNG_USER_MEM_SUPPORTED
541/* This function is called when the application wants to use another method
542 * of allocating and freeing memory.
543 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500544void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500545png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
546 malloc_fn, png_free_ptr free_fn)
547{
548 png_ptr->mem_ptr = mem_ptr;
549 png_ptr->malloc_fn = malloc_fn;
550 png_ptr->free_fn = free_fn;
551}
552
553/* This function returns a pointer to the mem_ptr associated with the user
554 * functions. The application should free any memory associated with this
555 * pointer before png_write_destroy and png_read_destroy are called.
556 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500557png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500558png_get_mem_ptr(png_structp png_ptr)
559{
560 return ((png_voidp)png_ptr->mem_ptr);
561}
562#endif /* PNG_USER_MEM_SUPPORTED */