blob: 661c4f8a459b42a331319c6c647917bfb4228ed7 [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-Pehrson79134c62009-02-14 10:32:18 -06004 * Last changed in libpng 1.4.0 [February 14, 2009]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 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-Pehrson79134c62009-02-14 10:32:18 -0600116png_calloc(png_structp png_ptr, png_alloc_size_t size)
117{
118 png_voidp ret;
119
120 ret = (png_malloc(png_ptr, size));
121 if (ret != NULL)
122 png_memset(ret,0,(png_size_t)size);
123 return (ret);
124}
125
126png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500127png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600128{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600129 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500130
Andreas Dilger47a0c421997-05-16 02:46:07 -0500131 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500132 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600133
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500134#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500135 if (png_ptr->malloc_fn != NULL)
136 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500137 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500138 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500139 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500140 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500141 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500142}
143
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500144png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500145png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500146{
147 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600148#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500149
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600150 if (png_ptr == NULL || size == 0)
151 return (NULL);
152
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600153#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500154 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500155 {
156 png_warning(png_ptr, "Cannot Allocate > 64K");
157 ret = NULL;
158 }
159 else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600160#endif
161
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500162 if (size != (size_t)size)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500163 ret = NULL;
164 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600165 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600167 {
168 /* try to see if we need to do any of this fancy stuff */
169 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 {
172 int num_blocks;
173 png_uint_32 total_size;
174 png_bytep table;
175 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600176 png_byte huge * hptr;
177
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600179 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600181 ret = NULL;
182 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600183
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500184 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500185 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
186 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600187 num_blocks = 1;
188 if (png_ptr->zlib_mem_level >= 7)
189 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
190 else
191 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600192
Guy Schalnate5a37791996-06-05 15:50:50 -0500193 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600194
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600196
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 if (table == NULL)
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-Pehrsonbeb572e2006-08-19 13:59:24 -0500201 png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600202 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500203 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500204#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600205 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600206 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600207
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600209 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500210#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500211 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600212 png_error(png_ptr,
213 "Farmalloc didn't return normalized pointer");
214 else
215 png_warning(png_ptr,
216 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500217#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600218 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600220
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600221 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500223 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500224
Andreas Dilger47a0c421997-05-16 02:46:07 -0500225 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600226 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500227#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500228 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500229 png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600230 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500231 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500232#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600233 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500234 }
235
236 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500238 {
239 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500240 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600241 }
242 for (i = 0; i < num_blocks; i++)
243 {
244 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500245 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600246 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600247
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600248 png_ptr->offset_table_number = num_blocks;
249 png_ptr->offset_table_count = 0;
250 png_ptr->offset_table_count_free = 0;
251 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600252 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500253
254 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600255 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500256#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500257 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500258 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600259 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500260 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500261#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600262 return (NULL);
263 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500264
265 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600266 }
267 else
268 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500269
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500270#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500271 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500272 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500273 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500274 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600275 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500276 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500277 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500278#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500279
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600280 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500281}
282
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500283/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600284 configuration, png_ptr is not used, but is passed in case it
285 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500286
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500287void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500288png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500289{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 return;
292
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500293#ifdef PNG_USER_MEM_SUPPORTED
294 if (png_ptr->free_fn != NULL)
295 {
296 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500297 return;
298 }
299 else png_free_default(png_ptr, ptr);
300}
301
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500302void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500303png_free_default(png_structp png_ptr, png_voidp ptr)
304{
305#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600306
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500307 if (png_ptr == NULL || ptr == NULL) return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600308
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600310 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600312
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 for (i = 0; i < png_ptr->offset_table_count; i++)
314 {
315 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600316 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 ptr = NULL;
318 png_ptr->offset_table_count_free++;
319 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600320 }
321 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
323 {
324 farfree(png_ptr->offset_table);
325 farfree(png_ptr->offset_table_ptr);
326 png_ptr->offset_table = NULL;
327 png_ptr->offset_table_ptr = NULL;
328 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600329 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500330
331 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600332 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600334 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600335}
336
337#else /* Not the Borland DOS special memory handler */
338
Guy Schalnate5a37791996-06-05 15:50:50 -0500339/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600340 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500341 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500342png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600343png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500344{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500345#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500347}
348
349/* Allocate memory for a png_struct or a png_info. The malloc and
350 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500351 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500352png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500353png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500354{
355#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500357 png_voidp struct_ptr;
358
359 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500360 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500361 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500362 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500363 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500364 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500365
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500366#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500367 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500368 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600369 png_struct dummy_struct;
370 png_structp png_ptr = &dummy_struct;
371 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600372 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500373 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500374 png_memset(struct_ptr, 0, size);
375 return (struct_ptr);
376 }
377#endif /* PNG_USER_MEM_SUPPORTED */
378
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500379#if defined(__TURBOC__) && !defined(__FLAT__)
380 struct_ptr = (png_voidp)farmalloc(size);
381#else
382# if defined(_MSC_VER) && defined(MAXSEG_64K)
383 struct_ptr = (png_voidp)halloc(size, 1);
384# else
385 struct_ptr = (png_voidp)malloc(size);
386# endif
387#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500388 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500389 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500390
391 return (struct_ptr);
392}
393
394
395/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500396void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500397png_destroy_struct(png_voidp struct_ptr)
398{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500399#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500400 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500401}
402
403/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500404void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500405png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
406 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500407{
408#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600410 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500411#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500412 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500413 {
414 png_struct dummy_struct;
415 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500416 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500417 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500418 return;
419 }
420#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500421#if defined(__TURBOC__) && !defined(__FLAT__)
422 farfree(struct_ptr);
423#else
424# if defined(_MSC_VER) && defined(MAXSEG_64K)
425 hfree(struct_ptr);
426# else
427 free(struct_ptr);
428# endif
429#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600430 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500431}
432
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600433/* Allocate memory. For reasonable files, size should never exceed
434 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600436 need to allocate exactly 64K, so whatever you call here must
437 have the ability to do that. */
438
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500439png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600440png_calloc(png_structp png_ptr, png_alloc_size_t size)
441{
442 png_voidp ret;
443
444 ret = (png_malloc(png_ptr, size));
445 if (ret != NULL)
446 png_memset(ret,0,(png_size_t)size);
447 return (ret);
448}
449
450png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500451png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600452{
453 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500454
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500455#ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500457 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500459 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500460 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500461 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500462 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500463 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500464 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500465 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500466}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500467
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600468png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500469png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500470{
471 png_voidp ret;
472#endif /* PNG_USER_MEM_SUPPORTED */
473
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500474 if (png_ptr == NULL || size == 0)
475 return (NULL);
476
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600477#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500478 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600479 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500480#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500481 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600482 png_error(png_ptr, "Cannot Allocate > 64K");
483 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500484#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600485 return NULL;
486 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600487#endif
488
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500489 /* Check for overflow */
490#if defined(__TURBOC__) && !defined(__FLAT__)
491 if (size != (unsigned long)size)
492 ret = NULL;
493 else
494 ret = farmalloc(size);
495#else
496# if defined(_MSC_VER) && defined(MAXSEG_64K)
497 if (size != (unsigned long)size)
498 ret = NULL;
499 else
500 ret = halloc(size, 1);
501# else
502 if (size != (size_t)size)
503 ret = NULL;
504 else
505 ret = malloc((size_t)size);
506# endif
507#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600508
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500509#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600510 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600511 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500512#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600513
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600514 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600515}
516
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500517/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
518 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500519void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500520png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600521{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500522 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600523 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500524
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500525#ifdef PNG_USER_MEM_SUPPORTED
526 if (png_ptr->free_fn != NULL)
527 {
528 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500529 return;
530 }
531 else png_free_default(png_ptr, ptr);
532}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600533void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500534png_free_default(png_structp png_ptr, png_voidp ptr)
535{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500536 if (png_ptr == NULL || ptr == NULL)
537 return;
538
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500539#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500540
541#if defined(__TURBOC__) && !defined(__FLAT__)
542 farfree(ptr);
543#else
544# if defined(_MSC_VER) && defined(MAXSEG_64K)
545 hfree(ptr);
546# else
547 free(ptr);
548# endif
549#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500550}
551
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600552#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600553
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500554/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500555 * function will set up png_malloc() to issue a png_warning and return NULL
556 * instead of issuing a png_error, if it fails to allocate the requested
557 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500558 */
559png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500560png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500561{
562 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600563 png_uint_32 save_flags;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500564 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500565
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500566 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500567 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
568 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
569 png_ptr->flags=save_flags;
570 return(ptr);
571}
572
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500573
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500574#ifdef PNG_USER_MEM_SUPPORTED
575/* This function is called when the application wants to use another method
576 * of allocating and freeing memory.
577 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500578void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500579png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
580 malloc_fn, png_free_ptr free_fn)
581{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500582 if (png_ptr != NULL)
583 {
584 png_ptr->mem_ptr = mem_ptr;
585 png_ptr->malloc_fn = malloc_fn;
586 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600587 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500588}
589
590/* This function returns a pointer to the mem_ptr associated with the user
591 * functions. The application should free any memory associated with this
592 * pointer before png_write_destroy and png_read_destroy are called.
593 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500594png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500595png_get_mem_ptr(png_structp png_ptr)
596{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500597 if (png_ptr == NULL) return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500598 return ((png_voidp)png_ptr->mem_ptr);
599}
600#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600601#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */