blob: 53cccdbff170ce47f8ff63e8f7691ef4b6d2a098 [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-Pehrson3f549252001-10-27 07:35:13 -05004 * libpng 1.2.1beta3 - October 27, 2001
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -06006 * Copyright (c) 1998-2001 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-Pehrson3f549252001-10-27 07:35:13 -050046 return (NULL);
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;
54 struct_ptr = (*(malloc_fn))(png_ptr, 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-Pehrson75294572000-05-06 14:09:57 -0500113png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500114png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600115{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500116#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600117 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500118#endif
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 {
125 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
126 if (ret == NULL)
127 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-Pehrson46f61e21998-01-30 21:45:12 -0600180 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600182
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500185 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600186 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600187
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 png_ptr->offset_table_ptr = farmalloc(num_blocks *
190 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600193 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600194 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 }
196
197 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500198 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500199 {
200 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500201 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600202 }
203 for (i = 0; i < num_blocks; i++)
204 {
205 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500206 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600208
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600209 png_ptr->offset_table_number = num_blocks;
210 png_ptr->offset_table_count = 0;
211 png_ptr->offset_table_count_free = 0;
212 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600213 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500214
215 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600216 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500217
218 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 }
220 else
221 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500222
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500223 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500224 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600225 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500226 }
227
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600228 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500229}
230
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500231/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600232 configuration, png_ptr is not used, but is passed in case it
233 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500234void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500235png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500236{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600238 return;
239
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500240#ifdef PNG_USER_MEM_SUPPORTED
241 if (png_ptr->free_fn != NULL)
242 {
243 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500244 return;
245 }
246 else png_free_default(png_ptr, ptr);
247}
248
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500249void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500250png_free_default(png_structp png_ptr, png_voidp ptr)
251{
252#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600253
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600255 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600257
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 for (i = 0; i < png_ptr->offset_table_count; i++)
259 {
260 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600261 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500262 ptr = NULL;
263 png_ptr->offset_table_count_free++;
264 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600265 }
266 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
268 {
269 farfree(png_ptr->offset_table);
270 farfree(png_ptr->offset_table_ptr);
271 png_ptr->offset_table = NULL;
272 png_ptr->offset_table_ptr = NULL;
273 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600274 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275
276 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600277 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600279 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600280}
281
282#else /* Not the Borland DOS special memory handler */
283
Guy Schalnate5a37791996-06-05 15:50:50 -0500284/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600285 memset can be replaced by a single call to calloc() if this is thought
286 to improve performance noticably.*/
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500287png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600288png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500289{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500290#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500291 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500292}
293
294/* Allocate memory for a png_struct or a png_info. The malloc and
295 memset can be replaced by a single call to calloc() if this is thought
296 to improve performance noticably.*/
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500297png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500298png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500299{
300#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500302 png_voidp struct_ptr;
303
304 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500306 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500308 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500309 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500310
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500311#ifdef PNG_USER_MEM_SUPPORTED
312 if(malloc_fn != NULL)
313 {
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500314 if (mem_ptr != NULL)
315 {
316 png_struct dummy_struct;
317 png_structp png_ptr = &dummy_struct;
318 png_ptr->mem_ptr=mem_ptr;
319 struct_ptr = (*(malloc_fn))(png_ptr, size);
320 }
321 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500322 {
323 struct_ptr = (*(malloc_fn))(png_structp_NULL, size);
324 }
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500325 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500326 png_memset(struct_ptr, 0, size);
327 return (struct_ptr);
328 }
329#endif /* PNG_USER_MEM_SUPPORTED */
330
Guy Schalnate5a37791996-06-05 15:50:50 -0500331#if defined(__TURBOC__) && !defined(__FLAT__)
332 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
333#else
334# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500335 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500336# else
337 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
338# endif
339#endif
340 {
341 png_memset(struct_ptr, 0, size);
342 }
343
344 return (struct_ptr);
345}
346
347
348/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500349void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500350png_destroy_struct(png_voidp struct_ptr)
351{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500352#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500353 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500354}
355
356/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500357void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500358png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
359 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500360{
361#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600363 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500364#ifdef PNG_USER_MEM_SUPPORTED
365 if(free_fn != NULL)
366 {
367 png_struct dummy_struct;
368 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500369 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500370 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500371 return;
372 }
373#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500374#if defined(__TURBOC__) && !defined(__FLAT__)
375 farfree(struct_ptr);
376#else
377# if defined(_MSC_VER) && defined(MAXSEG_64K)
378 hfree(struct_ptr);
379# else
380 free(struct_ptr);
381# endif
382#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600383 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500384}
385
386
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600387/* Allocate memory. For reasonable files, size should never exceed
388 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600390 need to allocate exactly 64K, so whatever you call here must
391 have the ability to do that. */
392
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500393png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500394png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600395{
396 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500398 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600399
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500400#ifdef PNG_USER_MEM_SUPPORTED
401 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500402 {
403 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
404 if (ret == NULL)
405 png_error(png_ptr, "Out of Memory!");
406 return (ret);
407 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500408 else
409 return (png_malloc_default(png_ptr, size));
410}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500411png_voidp /* PRIVATE */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500412png_malloc_default(png_structp png_ptr, png_uint_32 size)
413{
414 png_voidp ret;
415#endif /* PNG_USER_MEM_SUPPORTED */
416
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600417#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600418 if (size > (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600419 png_error(png_ptr, "Cannot Allocate > 64K");
420#endif
421
422#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600423 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600424#else
425# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600426 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600428 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600429# endif
430#endif
431
432 if (ret == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600433 png_error(png_ptr, "Out of Memory");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600434
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600435 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600436}
437
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500438/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
439 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500440void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500441png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600442{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600444 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500445
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500446#ifdef PNG_USER_MEM_SUPPORTED
447 if (png_ptr->free_fn != NULL)
448 {
449 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500450 return;
451 }
452 else png_free_default(png_ptr, ptr);
453}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500454void /* PRIVATE */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500455png_free_default(png_structp png_ptr, png_voidp ptr)
456{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500457 if (png_ptr == NULL || ptr == NULL)
458 return;
459
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500460#endif /* PNG_USER_MEM_SUPPORTED */
461
Guy Schalnat6d764711995-12-19 03:22:19 -0600462#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500463 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500464#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600465# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600467# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600469# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500470#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500471}
472
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600473#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600474
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500475png_voidp /* PRIVATE */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600476png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600477 png_uint_32 length)
478{
479 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600480
481 size = (png_size_t)length;
482 if ((png_uint_32)size != length)
483 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600484
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600485 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600486}
487
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500488png_voidp /* PRIVATE */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600489png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600490 png_uint_32 length)
491{
492 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600493
494 size = (png_size_t)length;
495 if ((png_uint_32)size != length)
496 png_error(png_ptr,"Overflow in png_memset_check.");
497
498 return (png_memset (s1, value, size));
499
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600500}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500501
502#ifdef PNG_USER_MEM_SUPPORTED
503/* This function is called when the application wants to use another method
504 * of allocating and freeing memory.
505 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500506void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500507png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
508 malloc_fn, png_free_ptr free_fn)
509{
510 png_ptr->mem_ptr = mem_ptr;
511 png_ptr->malloc_fn = malloc_fn;
512 png_ptr->free_fn = free_fn;
513}
514
515/* This function returns a pointer to the mem_ptr associated with the user
516 * functions. The application should free any memory associated with this
517 * pointer before png_write_destroy and png_read_destroy are called.
518 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500519png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500520png_get_mem_ptr(png_structp png_ptr)
521{
522 return ((png_voidp)png_ptr->mem_ptr);
523}
524#endif /* PNG_USER_MEM_SUPPORTED */