blob: 01945307119907e08b72a963bbb540647a884cdd [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-Pehrson77396b62010-08-02 08:00:10 -05004 * Last changed in libpng 1.5.0 [August 2, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050013 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050014 * need special memory handling are expected to supply replacement
15 * functions for png_malloc() and png_free(), and to use
16 * png_create_read_struct_2() and png_create_write_struct_2() to
17 * identify the replacement functions.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060018 */
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050020#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
23
Guy Schalnat4ee97b01996-01-16 01:51:56 -060024/* Borland DOS special memory handler */
25#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050026/* If you change this, be sure to change the one in png.h also */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060027
Guy Schalnate5a37791996-06-05 15:50:50 -050028/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060029 by a single call to calloc() if this is thought to improve performance. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050030PNG_FUNCTION(png_voidp /* PRIVATE */,
31png_create_struct,(int type),PNG_ALLOCATED)
Guy Schalnate5a37791996-06-05 15:50:50 -050032{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060033# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050034 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050035}
36
37/* Alternate version of png_create_struct, for use with user-defined malloc. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050038PNG_FUNCTION(png_voidp /* PRIVATE */,
39png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr),
40 PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050041{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060042# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060043 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050044 png_voidp struct_ptr;
45
46 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050047 size = png_sizeof(png_info);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050048
Guy Schalnate5a37791996-06-05 15:50:50 -050049 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050050 size = png_sizeof(png_struct);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050051
Guy Schalnate5a37791996-06-05 15:50:50 -050052 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050053 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050054
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060055# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050056 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050057 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050058 png_struct dummy_struct;
59 png_structp png_ptr = &dummy_struct;
60 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050061 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050062 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050063
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050064 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060065# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050066 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050067 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050068 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050069
Guy Schalnate5a37791996-06-05 15:50:50 -050070 return (struct_ptr);
71}
72
Guy Schalnate5a37791996-06-05 15:50:50 -050073/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050074void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050075png_destroy_struct(png_voidp struct_ptr)
76{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060077# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050078 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050079}
80
81/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050082void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050083png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
84 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050085{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060086# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060088 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060089# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050090 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050091 {
92 png_struct dummy_struct;
93 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050094 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050095 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050096 return;
97 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050098
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060099# endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500100 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600101 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500102}
103
Guy Schalnat0d580581995-07-20 02:43:20 -0500104/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105 * 64K. However, zlib may allocate more then 64K if you don't tell
106 * it not to. See zconf.h and png.h for more information. zlib does
107 * need to allocate exactly 64K, so whatever you call here must
108 * have the ability to do that.
109 *
110 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500111 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 * memory stuff). zlib doesn't like this at all, so we have to
113 * detect and deal with it. This code should not be needed in
114 * Windows or OS/2 modes, and only in 16 bit mode. This code has
115 * been updated by Alexander Lehmann for version 0.89 to waste less
116 * memory.
117 *
118 * Note that we can't use png_size_t for the "size" declaration,
119 * since on some systems a png_size_t is a 16-bit quantity, and as a
120 * result, we would be truncating potentially larger memory requests
121 * (which should cause a fatal error) and introducing major problems.
122 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500123PNG_FUNCTION(png_voidp,PNGAPI
124png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600125{
126 png_voidp ret;
127
128 ret = (png_malloc(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500129
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600130 if (ret != NULL)
131 png_memset(ret,0,(png_size_t)size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500132
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600133 return (ret);
134}
135
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500136PNG_FUNCTION(png_voidp,PNGAPI
137png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600138{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600139 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500140
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500142 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600143
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600144# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500145 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500146 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500147
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500148 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500149 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500150
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500151 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500152 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500153
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500154 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500155}
156
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500157PNG_FUNCTION(png_voidp,PNGAPI
158png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500159{
160 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600161# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500162
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600163 if (png_ptr == NULL || size == 0)
164 return (NULL);
165
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600166# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500167 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500168 {
169 png_warning(png_ptr, "Cannot Allocate > 64K");
170 ret = NULL;
171 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500172
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500173 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600174# endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600175
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500176 if (size != (size_t)size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500177 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500179 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500183 /* Try to see if we need to do any of this fancy stuff */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600186 {
187 int num_blocks;
188 png_uint_32 total_size;
189 png_bytep table;
190 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600191 png_byte huge * hptr;
192
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600194 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600196 ret = NULL;
197 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600198
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500199 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500200 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500201
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500202 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 num_blocks = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500204
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600205 if (png_ptr->zlib_mem_level >= 7)
206 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500207
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 else
209 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600210
Guy Schalnate5a37791996-06-05 15:50:50 -0500211 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600212
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600213 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600214
Andreas Dilger47a0c421997-05-16 02:46:07 -0500215 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600216 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600217# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500218 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600219 png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500220
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600221 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500222 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600223# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600224 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600225 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600226
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600228 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600229# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500230 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600231 png_error(png_ptr,
232 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500233
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600234 else
235 png_warning(png_ptr,
236 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600237# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600238 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600239 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600240
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600241 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500244
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600246 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600247# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500248 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600249 png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500250
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600251 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500252 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600253# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600254 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500255 }
256
257 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500259 {
260 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500261 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600262 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500263
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600264 for (i = 0; i < num_blocks; i++)
265 {
266 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500267 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600268 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600269
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600270 png_ptr->offset_table_number = num_blocks;
271 png_ptr->offset_table_count = 0;
272 png_ptr->offset_table_count_free = 0;
273 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600274 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500275
276 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600277 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600278# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500279 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500280 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500281
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600282 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500283 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600284# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600285 return (NULL);
286 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500287
288 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600289 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500290
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 else
292 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500293
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600294# ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500295 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500296 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500297 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500298 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500299
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600300 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500301 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600303# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500304
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600305 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500306}
307
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500308/* Free a pointer allocated by png_malloc(). In the default
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 * configuration, png_ptr is not used, but is passed in case it
310 * is needed. If ptr is NULL, return without taking any action.
311 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500312void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500313png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500314{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600316 return;
317
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600318# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500319 if (png_ptr->free_fn != NULL)
320 {
321 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500322 return;
323 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500324
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500325 else
326 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500327}
328
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500329void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500330png_free_default(png_structp png_ptr, png_voidp ptr)
331{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600332# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600333
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500334 if (png_ptr == NULL || ptr == NULL)
335 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600336
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600338 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600340
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 for (i = 0; i < png_ptr->offset_table_count; i++)
342 {
343 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600344 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 ptr = NULL;
346 png_ptr->offset_table_count_free++;
347 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600348 }
349 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500350 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
351 {
352 farfree(png_ptr->offset_table);
353 farfree(png_ptr->offset_table_ptr);
354 png_ptr->offset_table = NULL;
355 png_ptr->offset_table_ptr = NULL;
356 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600357 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500358
359 if (ptr != NULL)
360 farfree(ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600361}
362
363#else /* Not the Borland DOS special memory handler */
364
Guy Schalnate5a37791996-06-05 15:50:50 -0500365/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600366 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500367 to improve performance noticably. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500368PNG_FUNCTION(png_voidp /* PRIVATE */,
369png_create_struct,(int type),PNG_ALLOCATED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500370{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600371# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500372 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500373}
374
375/* Allocate memory for a png_struct or a png_info. The malloc and
376 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500377 to improve performance noticably. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500378PNG_FUNCTION(png_voidp /* PRIVATE */,
379png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr),
380 PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500381{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600382# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500384 png_voidp struct_ptr;
385
386 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500387 size = png_sizeof(png_info);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500388
Guy Schalnate5a37791996-06-05 15:50:50 -0500389 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500390 size = png_sizeof(png_struct);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500391
Guy Schalnate5a37791996-06-05 15:50:50 -0500392 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500393 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500394
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600395# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500397 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600398 png_struct dummy_struct;
399 png_structp png_ptr = &dummy_struct;
400 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600401 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500402
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500403 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500404 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500405
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500406 return (struct_ptr);
407 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600408# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500409
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600410# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500411 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600412# else
413# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500414 struct_ptr = (png_voidp)halloc(size, 1);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600415# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500416 struct_ptr = (png_voidp)malloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600417# endif
418# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500419
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500420 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500421 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500422
423 return (struct_ptr);
424}
425
426
427/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500428void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500429png_destroy_struct(png_voidp struct_ptr)
430{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600431# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500432 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500433}
434
435/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500436void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500437png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
438 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500439{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600440# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600442 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600443# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500444 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500445 {
446 png_struct dummy_struct;
447 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500448 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500449 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500450 return;
451 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600452# endif /* PNG_USER_MEM_SUPPORTED */
453# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500454 farfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500455
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600456# else
457# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500458 hfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500459
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600460# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500461 free(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500462
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600463# endif
464# endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600465 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500466}
467
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600468/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500469 * 64K. However, zlib may allocate more then 64K if you don't tell
470 * it not to. See zconf.h and png.h for more information. zlib does
471 * need to allocate exactly 64K, so whatever you call here must
472 * have the ability to do that.
473 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600474
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500475PNG_FUNCTION(png_voidp,PNGAPI
476png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600477{
478 png_voidp ret;
479
480 ret = (png_malloc(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500481
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600482 if (ret != NULL)
483 png_memset(ret,0,(png_size_t)size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500484
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600485 return (ret);
486}
487
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500488PNG_FUNCTION(png_voidp,PNGAPI
489png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600490{
491 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500492
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600493# ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500495 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600496
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500497 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500498 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500499
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500500 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500501 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500502
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500503 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500504 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500505
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500506 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500507}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500508
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500509PNG_FUNCTION(png_voidp,PNGAPI
510png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500511{
512 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600513# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500514
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500515 if (png_ptr == NULL || size == 0)
516 return (NULL);
517
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600518# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500519 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600520 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600521# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500522 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600523 png_error(png_ptr, "Cannot Allocate > 64K");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500524
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600525 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600526# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600527 return NULL;
528 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600529# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600530
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500531 /* Check for overflow */
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600532# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500533
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500534 if (size != (unsigned long)size)
535 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500536
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 else
538 ret = farmalloc(size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500539
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600540# else
541# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500542 if (size != (unsigned long)size)
543 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500544
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545 else
546 ret = halloc(size, 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500547
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600548# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500549 if (size != (size_t)size)
550 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500551
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500552 else
553 ret = malloc((size_t)size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600554# endif
555# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600556
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600557# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600558 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600560# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600561
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600562 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600563}
564
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500565/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500566 * without taking any action.
567 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500568void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500569png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600570{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600572 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500573
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600574# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500575 if (png_ptr->free_fn != NULL)
576 {
577 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500578 return;
579 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500580
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500581 else
582 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500583}
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500584
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600585void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500586png_free_default(png_structp png_ptr, png_voidp ptr)
587{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500588 if (png_ptr == NULL || ptr == NULL)
589 return;
590
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600591# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500592
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600593# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500594 farfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500595
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600596# else
597# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500598 hfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500599
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600600# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500601 free(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500602
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600603# endif
604# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500605}
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600606#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600607
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500608/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500609 * function will set up png_malloc() to issue a png_warning and return NULL
610 * instead of issuing a png_error, if it fails to allocate the requested
611 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500612 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500613PNG_FUNCTION(png_voidp,PNGAPI
614png_malloc_warn,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500615{
616 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600617 png_uint_32 save_flags;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500618 if (png_ptr == NULL)
619 return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500620
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500621 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500622 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
623 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
624 png_ptr->flags=save_flags;
625 return(ptr);
626}
627
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500628
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500629#ifdef PNG_USER_MEM_SUPPORTED
630/* This function is called when the application wants to use another method
631 * of allocating and freeing memory.
632 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500633void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500634png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
635 malloc_fn, png_free_ptr free_fn)
636{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500637 if (png_ptr != NULL)
638 {
639 png_ptr->mem_ptr = mem_ptr;
640 png_ptr->malloc_fn = malloc_fn;
641 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600642 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500643}
644
645/* This function returns a pointer to the mem_ptr associated with the user
646 * functions. The application should free any memory associated with this
647 * pointer before png_write_destroy and png_read_destroy are called.
648 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500649png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500650png_get_mem_ptr(png_structp png_ptr)
651{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500652 if (png_ptr == NULL)
653 return (NULL);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500654
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500655 return ((png_voidp)png_ptr->mem_ptr);
656}
657#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600658#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */