blob: bff23d785b89e936d2cd8fceab1c337ec02af7c4 [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-Pehrson3097f612001-05-07 14:52:45 -05004 * libpng 1.2.0beta2 - May 7, 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-Pehrson1fd5fb32001-05-06 05:34:26 -050030 return (png_create_struct_2(type, 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-Pehrson1fd5fb32001-05-06 05:34:26 -050035png_create_struct_2(int type, png_malloc_ptr malloc_fn)
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-Pehrsonb2120021998-01-31 20:07:59 -060046 return ((png_voidp)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-Pehrson1fd5fb32001-05-06 05:34:26 -050051 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050052 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -050053 return (struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050054 }
55#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050056 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
57 {
58 png_memset(struct_ptr, 0, size);
59 }
Guy Schalnate5a37791996-06-05 15:50:50 -050060 return (struct_ptr);
61}
62
63
64/* 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-Pehrson1fd5fb32001-05-06 05:34:26 -050069 png_destroy_struct_2(struct_ptr, (png_free_ptr)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-Pehrson1fd5fb32001-05-06 05:34:26 -050074png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050075{
76#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050077 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060078 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050079#ifdef PNG_USER_MEM_SUPPORTED
80 if(free_fn != NULL)
81 {
82 png_struct dummy_struct;
83 png_structp png_ptr = &dummy_struct;
84 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050085 return;
86 }
87#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050088 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060089 }
Guy Schalnate5a37791996-06-05 15:50:50 -050090}
91
Guy Schalnat0d580581995-07-20 02:43:20 -050092/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 * 64K. However, zlib may allocate more then 64K if you don't tell
94 * it not to. See zconf.h and png.h for more information. zlib does
95 * need to allocate exactly 64K, so whatever you call here must
96 * have the ability to do that.
97 *
98 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050099 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 * memory stuff). zlib doesn't like this at all, so we have to
101 * detect and deal with it. This code should not be needed in
102 * Windows or OS/2 modes, and only in 16 bit mode. This code has
103 * been updated by Alexander Lehmann for version 0.89 to waste less
104 * memory.
105 *
106 * Note that we can't use png_size_t for the "size" declaration,
107 * since on some systems a png_size_t is a 16-bit quantity, and as a
108 * result, we would be truncating potentially larger memory requests
109 * (which should cause a fatal error) and introducing major problems.
110 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500112png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600113{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500114#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600115 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500116#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600118 return ((png_voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600119
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500120#ifdef PNG_USER_MEM_SUPPORTED
121 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500122 {
123 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
124 if (ret == NULL)
125 png_error(png_ptr, "Out of memory!");
126 return (ret);
127 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500128 else
129 return png_malloc_default(png_ptr, size);
130}
131
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500132png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500133png_malloc_default(png_structp png_ptr, png_uint_32 size)
134{
135 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600136#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500137
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600138#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600139 if (size > (png_uint_32)65536L)
140 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600141#endif
142
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600143 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600144 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600146 {
147 /* try to see if we need to do any of this fancy stuff */
148 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500149 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600150 {
151 int num_blocks;
152 png_uint_32 total_size;
153 png_bytep table;
154 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600155 png_byte huge * hptr;
156
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600158 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600159 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600160 ret = NULL;
161 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600162
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500163 if(png_ptr->zlib_window_bits > 14)
164 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
165 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600166 num_blocks = 1;
167 if (png_ptr->zlib_mem_level >= 7)
168 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
169 else
170 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600171
Guy Schalnate5a37791996-06-05 15:50:50 -0500172 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600173
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600174 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600175
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600177 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600178 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600180
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500183 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600185
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600186 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 png_ptr->offset_table_ptr = farmalloc(num_blocks *
188 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500189
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600191 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600192 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500193 }
194
195 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 {
198 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500199 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600200 }
201 for (i = 0; i < num_blocks; i++)
202 {
203 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500204 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600205 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600206
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 png_ptr->offset_table_number = num_blocks;
208 png_ptr->offset_table_count = 0;
209 png_ptr->offset_table_count_free = 0;
210 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500212
213 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600214 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500215
216 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600217 }
218 else
219 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500220
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500221 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600223 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500224 }
225
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600226 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500227}
228
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500229/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600230 configuration, png_ptr is not used, but is passed in case it
231 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500232void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500233png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500234{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600236 return;
237
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500238#ifdef PNG_USER_MEM_SUPPORTED
239 if (png_ptr->free_fn != NULL)
240 {
241 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500242 return;
243 }
244 else png_free_default(png_ptr, ptr);
245}
246
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500247void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500248png_free_default(png_structp png_ptr, png_voidp ptr)
249{
250#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600251
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600253 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600255
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 for (i = 0; i < png_ptr->offset_table_count; i++)
257 {
258 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600259 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 ptr = NULL;
261 png_ptr->offset_table_count_free++;
262 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600263 }
264 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500265 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
266 {
267 farfree(png_ptr->offset_table);
268 farfree(png_ptr->offset_table_ptr);
269 png_ptr->offset_table = NULL;
270 png_ptr->offset_table_ptr = NULL;
271 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600272 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273
274 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600275 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600277 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600278}
279
280#else /* Not the Borland DOS special memory handler */
281
Guy Schalnate5a37791996-06-05 15:50:50 -0500282/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600283 memset can be replaced by a single call to calloc() if this is thought
284 to improve performance noticably.*/
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500285png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600286png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500287{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500288#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500289 return (png_create_struct_2(type, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500290}
291
292/* Allocate memory for a png_struct or a png_info. The malloc and
293 memset can be replaced by a single call to calloc() if this is thought
294 to improve performance noticably.*/
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500295png_voidp /* PRIVATE */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500296png_create_struct_2(int type, png_malloc_ptr malloc_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500297{
298#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500300 png_voidp struct_ptr;
301
302 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500304 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500306 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600307 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500308
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500309#ifdef PNG_USER_MEM_SUPPORTED
310 if(malloc_fn != NULL)
311 {
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500312 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500313 png_memset(struct_ptr, 0, size);
314 return (struct_ptr);
315 }
316#endif /* PNG_USER_MEM_SUPPORTED */
317
Guy Schalnate5a37791996-06-05 15:50:50 -0500318#if defined(__TURBOC__) && !defined(__FLAT__)
319 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
320#else
321# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500322 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500323# else
324 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
325# endif
326#endif
327 {
328 png_memset(struct_ptr, 0, size);
329 }
330
331 return (struct_ptr);
332}
333
334
335/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500336void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500337png_destroy_struct(png_voidp struct_ptr)
338{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500339#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500340 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500341}
342
343/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500344void /* PRIVATE */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500345png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500346{
347#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600349 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500350#ifdef PNG_USER_MEM_SUPPORTED
351 if(free_fn != NULL)
352 {
353 png_struct dummy_struct;
354 png_structp png_ptr = &dummy_struct;
355 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500356 return;
357 }
358#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500359#if defined(__TURBOC__) && !defined(__FLAT__)
360 farfree(struct_ptr);
361#else
362# if defined(_MSC_VER) && defined(MAXSEG_64K)
363 hfree(struct_ptr);
364# else
365 free(struct_ptr);
366# endif
367#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600368 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500369}
370
371
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600372/* Allocate memory. For reasonable files, size should never exceed
373 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600375 need to allocate exactly 64K, so whatever you call here must
376 have the ability to do that. */
377
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500378png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500379png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600380{
381 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600383 return ((png_voidp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600384
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500385#ifdef PNG_USER_MEM_SUPPORTED
386 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500387 {
388 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
389 if (ret == NULL)
390 png_error(png_ptr, "Out of Memory!");
391 return (ret);
392 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500393 else
394 return (png_malloc_default(png_ptr, size));
395}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500396png_voidp /* PRIVATE */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500397png_malloc_default(png_structp png_ptr, png_uint_32 size)
398{
399 png_voidp ret;
400#endif /* PNG_USER_MEM_SUPPORTED */
401
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600402#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600403 if (size > (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600404 png_error(png_ptr, "Cannot Allocate > 64K");
405#endif
406
407#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600408 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600409#else
410# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600411 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600412# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600413 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600414# endif
415#endif
416
417 if (ret == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600418 png_error(png_ptr, "Out of Memory");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600419
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600420 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421}
422
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500423/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
424 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500425void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500426png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600429 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500430
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500431#ifdef PNG_USER_MEM_SUPPORTED
432 if (png_ptr->free_fn != NULL)
433 {
434 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500435 return;
436 }
437 else png_free_default(png_ptr, ptr);
438}
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500439void /* PRIVATE */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500440png_free_default(png_structp png_ptr, png_voidp ptr)
441{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500442 if (png_ptr == NULL || ptr == NULL)
443 return;
444
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500445#endif /* PNG_USER_MEM_SUPPORTED */
446
Guy Schalnat6d764711995-12-19 03:22:19 -0600447#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500449#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600450# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600452# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600454# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500455#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500456}
457
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600458#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600459
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500460png_voidp /* PRIVATE */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600461png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600462 png_uint_32 length)
463{
464 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600465
466 size = (png_size_t)length;
467 if ((png_uint_32)size != length)
468 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600469
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600470 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600471}
472
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500473png_voidp /* PRIVATE */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600474png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600475 png_uint_32 length)
476{
477 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600478
479 size = (png_size_t)length;
480 if ((png_uint_32)size != length)
481 png_error(png_ptr,"Overflow in png_memset_check.");
482
483 return (png_memset (s1, value, size));
484
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600485}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500486
487#ifdef PNG_USER_MEM_SUPPORTED
488/* This function is called when the application wants to use another method
489 * of allocating and freeing memory.
490 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500491void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500492png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
493 malloc_fn, png_free_ptr free_fn)
494{
495 png_ptr->mem_ptr = mem_ptr;
496 png_ptr->malloc_fn = malloc_fn;
497 png_ptr->free_fn = free_fn;
498}
499
500/* This function returns a pointer to the mem_ptr associated with the user
501 * functions. The application should free any memory associated with this
502 * pointer before png_write_destroy and png_read_destroy are called.
503 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500504png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500505png_get_mem_ptr(png_structp png_ptr)
506{
507 return ((png_voidp)png_ptr->mem_ptr);
508}
509#endif /* PNG_USER_MEM_SUPPORTED */