blob: 0b98055a039a91e51ee89f792fdf54e406504274 [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-Pehrsonf7d1a171998-06-06 15:31:35 -05004 * libpng 1.0.1e - June 6, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
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-Pehrsonb6ce43d1998-01-01 07:13:13 -060011 * need special memory handling are expected to modify the code in this file
12 * to meet their needs. See the instructions at each function.
13 */
Guy Schalnat0d580581995-07-20 02:43:20 -050014
15#define PNG_INTERNAL
16#include "png.h"
17
Guy Schalnat4ee97b01996-01-16 01:51:56 -060018/* Borland DOS special memory handler */
19#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
20/* if you change this, be sure to change the one in png.h also */
21
Guy Schalnate5a37791996-06-05 15:50:50 -050022/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060023 by a single call to calloc() if this is thought to improve performance. */
Guy Schalnate5a37791996-06-05 15:50:50 -050024png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060025png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050026{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050027#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_struct_2(type, NULL));
29}
30
31/* Alternate version of png_create_struct, for use with user-defined malloc. */
32png_voidp
33png_create_struct_2(int type, png_malloc_ptr malloc_fn)
34{
35#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060036 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050037 png_voidp struct_ptr;
38
39 if (type == PNG_STRUCT_INFO)
40 size = sizeof(png_info);
41 else if (type == PNG_STRUCT_PNG)
42 size = sizeof(png_struct);
43 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -060044 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -050045
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050046#ifdef PNG_USER_MEM_SUPPORTED
47 if(malloc_fn != NULL)
48 {
49 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
50 png_memset(struct_ptr, 0, size);
51 return (struct_ptr);
52 }
53#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050054 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
55 {
56 png_memset(struct_ptr, 0, size);
57 }
Guy Schalnate5a37791996-06-05 15:50:50 -050058 return (struct_ptr);
59}
60
61
62/* Free memory allocated by a png_create_struct() call */
63void
64png_destroy_struct(png_voidp struct_ptr)
65{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050066#ifdef PNG_USER_MEM_SUPPORTED
67 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
68}
69
70/* Free memory allocated by a png_create_struct() call */
71void
72png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
73{
74#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050075 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060076 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050077#ifdef PNG_USER_MEM_SUPPORTED
78 if(free_fn != NULL)
79 {
80 png_struct dummy_struct;
81 png_structp png_ptr = &dummy_struct;
82 (*(free_fn))(png_ptr, struct_ptr);
83 struct_ptr = NULL;
84 return;
85 }
86#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050087 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060088 struct_ptr = NULL;
89 }
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 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600111png_voidp
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)
122 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
123 else
124 return png_malloc_default(png_ptr, size);
125}
126
127png_voidp
128png_malloc_default(png_structp png_ptr, png_uint_32 size)
129{
130 png_voidp ret;
131#endif PNG_USER_MEM_SUPPORTED
132
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600133#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 if (size > (png_uint_32)65536L)
135 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600136#endif
137
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600138 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600139 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500140 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 {
142 /* try to see if we need to do any of this fancy stuff */
143 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145 {
146 int num_blocks;
147 png_uint_32 total_size;
148 png_bytep table;
149 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600150 png_byte huge * hptr;
151
Andreas Dilger47a0c421997-05-16 02:46:07 -0500152 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600153 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600155 ret = NULL;
156 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600157
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
159 if (num_blocks < 1)
160 num_blocks = 1;
161 if (png_ptr->zlib_mem_level >= 7)
162 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
163 else
164 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600165
Guy Schalnate5a37791996-06-05 15:50:50 -0500166 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600167
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600168 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600169
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600172 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600173 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600174
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600176 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600178 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600179
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 png_ptr->offset_table_ptr = farmalloc(num_blocks *
182 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500183
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600185 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600186 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500187 }
188
189 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500191 {
192 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
193 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600194 }
195 for (i = 0; i < num_blocks; i++)
196 {
197 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600198 hptr += (png_uint_32)65536L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600199 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600200
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600201 png_ptr->offset_table_number = num_blocks;
202 png_ptr->offset_table_count = 0;
203 png_ptr->offset_table_count_free = 0;
204 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600205 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500206
207 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600208 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500209
210 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211 }
212 else
213 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500214
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500215 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500216 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600217 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 }
219
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600220 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500221}
222
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500223/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 configuration, png_ptr is not used, but is passed in case it
225 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500226void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500227png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500228{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 return;
231
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500232#ifdef PNG_USER_MEM_SUPPORTED
233 if (png_ptr->free_fn != NULL)
234 {
235 (*(png_ptr->free_fn))(png_ptr, ptr);
236 ptr = NULL;
237 return;
238 }
239 else png_free_default(png_ptr, ptr);
240}
241
242void
243png_free_default(png_structp png_ptr, png_voidp ptr)
244{
245#endif /* PNG_USER_MEM_SUPPORTED */
246
Andreas Dilger47a0c421997-05-16 02:46:07 -0500247 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600248 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600250
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 for (i = 0; i < png_ptr->offset_table_count; i++)
252 {
253 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600254 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 ptr = NULL;
256 png_ptr->offset_table_count_free++;
257 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600258 }
259 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
261 {
262 farfree(png_ptr->offset_table);
263 farfree(png_ptr->offset_table_ptr);
264 png_ptr->offset_table = NULL;
265 png_ptr->offset_table_ptr = NULL;
266 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600267 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268
269 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600270 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600272 ptr = NULL;
273 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600274}
275
276#else /* Not the Borland DOS special memory handler */
277
Guy Schalnate5a37791996-06-05 15:50:50 -0500278/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600279 memset can be replaced by a single call to calloc() if this is thought
280 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500281png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600282png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500283{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500284#ifdef PNG_USER_MEM_SUPPORTED
285 return (png_create_struct_2(type, NULL));
286}
287
288/* Allocate memory for a png_struct or a png_info. The malloc and
289 memset can be replaced by a single call to calloc() if this is thought
290 to improve performance noticably.*/
291png_voidp
292png_create_struct_2(int type, png_malloc_ptr malloc_fn)
293{
294#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500296 png_voidp struct_ptr;
297
298 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500300 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500302 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600303 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500304
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500305#ifdef PNG_USER_MEM_SUPPORTED
306 if(malloc_fn != NULL)
307 {
308 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
309 png_memset(struct_ptr, 0, size);
310 return (struct_ptr);
311 }
312#endif /* PNG_USER_MEM_SUPPORTED */
313
Guy Schalnate5a37791996-06-05 15:50:50 -0500314#if defined(__TURBOC__) && !defined(__FLAT__)
315 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
316#else
317# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500318 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500319# else
320 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
321# endif
322#endif
323 {
324 png_memset(struct_ptr, 0, size);
325 }
326
327 return (struct_ptr);
328}
329
330
331/* Free memory allocated by a png_create_struct() call */
332void
333png_destroy_struct(png_voidp struct_ptr)
334{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500335#ifdef PNG_USER_MEM_SUPPORTED
336 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
337}
338
339/* Free memory allocated by a png_create_struct() call */
340void
341png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
342{
343#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600345 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500346#ifdef PNG_USER_MEM_SUPPORTED
347 if(free_fn != NULL)
348 {
349 png_struct dummy_struct;
350 png_structp png_ptr = &dummy_struct;
351 (*(free_fn))(png_ptr, struct_ptr);
352 struct_ptr = NULL;
353 return;
354 }
355#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500356#if defined(__TURBOC__) && !defined(__FLAT__)
357 farfree(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500358 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500359#else
360# if defined(_MSC_VER) && defined(MAXSEG_64K)
361 hfree(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500362 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500363# else
364 free(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500365 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500366# 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
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600378png_voidp
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{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500381#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500383#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600385 return ((png_voidp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600386
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500387#ifdef PNG_USER_MEM_SUPPORTED
388 if(png_ptr->malloc_fn != NULL)
389 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
390 else
391 return (png_malloc_default(png_ptr, size));
392}
393png_voidp
394png_malloc_default(png_structp png_ptr, png_uint_32 size)
395{
396 png_voidp ret;
397#endif /* PNG_USER_MEM_SUPPORTED */
398
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600399#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600400 if (size > (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600401 png_error(png_ptr, "Cannot Allocate > 64K");
402#endif
403
404#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600405 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600406#else
407# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600408 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600409# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600410 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600411# endif
412#endif
413
414 if (ret == NULL)
415 {
416 png_error(png_ptr, "Out of Memory");
417 }
418
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600419 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600420}
421
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500422/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
423 without taking any action. */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600424void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500425png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600426{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600428 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500429
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500430#ifdef PNG_USER_MEM_SUPPORTED
431 if (png_ptr->free_fn != NULL)
432 {
433 (*(png_ptr->free_fn))(png_ptr, ptr);
434 ptr = NULL;
435 return;
436 }
437 else png_free_default(png_ptr, ptr);
438}
439void
440png_free_default(png_structp png_ptr, png_voidp ptr)
441{
442#endif /* PNG_USER_MEM_SUPPORTED */
443
Guy Schalnat6d764711995-12-19 03:22:19 -0600444#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 farfree(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500446 ptr = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500447#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600448# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449 hfree(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500450 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600451# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 free(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500453 ptr = NULL;
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-Pehrsona357b991998-02-08 20:56:40 -0600460png_voidp
461png_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.");
469
470 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600471}
472
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600473png_voidp
474png_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 */
491void
492png_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 */
504png_voidp
505png_get_mem_ptr(png_structp png_ptr)
506{
507 return ((png_voidp)png_ptr->mem_ptr);
508}
509#endif /* PNG_USER_MEM_SUPPORTED */