blob: 8fa16b5fb798e26e121bef0f75cc436344a0a863 [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-Pehrson145f5c82008-07-10 09:13:13 -05004 * Last changed in libpng 1.4.0 [July 10, 2008]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05006 * Copyright (c) 1998-2008 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
Guy Schalnat0d580581995-07-20 02:43:20 -050017#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Guy Schalnat4ee97b01996-01-16 01:51:56 -060021/* Borland DOS special memory handler */
22#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
23/* if you change this, be sure to change the one in png.h also */
24
Guy Schalnate5a37791996-06-05 15:50:50 -050025/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060026 by a single call to calloc() if this is thought to improve performance. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060028png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050029{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050031 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050032}
33
34/* Alternate version of png_create_struct, for use with user-defined malloc. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050035png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050036png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050037{
38#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050040 png_voidp struct_ptr;
41
42 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050043 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -050044 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050045 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -050046 else
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050047 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050048
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050049#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050050 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050051 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050052 png_struct dummy_struct;
53 png_structp png_ptr = &dummy_struct;
54 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050055 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050056 }
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050057 else
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050058#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050059 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050060 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050061 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050062 return (struct_ptr);
63}
64
Guy Schalnate5a37791996-06-05 15:50:50 -050065/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050066void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050067png_destroy_struct(png_voidp struct_ptr)
68{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050069#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050070 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050071}
72
73/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050074void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050075png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
76 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050077{
78#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060080 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050081#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050082 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050083 {
84 png_struct dummy_struct;
85 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050086 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050087 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050088 return;
89 }
90#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050091 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060092 }
Guy Schalnate5a37791996-06-05 15:50:50 -050093}
94
Guy Schalnat0d580581995-07-20 02:43:20 -050095/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050096 * 64K. However, zlib may allocate more then 64K if you don't tell
97 * it not to. See zconf.h and png.h for more information. zlib does
98 * need to allocate exactly 64K, so whatever you call here must
99 * have the ability to do that.
100 *
101 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500102 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500103 * memory stuff). zlib doesn't like this at all, so we have to
104 * detect and deal with it. This code should not be needed in
105 * Windows or OS/2 modes, and only in 16 bit mode. This code has
106 * been updated by Alexander Lehmann for version 0.89 to waste less
107 * memory.
108 *
109 * Note that we can't use png_size_t for the "size" declaration,
110 * since on some systems a png_size_t is a 16-bit quantity, and as a
111 * result, we would be truncating potentially larger memory requests
112 * (which should cause a fatal error) and introducing major problems.
113 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500114
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500115png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500116png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600117{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500119
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500121 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600122
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500123#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500124 if (png_ptr->malloc_fn != NULL)
125 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500126 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500127 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500128 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500129 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500130 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500131}
132
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500133png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500134png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500135{
136 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600137#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500138
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600139 if (png_ptr == NULL || size == 0)
140 return (NULL);
141
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600142#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500143 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500144 {
145 png_warning(png_ptr, "Cannot Allocate > 64K");
146 ret = NULL;
147 }
148 else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600149#endif
150
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500151 if (size != (size_t)size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500152 ret = NULL;
153 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600156 {
157 /* try to see if we need to do any of this fancy stuff */
158 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600160 {
161 int num_blocks;
162 png_uint_32 total_size;
163 png_bytep table;
164 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600165 png_byte huge * hptr;
166
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600168 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600169 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600170 ret = NULL;
171 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600172
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500174 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
175 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600176 num_blocks = 1;
177 if (png_ptr->zlib_mem_level >= 7)
178 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
179 else
180 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600181
Guy Schalnate5a37791996-06-05 15:50:50 -0500182 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600183
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600185
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600187 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500188#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500189 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500190 png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600191 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500192 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500193#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600194 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600196
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500199#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500200 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600201 png_error(png_ptr,
202 "Farmalloc didn't return normalized pointer");
203 else
204 png_warning(png_ptr,
205 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500206#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600207 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600209
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600210 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500212 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500213
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600215 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500216#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500217 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500218 png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600219 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500220 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500221#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600222 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500223 }
224
225 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500227 {
228 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500229 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 }
231 for (i = 0; i < num_blocks; i++)
232 {
233 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500234 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600235 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600236
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600237 png_ptr->offset_table_number = num_blocks;
238 png_ptr->offset_table_count = 0;
239 png_ptr->offset_table_count_free = 0;
240 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600241 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500242
243 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600244 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500245#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500246 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500247 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600248 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500249 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500250#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600251 return (NULL);
252 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500253
254 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600255 }
256 else
257 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500258
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500259#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500261 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500262 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500263 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600264 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500267#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500268
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600269 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500270}
271
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500272/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600273 configuration, png_ptr is not used, but is passed in case it
274 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500275void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500276png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500277{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600279 return;
280
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500281#ifdef PNG_USER_MEM_SUPPORTED
282 if (png_ptr->free_fn != NULL)
283 {
284 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500285 return;
286 }
287 else png_free_default(png_ptr, ptr);
288}
289
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500290void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500291png_free_default(png_structp png_ptr, png_voidp ptr)
292{
293#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600294
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500295 if (png_ptr == NULL || ptr == NULL) return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600296
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600298 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600300
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 for (i = 0; i < png_ptr->offset_table_count; i++)
302 {
303 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600304 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 ptr = NULL;
306 png_ptr->offset_table_count_free++;
307 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600308 }
309 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
311 {
312 farfree(png_ptr->offset_table);
313 farfree(png_ptr->offset_table_ptr);
314 png_ptr->offset_table = NULL;
315 png_ptr->offset_table_ptr = NULL;
316 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600317 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318
319 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600320 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600322 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600323}
324
325#else /* Not the Borland DOS special memory handler */
326
Guy Schalnate5a37791996-06-05 15:50:50 -0500327/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600328 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500329 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500330png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600331png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500332{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500333#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500334 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500335}
336
337/* Allocate memory for a png_struct or a png_info. The malloc and
338 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500339 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500340png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500341png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500342{
343#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500345 png_voidp struct_ptr;
346
347 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500348 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500349 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500350 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500351 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500352 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500353
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500354#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500355 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500356 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600357 png_struct dummy_struct;
358 png_structp png_ptr = &dummy_struct;
359 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600360 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500361 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500362 png_memset(struct_ptr, 0, size);
363 return (struct_ptr);
364 }
365#endif /* PNG_USER_MEM_SUPPORTED */
366
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500367#if defined(__TURBOC__) && !defined(__FLAT__)
368 struct_ptr = (png_voidp)farmalloc(size);
369#else
370# if defined(_MSC_VER) && defined(MAXSEG_64K)
371 struct_ptr = (png_voidp)halloc(size, 1);
372# else
373 struct_ptr = (png_voidp)malloc(size);
374# endif
375#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500376 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500377 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500378
379 return (struct_ptr);
380}
381
382
383/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500384void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500385png_destroy_struct(png_voidp struct_ptr)
386{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500387#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500388 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500389}
390
391/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500392void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500393png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
394 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500395{
396#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600398 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500399#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500400 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500401 {
402 png_struct dummy_struct;
403 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500404 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500405 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500406 return;
407 }
408#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500409#if defined(__TURBOC__) && !defined(__FLAT__)
410 farfree(struct_ptr);
411#else
412# if defined(_MSC_VER) && defined(MAXSEG_64K)
413 hfree(struct_ptr);
414# else
415 free(struct_ptr);
416# endif
417#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600418 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500419}
420
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600421/* Allocate memory. For reasonable files, size should never exceed
422 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600424 need to allocate exactly 64K, so whatever you call here must
425 have the ability to do that. */
426
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500427png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500428png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600429{
430 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500431
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500432#ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500434 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600435
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500436 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500437 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500438 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500439 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500440 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500441 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500442 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500443}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500444
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600445png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500446png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500447{
448 png_voidp ret;
449#endif /* PNG_USER_MEM_SUPPORTED */
450
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500451 if (png_ptr == NULL || size == 0)
452 return (NULL);
453
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600454#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500455 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600456 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500457#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500458 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600459 png_error(png_ptr, "Cannot Allocate > 64K");
460 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500461#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600462 return NULL;
463 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600464#endif
465
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500466 /* Check for overflow */
467#if defined(__TURBOC__) && !defined(__FLAT__)
468 if (size != (unsigned long)size)
469 ret = NULL;
470 else
471 ret = farmalloc(size);
472#else
473# if defined(_MSC_VER) && defined(MAXSEG_64K)
474 if (size != (unsigned long)size)
475 ret = NULL;
476 else
477 ret = halloc(size, 1);
478# else
479 if (size != (size_t)size)
480 ret = NULL;
481 else
482 ret = malloc((size_t)size);
483# endif
484#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500486#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600487 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500489#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600490
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600491 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600492}
493
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500494/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
495 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500496void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500497png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600498{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600500 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500501
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502#ifdef PNG_USER_MEM_SUPPORTED
503 if (png_ptr->free_fn != NULL)
504 {
505 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500506 return;
507 }
508 else png_free_default(png_ptr, ptr);
509}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600510void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500511png_free_default(png_structp png_ptr, png_voidp ptr)
512{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500513 if (png_ptr == NULL || ptr == NULL)
514 return;
515
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500516#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500517
518#if defined(__TURBOC__) && !defined(__FLAT__)
519 farfree(ptr);
520#else
521# if defined(_MSC_VER) && defined(MAXSEG_64K)
522 hfree(ptr);
523# else
524 free(ptr);
525# endif
526#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500527}
528
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600529#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600530
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500531/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500532 * function will set up png_malloc() to issue a png_warning and return NULL
533 * instead of issuing a png_error, if it fails to allocate the requested
534 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500535 */
536png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500537png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500538{
539 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600540 png_uint_32 save_flags;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500541 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500542
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600543 save_flags=png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500544 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
545 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
546 png_ptr->flags=save_flags;
547 return(ptr);
548}
549
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500550
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500551#ifdef PNG_USER_MEM_SUPPORTED
552/* This function is called when the application wants to use another method
553 * of allocating and freeing memory.
554 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500555void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500556png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
557 malloc_fn, png_free_ptr free_fn)
558{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500559 if (png_ptr != NULL) {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500560 png_ptr->mem_ptr = mem_ptr;
561 png_ptr->malloc_fn = malloc_fn;
562 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600563 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500564}
565
566/* This function returns a pointer to the mem_ptr associated with the user
567 * functions. The application should free any memory associated with this
568 * pointer before png_write_destroy and png_read_destroy are called.
569 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500570png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500571png_get_mem_ptr(png_structp png_ptr)
572{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500573 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500574 return ((png_voidp)png_ptr->mem_ptr);
575}
576#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600577#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */