blob: 3659d47389da56f01f90f2e4867030755cee9fe4 [file] [log] [blame]
Guy Schalnat0f716451995-11-28 11:22:13 -06001/* pngmem.c - stub functions for memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002 *
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06003 * libpng 0.99e
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004 * For conditions of distribution and use, see copyright notice in png.h
5 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
6 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06007 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06008 * February 28, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
10 * This file provides a location for all memory allocation. Users which
11 * 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
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060018/* The following "hides" PNG_MALLOC and PNG_FREE thus allowing the pngtest
19 application to put a wrapper on top of them. */
20#ifdef PNGTEST_MEMORY_DEBUG
21 #define PNG_MALLOC png_debug_malloc
22 #define PNG_FREE png_debug_free
23#else
24 #define PNG_MALLOC png_malloc
25 #define PNG_FREE png_free
26#endif
27
Guy Schalnat4ee97b01996-01-16 01:51:56 -060028/* Borland DOS special memory handler */
29#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
30/* if you change this, be sure to change the one in png.h also */
31
Guy Schalnate5a37791996-06-05 15:50:50 -050032/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060033 by a single call to calloc() if this is thought to improve performance. */
Guy Schalnate5a37791996-06-05 15:50:50 -050034png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060035png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050036{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060037 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050038 png_voidp struct_ptr;
39
40 if (type == PNG_STRUCT_INFO)
41 size = sizeof(png_info);
42 else if (type == PNG_STRUCT_PNG)
43 size = sizeof(png_struct);
44 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -060045 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -050046
47 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
48 {
49 png_memset(struct_ptr, 0, size);
50 }
51
52 return (struct_ptr);
53}
54
55
56/* Free memory allocated by a png_create_struct() call */
57void
58png_destroy_struct(png_voidp struct_ptr)
59{
Andreas Dilger47a0c421997-05-16 02:46:07 -050060 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060061 {
Guy Schalnate5a37791996-06-05 15:50:50 -050062 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060063 struct_ptr = NULL;
64 }
Guy Schalnate5a37791996-06-05 15:50:50 -050065}
66
Guy Schalnat0d580581995-07-20 02:43:20 -050067/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050068 * 64K. However, zlib may allocate more then 64K if you don't tell
69 * it not to. See zconf.h and png.h for more information. zlib does
70 * need to allocate exactly 64K, so whatever you call here must
71 * have the ability to do that.
72 *
73 * Borland seems to have a problem in DOS mode for exactly 64K.
74 * It gives you a segment with an offset of 8 (perhaps to store it's
75 * memory stuff). zlib doesn't like this at all, so we have to
76 * detect and deal with it. This code should not be needed in
77 * Windows or OS/2 modes, and only in 16 bit mode. This code has
78 * been updated by Alexander Lehmann for version 0.89 to waste less
79 * memory.
80 *
81 * Note that we can't use png_size_t for the "size" declaration,
82 * since on some systems a png_size_t is a 16-bit quantity, and as a
83 * result, we would be truncating potentially larger memory requests
84 * (which should cause a fatal error) and introducing major problems.
85 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060086png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060087PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -060088{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060089 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -060091 return ((png_voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -060092
93#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060094 if (size > (png_uint_32)65536L)
95 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -060096#endif
97
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060098 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060099 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600101 {
102 /* try to see if we need to do any of this fancy stuff */
103 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600105 {
106 int num_blocks;
107 png_uint_32 total_size;
108 png_bytep table;
109 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600110 png_byte huge * hptr;
111
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600113 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600114 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600115 ret = NULL;
116 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600117
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
119 if (num_blocks < 1)
120 num_blocks = 1;
121 if (png_ptr->zlib_mem_level >= 7)
122 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
123 else
124 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600125
Guy Schalnate5a37791996-06-05 15:50:50 -0500126 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600127
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600129
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600131 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600132 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600136 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500137 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600138 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600139
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 png_ptr->offset_table_ptr = farmalloc(num_blocks *
142 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500143
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600146 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500147 }
148
149 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 {
152 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
153 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 }
155 for (i = 0; i < num_blocks; i++)
156 {
157 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600158 hptr += (png_uint_32)65536L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600159 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600160
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600161 png_ptr->offset_table_number = num_blocks;
162 png_ptr->offset_table_count = 0;
163 png_ptr->offset_table_count_free = 0;
164 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600165 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500166
167 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600168 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500169
170 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 }
172 else
173 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500174
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600177 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500178 }
179
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600180 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500181}
182
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600183/* free a pointer allocated by PNG_MALLOC(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184 configuration, png_ptr is not used, but is passed in case it
185 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500186void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600187PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500188{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600190 return;
191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600193 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 for (i = 0; i < png_ptr->offset_table_count; i++)
197 {
198 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600199 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 ptr = NULL;
201 png_ptr->offset_table_count_free++;
202 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 }
204 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500205 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
206 {
207 farfree(png_ptr->offset_table);
208 farfree(png_ptr->offset_table_ptr);
209 png_ptr->offset_table = NULL;
210 png_ptr->offset_table_ptr = NULL;
211 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600212 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500213
214 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600217 ptr = NULL;
218 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219}
220
221#else /* Not the Borland DOS special memory handler */
222
Guy Schalnate5a37791996-06-05 15:50:50 -0500223/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 memset can be replaced by a single call to calloc() if this is thought
225 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500226png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500228{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500230 png_voidp struct_ptr;
231
232 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500234 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500236 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600237 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500238
239#if defined(__TURBOC__) && !defined(__FLAT__)
240 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
241#else
242# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500243 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500244# else
245 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
246# endif
247#endif
248 {
249 png_memset(struct_ptr, 0, size);
250 }
251
252 return (struct_ptr);
253}
254
255
256/* Free memory allocated by a png_create_struct() call */
257void
258png_destroy_struct(png_voidp struct_ptr)
259{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600261 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500262#if defined(__TURBOC__) && !defined(__FLAT__)
263 farfree(struct_ptr);
264#else
265# if defined(_MSC_VER) && defined(MAXSEG_64K)
266 hfree(struct_ptr);
267# else
268 free(struct_ptr);
269# endif
270#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600271 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500272}
273
274
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600275/* Allocate memory. For reasonable files, size should never exceed
276 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600278 need to allocate exactly 64K, so whatever you call here must
279 have the ability to do that. */
280
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600281png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600282PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600283{
284 png_voidp ret;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600285
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600287 return ((png_voidp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600288
289#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600290 if (size > (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 png_error(png_ptr, "Cannot Allocate > 64K");
292#endif
293
294#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600295 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600296#else
297# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600298 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600299# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600300 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600301# endif
302#endif
303
304 if (ret == NULL)
305 {
306 png_error(png_ptr, "Out of Memory");
307 }
308
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600309 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600310}
311
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600312/* Free a pointer allocated by PNG_MALLOC(). In the default
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600313 configuration, png_ptr is not used, but is passed in case it
314 is needed. If ptr is NULL, return without taking any action. */
315void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600316PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600317{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600319 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500320
Guy Schalnat6d764711995-12-19 03:22:19 -0600321#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500323#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600324# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600326# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500327 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600328# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500329#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500330}
331
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600332#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600333
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600334png_voidp
335png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600336 png_uint_32 length)
337{
338 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600339
340 size = (png_size_t)length;
341 if ((png_uint_32)size != length)
342 png_error(png_ptr,"Overflow in png_memcpy_check.");
343
344 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600345}
346
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600347png_voidp
348png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600349 png_uint_32 length)
350{
351 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600352
353 size = (png_size_t)length;
354 if ((png_uint_32)size != length)
355 png_error(png_ptr,"Overflow in png_memset_check.");
356
357 return (png_memset (s1, value, size));
358
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600359}