blob: 8cc220da367486a0e81f79c14f30f0f2b9283096 [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-Pehrson4393a9a1999-09-17 12:27:26 -05004 * libpng 1.0.4 - September 17, 1999
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-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 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-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. */
Guy Schalnate5a37791996-06-05 15:50:50 -050026png_voidp
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
30 return (png_create_struct_2(type, NULL));
31}
32
33/* Alternate version of png_create_struct, for use with user-defined malloc. */
34png_voidp
35png_create_struct_2(int type, png_malloc_ptr malloc_fn)
36{
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 {
51 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
52 png_memset(struct_ptr, 0, size);
53 return (struct_ptr);
54 }
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 */
65void
66png_destroy_struct(png_voidp struct_ptr)
67{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050068#ifdef PNG_USER_MEM_SUPPORTED
69 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
70}
71
72/* Free memory allocated by a png_create_struct() call */
73void
74png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
75{
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);
85 struct_ptr = NULL;
86 return;
87 }
88#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050089 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060090 struct_ptr = NULL;
91 }
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 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600113png_voidp
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-Pehrsonb2120021998-01-31 20:07:59 -0600120 return ((png_voidp)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)
124 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
125 else
126 return png_malloc_default(png_ptr, size);
127}
128
129png_voidp
130png_malloc_default(png_structp png_ptr, png_uint_32 size)
131{
132 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600133#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500134
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600135#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600136 if (size > (png_uint_32)65536L)
137 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600138#endif
139
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600140 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600143 {
144 /* try to see if we need to do any of this fancy stuff */
145 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600147 {
148 int num_blocks;
149 png_uint_32 total_size;
150 png_bytep table;
151 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600152 png_byte huge * hptr;
153
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600155 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600156 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600157 ret = NULL;
158 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600159
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500160 if(png_ptr->zlib_window_bits > 14)
161 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
162 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600163 num_blocks = 1;
164 if (png_ptr->zlib_mem_level >= 7)
165 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
166 else
167 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600168
Guy Schalnate5a37791996-06-05 15:50:50 -0500169 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600170
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600172
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600174 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600175 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600176 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600177
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500180 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600182
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 png_ptr->offset_table_ptr = farmalloc(num_blocks *
185 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500186
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600189 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500190 }
191
192 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500194 {
195 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500196 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600197 }
198 for (i = 0; i < num_blocks; i++)
199 {
200 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500201 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600202 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600203
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600204 png_ptr->offset_table_number = num_blocks;
205 png_ptr->offset_table_count = 0;
206 png_ptr->offset_table_count_free = 0;
207 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500209
210 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600211 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500212
213 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600214 }
215 else
216 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500217
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500218 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500219 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600220 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500221 }
222
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600223 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500224}
225
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500226/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227 configuration, png_ptr is not used, but is passed in case it
228 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500229void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500230png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500231{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600233 return;
234
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500235#ifdef PNG_USER_MEM_SUPPORTED
236 if (png_ptr->free_fn != NULL)
237 {
238 (*(png_ptr->free_fn))(png_ptr, ptr);
239 ptr = NULL;
240 return;
241 }
242 else png_free_default(png_ptr, ptr);
243}
244
245void
246png_free_default(png_structp png_ptr, png_voidp ptr)
247{
248#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600249
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600251 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600253
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 for (i = 0; i < png_ptr->offset_table_count; i++)
255 {
256 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600257 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 ptr = NULL;
259 png_ptr->offset_table_count_free++;
260 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600261 }
262 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
264 {
265 farfree(png_ptr->offset_table);
266 farfree(png_ptr->offset_table_ptr);
267 png_ptr->offset_table = NULL;
268 png_ptr->offset_table_ptr = NULL;
269 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600270 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271
272 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600273 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600275 ptr = NULL;
276 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600277}
278
279#else /* Not the Borland DOS special memory handler */
280
Guy Schalnate5a37791996-06-05 15:50:50 -0500281/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600282 memset can be replaced by a single call to calloc() if this is thought
283 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500284png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600285png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500286{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500287#ifdef PNG_USER_MEM_SUPPORTED
288 return (png_create_struct_2(type, NULL));
289}
290
291/* Allocate memory for a png_struct or a png_info. The malloc and
292 memset can be replaced by a single call to calloc() if this is thought
293 to improve performance noticably.*/
294png_voidp
295png_create_struct_2(int type, png_malloc_ptr malloc_fn)
296{
297#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500299 png_voidp struct_ptr;
300
301 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500303 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500305 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600306 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500307
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500308#ifdef PNG_USER_MEM_SUPPORTED
309 if(malloc_fn != NULL)
310 {
311 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
312 png_memset(struct_ptr, 0, size);
313 return (struct_ptr);
314 }
315#endif /* PNG_USER_MEM_SUPPORTED */
316
Guy Schalnate5a37791996-06-05 15:50:50 -0500317#if defined(__TURBOC__) && !defined(__FLAT__)
318 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
319#else
320# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500321 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500322# else
323 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
324# endif
325#endif
326 {
327 png_memset(struct_ptr, 0, size);
328 }
329
330 return (struct_ptr);
331}
332
333
334/* Free memory allocated by a png_create_struct() call */
335void
336png_destroy_struct(png_voidp struct_ptr)
337{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500338#ifdef PNG_USER_MEM_SUPPORTED
339 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
340}
341
342/* Free memory allocated by a png_create_struct() call */
343void
344png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
345{
346#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500347 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600348 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500349#ifdef PNG_USER_MEM_SUPPORTED
350 if(free_fn != NULL)
351 {
352 png_struct dummy_struct;
353 png_structp png_ptr = &dummy_struct;
354 (*(free_fn))(png_ptr, struct_ptr);
355 struct_ptr = NULL;
356 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);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500361 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500362#else
363# if defined(_MSC_VER) && defined(MAXSEG_64K)
364 hfree(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500365 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500366# else
367 free(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500368 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500369# endif
370#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600371 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500372}
373
374
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600375/* Allocate memory. For reasonable files, size should never exceed
376 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600378 need to allocate exactly 64K, so whatever you call here must
379 have the ability to do that. */
380
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600381png_voidp
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500382png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600383{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500384#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600385 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500386#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600388 return ((png_voidp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600389
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500390#ifdef PNG_USER_MEM_SUPPORTED
391 if(png_ptr->malloc_fn != NULL)
392 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
393 else
394 return (png_malloc_default(png_ptr, size));
395}
396png_voidp
397png_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)
418 {
419 png_error(png_ptr, "Out of Memory");
420 }
421
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600422 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600423}
424
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500425/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
426 without taking any action. */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500428png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600429{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600431 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500432
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500433#ifdef PNG_USER_MEM_SUPPORTED
434 if (png_ptr->free_fn != NULL)
435 {
436 (*(png_ptr->free_fn))(png_ptr, ptr);
437 ptr = NULL;
438 return;
439 }
440 else png_free_default(png_ptr, ptr);
441}
442void
443png_free_default(png_structp png_ptr, png_voidp ptr)
444{
445#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);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500449 ptr = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500450#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600451# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 hfree(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500453 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600454# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455 free(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500456 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600457# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500458#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500459}
460
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600461#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600462
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600463png_voidp
464png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600465 png_uint_32 length)
466{
467 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600468
469 size = (png_size_t)length;
470 if ((png_uint_32)size != length)
471 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600472
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600473 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600474}
475
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600476png_voidp
477png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600478 png_uint_32 length)
479{
480 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600481
482 size = (png_size_t)length;
483 if ((png_uint_32)size != length)
484 png_error(png_ptr,"Overflow in png_memset_check.");
485
486 return (png_memset (s1, value, size));
487
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600488}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500489
490#ifdef PNG_USER_MEM_SUPPORTED
491/* This function is called when the application wants to use another method
492 * of allocating and freeing memory.
493 */
494void
495png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
496 malloc_fn, png_free_ptr free_fn)
497{
498 png_ptr->mem_ptr = mem_ptr;
499 png_ptr->malloc_fn = malloc_fn;
500 png_ptr->free_fn = free_fn;
501}
502
503/* This function returns a pointer to the mem_ptr associated with the user
504 * functions. The application should free any memory associated with this
505 * pointer before png_write_destroy and png_read_destroy are called.
506 */
507png_voidp
508png_get_mem_ptr(png_structp png_ptr)
509{
510 return ((png_voidp)png_ptr->mem_ptr);
511}
512#endif /* PNG_USER_MEM_SUPPORTED */