blob: 14927f795399b5c7fce167845d9a3aa06d699707 [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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-Pehrson3e61d792009-06-24 09:31:28 -05009 * This code is released under the zlib-libpng license.
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050010 * For conditions of distribution and use, see copyright notice, disclaimer,
11 * 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
Guy Schalnat0d580581995-07-20 02:43:20 -050020#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050022#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060023
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-Pehrsonf7d1a171998-06-06 15:31:35 -050033#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{
41#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-Pehrsonf7d1a171998-06-06 15:31:35 -050052#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-Pehrsonf7d1a171998-06-06 15:31:35 -050061#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-Pehrsonf7d1a171998-06-06 15:31:35 -050072#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{
81#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060083 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050084#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 }
93#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-Pehrson0ffb71a2009-02-28 06:08:20 -0600117#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500118png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600119png_calloc(png_structp png_ptr, png_alloc_size_t size)
120{
121 png_voidp ret;
122
123 ret = (png_malloc(png_ptr, size));
124 if (ret != NULL)
125 png_memset(ret,0,(png_size_t)size);
126 return (ret);
127}
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -0600128#endif
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600129
130png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500131png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600132{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500136 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600137
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500138#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500139 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500141 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500143 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500144 png_error(png_ptr, "Out of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500145 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500146}
147
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500148png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500149png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500150{
151 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600152#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500153
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600154 if (png_ptr == NULL || size == 0)
155 return (NULL);
156
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600157#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500158 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500159 {
160 png_warning(png_ptr, "Cannot Allocate > 64K");
161 ret = NULL;
162 }
163 else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600164#endif
165
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500166 if (size != (size_t)size)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500167 ret = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500168 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600169 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500172 /* Try to see if we need to do any of this fancy stuff */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600173 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600175 {
176 int num_blocks;
177 png_uint_32 total_size;
178 png_bytep table;
179 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600180 png_byte huge * hptr;
181
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600183 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600185 ret = NULL;
186 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600187
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500188 if (png_ptr->zlib_window_bits > 14)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500189 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
190 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600191 num_blocks = 1;
192 if (png_ptr->zlib_mem_level >= 7)
193 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
194 else
195 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600196
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600198
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600199 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600200
Andreas Dilger47a0c421997-05-16 02:46:07 -0500201 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600202 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500203#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500204 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500205 png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600206 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500207 png_warning(png_ptr, "Out Of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500208#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600209 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600210 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600211
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600213 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500214#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500215 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600216 png_error(png_ptr,
217 "Farmalloc didn't return normalized pointer");
218 else
219 png_warning(png_ptr,
220 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500221#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600222 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600223 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600224
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600225 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500227 png_sizeof(png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500228
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500231#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500232 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500233 png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600234 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500235 png_warning(png_ptr, "Out Of memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500236#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600237 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500238 }
239
240 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500242 {
243 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500244 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600245 }
246 for (i = 0; i < num_blocks; i++)
247 {
248 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500249 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600250 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600251
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600252 png_ptr->offset_table_number = num_blocks;
253 png_ptr->offset_table_count = 0;
254 png_ptr->offset_table_count_free = 0;
255 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600256 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500257
258 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600259 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500260#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500261 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500262 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600263 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500264 png_warning(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500265#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600266 return (NULL);
267 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500268
269 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600270 }
271 else
272 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500273
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500274#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500275 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500276 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500277 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500278 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600279 else
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500280 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500281 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500282#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500283
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600284 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500285}
286
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500287/* Free a pointer allocated by png_malloc(). In the default
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500288 * configuration, png_ptr is not used, but is passed in case it
289 * is needed. If ptr is NULL, return without taking any action.
290 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500291void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500292png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500293{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500294 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600295 return;
296
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500297#ifdef PNG_USER_MEM_SUPPORTED
298 if (png_ptr->free_fn != NULL)
299 {
300 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500301 return;
302 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500303 else
304 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500305}
306
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500307void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500308png_free_default(png_structp png_ptr, png_voidp ptr)
309{
310#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600311
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500312 if (png_ptr == NULL || ptr == NULL)
313 return;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600314
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600316 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600318
Andreas Dilger47a0c421997-05-16 02:46:07 -0500319 for (i = 0; i < png_ptr->offset_table_count; i++)
320 {
321 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600322 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323 ptr = NULL;
324 png_ptr->offset_table_count_free++;
325 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600326 }
327 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
329 {
330 farfree(png_ptr->offset_table);
331 farfree(png_ptr->offset_table_ptr);
332 png_ptr->offset_table = NULL;
333 png_ptr->offset_table_ptr = NULL;
334 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600335 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336
337 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600338 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600340 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600341}
342
343#else /* Not the Borland DOS special memory handler */
344
Guy Schalnate5a37791996-06-05 15:50:50 -0500345/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600346 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500347 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500348png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600349png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500350{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500351#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500352 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500353}
354
355/* Allocate memory for a png_struct or a png_info. The malloc and
356 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500357 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500358png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500359png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500360{
361#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500363 png_voidp struct_ptr;
364
365 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500366 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500367 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500368 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500369 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500370 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500371
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500372#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500374 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600375 png_struct dummy_struct;
376 png_structp png_ptr = &dummy_struct;
377 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600378 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500379 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500380 png_memset(struct_ptr, 0, size);
381 return (struct_ptr);
382 }
383#endif /* PNG_USER_MEM_SUPPORTED */
384
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500385#if defined(__TURBOC__) && !defined(__FLAT__)
386 struct_ptr = (png_voidp)farmalloc(size);
387#else
388# if defined(_MSC_VER) && defined(MAXSEG_64K)
389 struct_ptr = (png_voidp)halloc(size, 1);
390# else
391 struct_ptr = (png_voidp)malloc(size);
392# endif
393#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500394 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500396
397 return (struct_ptr);
398}
399
400
401/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500402void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500403png_destroy_struct(png_voidp struct_ptr)
404{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500405#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500406 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500407}
408
409/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500410void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500411png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
412 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500413{
414#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600416 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500417#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500418 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500419 {
420 png_struct dummy_struct;
421 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500422 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500423 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424 return;
425 }
426#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500427#if defined(__TURBOC__) && !defined(__FLAT__)
428 farfree(struct_ptr);
429#else
430# if defined(_MSC_VER) && defined(MAXSEG_64K)
431 hfree(struct_ptr);
432# else
433 free(struct_ptr);
434# endif
435#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600436 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500437}
438
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600439/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500440 * 64K. However, zlib may allocate more then 64K if you don't tell
441 * it not to. See zconf.h and png.h for more information. zlib does
442 * need to allocate exactly 64K, so whatever you call here must
443 * have the ability to do that.
444 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600445
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500446png_voidp PNGAPI
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600447png_calloc(png_structp png_ptr, png_alloc_size_t size)
448{
449 png_voidp ret;
450
451 ret = (png_malloc(png_ptr, size));
452 if (ret != NULL)
453 png_memset(ret,0,(png_size_t)size);
454 return (ret);
455}
456
457png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500458png_malloc(png_structp png_ptr, png_alloc_size_t size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600459{
460 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500461
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500462#ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500463 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500464 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600465
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500466 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500467 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500468 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500469 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500470 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500471 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500472 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500473}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500474
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600475png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500476png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500477{
478 png_voidp ret;
479#endif /* PNG_USER_MEM_SUPPORTED */
480
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500481 if (png_ptr == NULL || size == 0)
482 return (NULL);
483
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600484#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500485 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600486 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500487#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500488 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600489 png_error(png_ptr, "Cannot Allocate > 64K");
490 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500491#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600492 return NULL;
493 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494#endif
495
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500496 /* Check for overflow */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500497#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500498 if (size != (unsigned long)size)
499 ret = NULL;
500 else
501 ret = farmalloc(size);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500502#else
503# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500504 if (size != (unsigned long)size)
505 ret = NULL;
506 else
507 ret = halloc(size, 1);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500508# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500509 if (size != (size_t)size)
510 ret = NULL;
511 else
512 ret = malloc((size_t)size);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500513# endif
514#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600515
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500516#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600517 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500519#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600520
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600521 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600522}
523
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500524/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525 * without taking any action.
526 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500528png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600529{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500532
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500533#ifdef PNG_USER_MEM_SUPPORTED
534 if (png_ptr->free_fn != NULL)
535 {
536 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500537 return;
538 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500539 else
540 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500541}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600542void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500543png_free_default(png_structp png_ptr, png_voidp ptr)
544{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500545 if (png_ptr == NULL || ptr == NULL)
546 return;
547
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500548#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500549
550#if defined(__TURBOC__) && !defined(__FLAT__)
551 farfree(ptr);
552#else
553# if defined(_MSC_VER) && defined(MAXSEG_64K)
554 hfree(ptr);
555# else
556 free(ptr);
557# endif
558#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500559}
560
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600561#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600562
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500563/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500564 * function will set up png_malloc() to issue a png_warning and return NULL
565 * instead of issuing a png_error, if it fails to allocate the requested
566 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500567 */
568png_voidp PNGAPI
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500569png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500570{
571 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600572 png_uint_32 save_flags;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573 if (png_ptr == NULL)
574 return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500575
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500576 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500577 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
578 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
579 png_ptr->flags=save_flags;
580 return(ptr);
581}
582
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500583
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500584#ifdef PNG_USER_MEM_SUPPORTED
585/* This function is called when the application wants to use another method
586 * of allocating and freeing memory.
587 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500588void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500589png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
590 malloc_fn, png_free_ptr free_fn)
591{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500592 if (png_ptr != NULL)
593 {
594 png_ptr->mem_ptr = mem_ptr;
595 png_ptr->malloc_fn = malloc_fn;
596 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600597 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500598}
599
600/* This function returns a pointer to the mem_ptr associated with the user
601 * functions. The application should free any memory associated with this
602 * pointer before png_write_destroy and png_read_destroy are called.
603 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500605png_get_mem_ptr(png_structp png_ptr)
606{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500607 if (png_ptr == NULL)
608 return (NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500609 return ((png_voidp)png_ptr->mem_ptr);
610}
611#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600612#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */