blob: 21ec12f80141f7922e8b2b386dafd59c567acd1e [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-Pehrsonaaf377c2010-03-08 11:20:30 -06004 * Last changed in libpng 1.5.0 [March 8, 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);
Guy Schalnate5a37791996-06-05 15:50:50 -050047 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050048 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -050049 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050050 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050051
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060052# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050053 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050054 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050055 png_struct dummy_struct;
56 png_structp png_ptr = &dummy_struct;
57 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050058 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050059 }
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050060 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060061# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050062 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050063 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050064 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050065 return (struct_ptr);
66}
67
Guy Schalnate5a37791996-06-05 15:50:50 -050068/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050069void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050070png_destroy_struct(png_voidp struct_ptr)
71{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060072# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050073 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050074}
75
76/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050077void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050078png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
79 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050080{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060081# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060083 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060084# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050085 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050086 {
87 png_struct dummy_struct;
88 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050089 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050090 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050091 return;
92 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060093# endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050094 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060095 }
Guy Schalnate5a37791996-06-05 15:50:50 -050096}
97
Guy Schalnat0d580581995-07-20 02:43:20 -050098/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050099 * 64K. However, zlib may allocate more then 64K if you don't tell
100 * it not to. See zconf.h and png.h for more information. zlib does
101 * need to allocate exactly 64K, so whatever you call here must
102 * have the ability to do that.
103 *
104 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500105 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106 * memory stuff). zlib doesn't like this at all, so we have to
107 * detect and deal with it. This code should not be needed in
108 * Windows or OS/2 modes, and only in 16 bit mode. This code has
109 * been updated by Alexander Lehmann for version 0.89 to waste less
110 * memory.
111 *
112 * Note that we can't use png_size_t for the "size" declaration,
113 * since on some systems a png_size_t is a 16-bit quantity, and as a
114 * result, we would be truncating potentially larger memory requests
115 * (which should cause a fatal error) and introducing major problems.
116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500117png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600118png_calloc(png_structp png_ptr, png_alloc_size_t size)
119{
120 png_voidp ret;
121
122 ret = (png_malloc(png_ptr, size));
123 if (ret != NULL)
124 png_memset(ret,0,(png_size_t)size);
125 return (ret);
126}
127
128png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500129png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600130{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600131 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500132
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500134 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600135
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600136# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500137 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500138 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500139 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500141 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500142 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500143 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500144}
145
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500146png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500147png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500148{
149 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600150# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500151
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600152 if (png_ptr == NULL || size == 0)
153 return (NULL);
154
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600155# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500156 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500157 {
158 png_warning(png_ptr, "Cannot Allocate > 64K");
159 ret = NULL;
160 }
161 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600162# endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600163
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500164 if (size != (size_t)size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500165 ret = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500166 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600167 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600169 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500170 /* Try to see if we need to do any of this fancy stuff */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600173 {
174 int num_blocks;
175 png_uint_32 total_size;
176 png_bytep table;
177 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600178 png_byte huge * hptr;
179
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600181 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600183 ret = NULL;
184 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600185
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500186 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500187 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
188 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600189 num_blocks = 1;
190 if (png_ptr->zlib_mem_level >= 7)
191 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
192 else
193 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600194
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600196
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600197 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600198
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600200 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600201# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500202 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600203 png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600204 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500205 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600206# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600207 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600209
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600212# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500213 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600214 png_error(png_ptr,
215 "Farmalloc didn't return normalized pointer");
216 else
217 png_warning(png_ptr,
218 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600219# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600220 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600221 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600222
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600223 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500224 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500225 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500226
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 if (png_ptr->offset_table_ptr == NULL)
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-Pehrsone3f3c4e2010-02-07 18:08:50 -0600231 png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600232 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500233 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600234# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600235 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500236 }
237
238 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500240 {
241 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500242 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600243 }
244 for (i = 0; i < num_blocks; i++)
245 {
246 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500247 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600248 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600249
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600250 png_ptr->offset_table_number = num_blocks;
251 png_ptr->offset_table_count = 0;
252 png_ptr->offset_table_count_free = 0;
253 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600254 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500255
256 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600257 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600258# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500259 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500260 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600261 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500262 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600263# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600264 return (NULL);
265 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500266
267 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600268 }
269 else
270 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500271
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600272# ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500273 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500274 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500275 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500276 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600277 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500278 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500279 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600280# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500281
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600282 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500283}
284
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500285/* Free a pointer allocated by png_malloc(). In the default
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500286 * configuration, png_ptr is not used, but is passed in case it
287 * is needed. If ptr is NULL, return without taking any action.
288 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500289void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500290png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500291{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500292 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600293 return;
294
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600295# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500296 if (png_ptr->free_fn != NULL)
297 {
298 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500299 return;
300 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500301 else
302 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500303}
304
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500305void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500306png_free_default(png_structp png_ptr, png_voidp ptr)
307{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600308# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600309
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500310 if (png_ptr == NULL || ptr == NULL)
311 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600312
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600316
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 for (i = 0; i < png_ptr->offset_table_count; i++)
318 {
319 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600320 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 ptr = NULL;
322 png_ptr->offset_table_count_free++;
323 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600324 }
325 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
327 {
328 farfree(png_ptr->offset_table);
329 farfree(png_ptr->offset_table_ptr);
330 png_ptr->offset_table = NULL;
331 png_ptr->offset_table_ptr = NULL;
332 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600333 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334
335 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600336 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600338 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600339}
340
341#else /* Not the Borland DOS special memory handler */
342
Guy Schalnate5a37791996-06-05 15:50:50 -0500343/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600344 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500345 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500346png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600347png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500348{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600349# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500350 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500351}
352
353/* Allocate memory for a png_struct or a png_info. The malloc and
354 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500355 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500356png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500357png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500358{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600359# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500360 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500361 png_voidp struct_ptr;
362
363 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500364 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500365 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500366 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500367 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500368 return (NULL);
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 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500372 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600373 png_struct dummy_struct;
374 png_structp png_ptr = &dummy_struct;
375 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600376 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500377 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500378 png_memset(struct_ptr, 0, size);
379 return (struct_ptr);
380 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600381# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500382
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600383# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500384 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600385# else
386# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500387 struct_ptr = (png_voidp)halloc(size, 1);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600388# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500389 struct_ptr = (png_voidp)malloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600390# endif
391# endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500392 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500394
395 return (struct_ptr);
396}
397
398
399/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500400void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500401png_destroy_struct(png_voidp struct_ptr)
402{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600403# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500404 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500405}
406
407/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500408void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500409png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
410 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500411{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600412# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600414 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600415# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500416 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500417 {
418 png_struct dummy_struct;
419 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500420 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500421 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500422 return;
423 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600424# endif /* PNG_USER_MEM_SUPPORTED */
425# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500426 farfree(struct_ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600427# else
428# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500429 hfree(struct_ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600430# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500431 free(struct_ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600432# endif
433# endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600434 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500435}
436
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600437/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500438 * 64K. However, zlib may allocate more then 64K if you don't tell
439 * it not to. See zconf.h and png.h for more information. zlib does
440 * need to allocate exactly 64K, so whatever you call here must
441 * have the ability to do that.
442 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600443
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500444png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600445png_calloc(png_structp png_ptr, png_alloc_size_t size)
446{
447 png_voidp ret;
448
449 ret = (png_malloc(png_ptr, size));
450 if (ret != NULL)
451 png_memset(ret,0,(png_size_t)size);
452 return (ret);
453}
454
455png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500456png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600457{
458 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500459
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600460# ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500462 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600463
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500464 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500465 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500466 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500467 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500468 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500469 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500470 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500471}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500472
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600473png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500474png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500475{
476 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600477# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500478
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500479 if (png_ptr == NULL || size == 0)
480 return (NULL);
481
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600482# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500483 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600484 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600485# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500486 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600487 png_error(png_ptr, "Cannot Allocate > 64K");
488 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600489# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600490 return NULL;
491 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600492# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600493
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500494 /* Check for overflow */
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600495# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500496 if (size != (unsigned long)size)
497 ret = NULL;
498 else
499 ret = farmalloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600500# else
501# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500502 if (size != (unsigned long)size)
503 ret = NULL;
504 else
505 ret = halloc(size, 1);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600506# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500507 if (size != (size_t)size)
508 ret = NULL;
509 else
510 ret = malloc((size_t)size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600511# endif
512# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600513
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600514# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600515 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600517# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600519 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600520}
521
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500522/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500523 * without taking any action.
524 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500525void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500526png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600527{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600529 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500530
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600531# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500532 if (png_ptr->free_fn != NULL)
533 {
534 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500535 return;
536 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 else
538 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500539}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600540void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500541png_free_default(png_structp png_ptr, png_voidp ptr)
542{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500543 if (png_ptr == NULL || ptr == NULL)
544 return;
545
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600546# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500547
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600548# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500549 farfree(ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600550# else
551# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500552 hfree(ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600553# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500554 free(ptr);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600555# endif
556# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500557}
558
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600559#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600560
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500561/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500562 * function will set up png_malloc() to issue a png_warning and return NULL
563 * instead of issuing a png_error, if it fails to allocate the requested
564 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500565 */
566png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500567png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500568{
569 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600570 png_uint_32 save_flags;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500571 if (png_ptr == NULL)
572 return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500573
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500574 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500575 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
576 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
577 png_ptr->flags=save_flags;
578 return(ptr);
579}
580
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500581
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500582#ifdef PNG_USER_MEM_SUPPORTED
583/* This function is called when the application wants to use another method
584 * of allocating and freeing memory.
585 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500586void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500587png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
588 malloc_fn, png_free_ptr free_fn)
589{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500590 if (png_ptr != NULL)
591 {
592 png_ptr->mem_ptr = mem_ptr;
593 png_ptr->malloc_fn = malloc_fn;
594 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600595 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500596}
597
598/* This function returns a pointer to the mem_ptr associated with the user
599 * functions. The application should free any memory associated with this
600 * pointer before png_write_destroy and png_read_destroy are called.
601 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500602png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500603png_get_mem_ptr(png_structp png_ptr)
604{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500605 if (png_ptr == NULL)
606 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500607 return ((png_voidp)png_ptr->mem_ptr);
608}
609#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600610#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */