blob: a63f5c4c4d9b4461477860d1c6bd98b600cb7aa7 [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-Pehrsonf24daf22010-05-06 09:44:04 -05004 * Last changed in libpng 1.5.0 [May 6, 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-Pehrson75294572000-05-06 14:09:57 -050030png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060031png_create_struct(int type)
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-Pehrson75294572000-05-06 14:09:57 -050038png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050039png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050040{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060041# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060042 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050043 png_voidp struct_ptr;
44
45 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050046 size = png_sizeof(png_info);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050047
Guy Schalnate5a37791996-06-05 15:50:50 -050048 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050049 size = png_sizeof(png_struct);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050050
Guy Schalnate5a37791996-06-05 15:50:50 -050051 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050052 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050053
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060054# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050055 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050056 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050057 png_struct dummy_struct;
58 png_structp png_ptr = &dummy_struct;
59 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050060 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050061 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050062
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050063 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060064# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050065 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050066 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050067 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050068
Guy Schalnate5a37791996-06-05 15:50:50 -050069 return (struct_ptr);
70}
71
Guy Schalnate5a37791996-06-05 15:50:50 -050072/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050073void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050074png_destroy_struct(png_voidp struct_ptr)
75{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060076# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050077 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050078}
79
80/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050081void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050082png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
83 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050084{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060085# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060087 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060088# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050089 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050090 {
91 png_struct dummy_struct;
92 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050093 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050094 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050095 return;
96 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050097
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060098# endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050099 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600100 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500101}
102
Guy Schalnat0d580581995-07-20 02:43:20 -0500103/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 * 64K. However, zlib may allocate more then 64K if you don't tell
105 * it not to. See zconf.h and png.h for more information. zlib does
106 * need to allocate exactly 64K, so whatever you call here must
107 * have the ability to do that.
108 *
109 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500110 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111 * memory stuff). zlib doesn't like this at all, so we have to
112 * detect and deal with it. This code should not be needed in
113 * Windows or OS/2 modes, and only in 16 bit mode. This code has
114 * been updated by Alexander Lehmann for version 0.89 to waste less
115 * memory.
116 *
117 * Note that we can't use png_size_t for the "size" declaration,
118 * since on some systems a png_size_t is a 16-bit quantity, and as a
119 * result, we would be truncating potentially larger memory requests
120 * (which should cause a fatal error) and introducing major problems.
121 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500122png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600123png_calloc(png_structp png_ptr, png_alloc_size_t size)
124{
125 png_voidp ret;
126
127 ret = (png_malloc(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600129 if (ret != NULL)
130 png_memset(ret,0,(png_size_t)size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500131
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600132 return (ret);
133}
134
135png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500136png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600137{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600138 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500139
Andreas Dilger47a0c421997-05-16 02:46:07 -0500140 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500141 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600142
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600143# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500144 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500146
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500147 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500149
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500150 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500151 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500152
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500153 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500154}
155
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500157png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500158{
159 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600160# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500161
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600162 if (png_ptr == NULL || size == 0)
163 return (NULL);
164
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600165# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500166 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500167 {
168 png_warning(png_ptr, "Cannot Allocate > 64K");
169 ret = NULL;
170 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500171
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500172 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600173# endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600174
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500175 if (size != (size_t)size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500176 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500177
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500178 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500182 /* Try to see if we need to do any of this fancy stuff */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600185 {
186 int num_blocks;
187 png_uint_32 total_size;
188 png_bytep table;
189 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600190 png_byte huge * hptr;
191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600193 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600194 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600195 ret = NULL;
196 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600197
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500198 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500199 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500200
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500201 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600202 num_blocks = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500203
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600204 if (png_ptr->zlib_mem_level >= 7)
205 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500206
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 else
208 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600209
Guy Schalnate5a37791996-06-05 15:50:50 -0500210 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600211
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600212 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600213
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600215 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600216# 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-Pehrsone3f3c4e2010-02-07 18:08:50 -0600218 png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500219
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600220 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500221 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600222# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600223 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600224 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600225
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600228# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500229 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600230 png_error(png_ptr,
231 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500232
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600233 else
234 png_warning(png_ptr,
235 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600236# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600237 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600238 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600239
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600240 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500242 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500243
Andreas Dilger47a0c421997-05-16 02:46:07 -0500244 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600245 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600246# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500247 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600248 png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600250 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500251 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600252# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600253 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500254 }
255
256 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500257 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500258 {
259 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500260 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600261 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500262
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600263 for (i = 0; i < num_blocks; i++)
264 {
265 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500266 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600267 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600268
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600269 png_ptr->offset_table_number = num_blocks;
270 png_ptr->offset_table_count = 0;
271 png_ptr->offset_table_count_free = 0;
272 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600273 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500274
275 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600276 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600277# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500278 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500279 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500280
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600281 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500282 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600283# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600284 return (NULL);
285 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500286
287 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600288 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500289
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600290 else
291 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500292
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600293# ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500294 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500295 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500296 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500297 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500298
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600299 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500300 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500301 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600302# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500303
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600304 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500305}
306
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500307/* Free a pointer allocated by png_malloc(). In the default
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500308 * configuration, png_ptr is not used, but is passed in case it
309 * is needed. If ptr is NULL, return without taking any action.
310 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500311void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500312png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500313{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600315 return;
316
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600317# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500318 if (png_ptr->free_fn != NULL)
319 {
320 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500321 return;
322 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500323
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500324 else
325 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500326}
327
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500328void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500329png_free_default(png_structp png_ptr, png_voidp ptr)
330{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600331# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600332
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500333 if (png_ptr == NULL || ptr == NULL)
334 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600335
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600337 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600339
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 for (i = 0; i < png_ptr->offset_table_count; i++)
341 {
342 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600343 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 ptr = NULL;
345 png_ptr->offset_table_count_free++;
346 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600347 }
348 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
350 {
351 farfree(png_ptr->offset_table);
352 farfree(png_ptr->offset_table_ptr);
353 png_ptr->offset_table = NULL;
354 png_ptr->offset_table_ptr = NULL;
355 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600356 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500357
358 if (ptr != NULL)
359 farfree(ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600360}
361
362#else /* Not the Borland DOS special memory handler */
363
Guy Schalnate5a37791996-06-05 15:50:50 -0500364/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600365 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500366 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500367png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600368png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500369{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600370# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500371 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500372}
373
374/* Allocate memory for a png_struct or a png_info. The malloc and
375 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500376 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500377png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500378png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500379{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600380# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500382 png_voidp struct_ptr;
383
384 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500385 size = png_sizeof(png_info);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500386
Guy Schalnate5a37791996-06-05 15:50:50 -0500387 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500388 size = png_sizeof(png_struct);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500389
Guy Schalnate5a37791996-06-05 15:50:50 -0500390 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500391 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500392
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600393# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500394 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500395 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600396 png_struct dummy_struct;
397 png_structp png_ptr = &dummy_struct;
398 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600399 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500400
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500401 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500402 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500403
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500404 return (struct_ptr);
405 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600406# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500407
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600408# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500409 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600410# else
411# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500412 struct_ptr = (png_voidp)halloc(size, 1);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600413# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500414 struct_ptr = (png_voidp)malloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600415# endif
416# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500417
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500418 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500419 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500420
421 return (struct_ptr);
422}
423
424
425/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500426void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500427png_destroy_struct(png_voidp struct_ptr)
428{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600429# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500430 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500431}
432
433/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500434void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500435png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
436 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500437{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600438# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600440 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600441# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500442 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500443 {
444 png_struct dummy_struct;
445 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500446 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500447 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500448 return;
449 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600450# endif /* PNG_USER_MEM_SUPPORTED */
451# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500452 farfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500453
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600454# else
455# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500456 hfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500457
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600458# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500459 free(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500460
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600461# endif
462# endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600463 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500464}
465
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600466/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500467 * 64K. However, zlib may allocate more then 64K if you don't tell
468 * it not to. See zconf.h and png.h for more information. zlib does
469 * need to allocate exactly 64K, so whatever you call here must
470 * have the ability to do that.
471 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600472
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500473png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600474png_calloc(png_structp png_ptr, png_alloc_size_t size)
475{
476 png_voidp ret;
477
478 ret = (png_malloc(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500479
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600480 if (ret != NULL)
481 png_memset(ret,0,(png_size_t)size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500482
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600483 return (ret);
484}
485
486png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500487png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488{
489 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500490
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600491# ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500492 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500493 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500495 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500496 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500497
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500498 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500499 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500501 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500502 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500503
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500504 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500505}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500506
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600507png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500508png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500509{
510 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600511# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500512
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500513 if (png_ptr == NULL || size == 0)
514 return (NULL);
515
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600516# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500517 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600518 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600519# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500520 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600521 png_error(png_ptr, "Cannot Allocate > 64K");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500522
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600523 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600524# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600525 return NULL;
526 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600527# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600528
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500529 /* Check for overflow */
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600530# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500531
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500532 if (size != (unsigned long)size)
533 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500534
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500535 else
536 ret = farmalloc(size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500537
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600538# else
539# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500540 if (size != (unsigned long)size)
541 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500542
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500543 else
544 ret = halloc(size, 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500545
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600546# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500547 if (size != (size_t)size)
548 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500549
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500550 else
551 ret = malloc((size_t)size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600552# endif
553# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600554
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600555# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600556 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600557 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600558# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600559
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600560 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600561}
562
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500563/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500564 * without taking any action.
565 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500566void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500567png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600568{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600570 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500571
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600572# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500573 if (png_ptr->free_fn != NULL)
574 {
575 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500576 return;
577 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500578
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500579 else
580 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500581}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600582void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500583png_free_default(png_structp png_ptr, png_voidp ptr)
584{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500585 if (png_ptr == NULL || ptr == NULL)
586 return;
587
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600588# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500589
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600590# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500591 farfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500592
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600593# else
594# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500595 hfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500596
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600597# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500598 free(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500599
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600600# endif
601# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500602}
603
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600604#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600605
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500606/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500607 * function will set up png_malloc() to issue a png_warning and return NULL
608 * instead of issuing a png_error, if it fails to allocate the requested
609 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500610 */
611png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500612png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500613{
614 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600615 png_uint_32 save_flags;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500616 if (png_ptr == NULL)
617 return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500618
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500619 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500620 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
621 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
622 png_ptr->flags=save_flags;
623 return(ptr);
624}
625
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500626
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500627#ifdef PNG_USER_MEM_SUPPORTED
628/* This function is called when the application wants to use another method
629 * of allocating and freeing memory.
630 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500631void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500632png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
633 malloc_fn, png_free_ptr free_fn)
634{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500635 if (png_ptr != NULL)
636 {
637 png_ptr->mem_ptr = mem_ptr;
638 png_ptr->malloc_fn = malloc_fn;
639 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600640 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500641}
642
643/* This function returns a pointer to the mem_ptr associated with the user
644 * functions. The application should free any memory associated with this
645 * pointer before png_write_destroy and png_read_destroy are called.
646 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500647png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500648png_get_mem_ptr(png_structp png_ptr)
649{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500650 if (png_ptr == NULL)
651 return (NULL);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500652
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500653 return ((png_voidp)png_ptr->mem_ptr);
654}
655#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600656#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */