blob: 837fb30b5569828a5a9ef029678007705b4af413 [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-Pehrson5fea36f2004-07-28 08:20:44 -05004 * libpng version 1.2.6beta4 - July 28, 2004
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05006 * Copyright (c) 1998-2004 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050010 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050011 * need special memory handling are expected to supply replacement
12 * functions for png_malloc() and png_free(), and to use
13 * png_create_read_struct_2() and png_create_write_struct_2() to
14 * identify the replacement functions.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060015 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
17#define PNG_INTERNAL
18#include "png.h"
19
Guy Schalnat4ee97b01996-01-16 01:51:56 -060020/* Borland DOS special memory handler */
21#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
22/* if you change this, be sure to change the one in png.h also */
23
Guy Schalnate5a37791996-06-05 15:50:50 -050024/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060025 by a single call to calloc() if this is thought to improve performance. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050026png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060027png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050028{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050029#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050030 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050031}
32
33/* Alternate version of png_create_struct, for use with user-defined malloc. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050034png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050035png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050036{
37#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050039 png_voidp struct_ptr;
40
41 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050042 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -050043 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050044 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -050045 else
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050046 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050047
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050048#ifdef PNG_USER_MEM_SUPPORTED
49 if(malloc_fn != NULL)
50 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050051 png_struct dummy_struct;
52 png_structp png_ptr = &dummy_struct;
53 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -060054 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050055 }
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050056 else
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050057#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050058 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050059 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050060 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050061 return (struct_ptr);
62}
63
Guy Schalnate5a37791996-06-05 15:50:50 -050064/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050065void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050066png_destroy_struct(png_voidp struct_ptr)
67{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050068#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050069 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050070}
71
72/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050073void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050074png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
75 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050076{
77#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060079 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050080#ifdef PNG_USER_MEM_SUPPORTED
81 if(free_fn != NULL)
82 {
83 png_struct dummy_struct;
84 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050085 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050086 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050087 return;
88 }
89#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050090 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060091 }
Guy Schalnate5a37791996-06-05 15:50:50 -050092}
93
Guy Schalnat0d580581995-07-20 02:43:20 -050094/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 * 64K. However, zlib may allocate more then 64K if you don't tell
96 * it not to. See zconf.h and png.h for more information. zlib does
97 * need to allocate exactly 64K, so whatever you call here must
98 * have the ability to do that.
99 *
100 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500101 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 * memory stuff). zlib doesn't like this at all, so we have to
103 * detect and deal with it. This code should not be needed in
104 * Windows or OS/2 modes, and only in 16 bit mode. This code has
105 * been updated by Alexander Lehmann for version 0.89 to waste less
106 * memory.
107 *
108 * Note that we can't use png_size_t for the "size" declaration,
109 * since on some systems a png_size_t is a 16-bit quantity, and as a
110 * result, we would be truncating potentially larger memory requests
111 * (which should cause a fatal error) and introducing major problems.
112 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500113
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500114png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500115png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600116{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600117 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500118
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500120 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600121
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500122#ifdef PNG_USER_MEM_SUPPORTED
123 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600124 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500125 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500126 ret = (png_malloc_default(png_ptr, size));
127 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
128 png_error(png_ptr, "Out of memory!");
129 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500130}
131
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500132png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500133png_malloc_default(png_structp png_ptr, png_uint_32 size)
134{
135 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600136#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500137
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600138#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600139 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500140 {
141 png_warning(png_ptr, "Cannot Allocate > 64K");
142 ret = NULL;
143 }
144 else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600145#endif
146
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500147 if (size != (size_t)size)
148 ret = NULL;
149 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600150 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600152 {
153 /* try to see if we need to do any of this fancy stuff */
154 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600156 {
157 int num_blocks;
158 png_uint_32 total_size;
159 png_bytep table;
160 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600161 png_byte huge * hptr;
162
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600164 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600165 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600166 ret = NULL;
167 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600168
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500169 if(png_ptr->zlib_window_bits > 14)
170 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
171 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600172 num_blocks = 1;
173 if (png_ptr->zlib_mem_level >= 7)
174 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
175 else
176 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600177
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600179
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600181
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500184#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500185 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600186 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
187 else
188 png_warning(png_ptr, "Out Of Memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500189#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600190 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600191 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600192
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600194 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500195#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500196 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600197 png_error(png_ptr,
198 "Farmalloc didn't return normalized pointer");
199 else
200 png_warning(png_ptr,
201 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500202#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600203 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600204 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600205
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600206 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500208 png_sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500209
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500212#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, "Out Of memory."); /* Note "O" and "M" */
215 else
216 png_warning(png_ptr, "Out Of memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500217#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600218 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500219 }
220
221 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500223 {
224 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500225 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600226 }
227 for (i = 0; i < num_blocks; i++)
228 {
229 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500230 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600231 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600232
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600233 png_ptr->offset_table_number = num_blocks;
234 png_ptr->offset_table_count = 0;
235 png_ptr->offset_table_count_free = 0;
236 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600237 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500238
239 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600240 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500241#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500242 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600243 png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
244 else
245 png_warning(png_ptr, "Out of Memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500246#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600247 return (NULL);
248 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500249
250 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600251 }
252 else
253 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500254
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500255#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500256 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500257 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500258 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600259 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
260 else
261 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500262 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500263#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500264
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600265 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500266}
267
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500268/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600269 configuration, png_ptr is not used, but is passed in case it
270 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500271void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500272png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500273{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600275 return;
276
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500277#ifdef PNG_USER_MEM_SUPPORTED
278 if (png_ptr->free_fn != NULL)
279 {
280 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500281 return;
282 }
283 else png_free_default(png_ptr, ptr);
284}
285
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500286void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500287png_free_default(png_structp png_ptr, png_voidp ptr)
288{
289#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600290
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600292 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600294
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295 for (i = 0; i < png_ptr->offset_table_count; i++)
296 {
297 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600298 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 ptr = NULL;
300 png_ptr->offset_table_count_free++;
301 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600302 }
303 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
305 {
306 farfree(png_ptr->offset_table);
307 farfree(png_ptr->offset_table_ptr);
308 png_ptr->offset_table = NULL;
309 png_ptr->offset_table_ptr = NULL;
310 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600311 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312
313 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600316 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600317}
318
319#else /* Not the Borland DOS special memory handler */
320
Guy Schalnate5a37791996-06-05 15:50:50 -0500321/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600322 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500323 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500324png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600325png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500326{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500327#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500328 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500329}
330
331/* Allocate memory for a png_struct or a png_info. The malloc and
332 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500333 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500334png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500335png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500336{
337#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500339 png_voidp struct_ptr;
340
341 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500342 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500343 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500344 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500345 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500346 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500347
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500348#ifdef PNG_USER_MEM_SUPPORTED
349 if(malloc_fn != NULL)
350 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600351 png_struct dummy_struct;
352 png_structp png_ptr = &dummy_struct;
353 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600354 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500355 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500356 png_memset(struct_ptr, 0, size);
357 return (struct_ptr);
358 }
359#endif /* PNG_USER_MEM_SUPPORTED */
360
Guy Schalnate5a37791996-06-05 15:50:50 -0500361#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500362 struct_ptr = (png_voidp)farmalloc(size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500363#else
364# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500365 struct_ptr = (png_voidp)halloc(size,1);
Guy Schalnate5a37791996-06-05 15:50:50 -0500366# else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500367 struct_ptr = (png_voidp)malloc(size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500368# endif
369#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500370 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500371 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500372
373 return (struct_ptr);
374}
375
376
377/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500378void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500379png_destroy_struct(png_voidp struct_ptr)
380{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500381#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500382 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500383}
384
385/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500386void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500387png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
388 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500389{
390#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600392 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500393#ifdef PNG_USER_MEM_SUPPORTED
394 if(free_fn != NULL)
395 {
396 png_struct dummy_struct;
397 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500398 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500399 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500400 return;
401 }
402#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500403#if defined(__TURBOC__) && !defined(__FLAT__)
404 farfree(struct_ptr);
405#else
406# if defined(_MSC_VER) && defined(MAXSEG_64K)
407 hfree(struct_ptr);
408# else
409 free(struct_ptr);
410# endif
411#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600412 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500413}
414
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600415/* Allocate memory. For reasonable files, size should never exceed
416 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600418 need to allocate exactly 64K, so whatever you call here must
419 have the ability to do that. */
420
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500421png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500422png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600423{
424 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500425
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500427 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600428
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500429#ifdef PNG_USER_MEM_SUPPORTED
430 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500431 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500432 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500433 ret = (png_malloc_default(png_ptr, size));
434 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
435 png_error(png_ptr, "Out of Memory!");
436 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500437}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500438
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600439png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500440png_malloc_default(png_structp png_ptr, png_uint_32 size)
441{
442 png_voidp ret;
443#endif /* PNG_USER_MEM_SUPPORTED */
444
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600445#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600446 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600447 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500448#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600449 if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
450 png_error(png_ptr, "Cannot Allocate > 64K");
451 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500452#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600453 return NULL;
454 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600455#endif
456
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500457 /* Check for overflow */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600458#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500459 if (size != (unsigned long)size)
460 ret = NULL;
461 else
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600462 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600463#else
464# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500465 if (size != (unsigned long)size)
466 ret = NULL;
467 else
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600468 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600469# else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500470 if (size != (size_t)size)
471 ret = NULL;
472 else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600473 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600474# endif
475#endif
476
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500477#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600478 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500480#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600481
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600482 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600483}
484
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500485/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
486 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500487void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500488png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600489{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600491 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500492
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500493#ifdef PNG_USER_MEM_SUPPORTED
494 if (png_ptr->free_fn != NULL)
495 {
496 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500497 return;
498 }
499 else png_free_default(png_ptr, ptr);
500}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600501void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502png_free_default(png_structp png_ptr, png_voidp ptr)
503{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500504 if (png_ptr == NULL || ptr == NULL)
505 return;
506
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500507#endif /* PNG_USER_MEM_SUPPORTED */
508
Guy Schalnat6d764711995-12-19 03:22:19 -0600509#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500511#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600512# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600514# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500515 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600516# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500517#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500518}
519
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600520#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600521
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500522#if defined(PNG_1_0_X)
523# define png_malloc_warn png_malloc
524#else
525/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500526 * function will set up png_malloc() to issue a png_warning and return NULL
527 * instead of issuing a png_error, if it fails to allocate the requested
528 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500529 */
530png_voidp PNGAPI
531png_malloc_warn(png_structp png_ptr, png_uint_32 size)
532{
533 png_voidp ptr;
534 png_uint_32 save_flags=png_ptr->flags;
535
536 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
537 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
538 png_ptr->flags=save_flags;
539 return(ptr);
540}
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500541#endif
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500542
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600543png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600544png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600545 png_uint_32 length)
546{
547 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600548
549 size = (png_size_t)length;
550 if ((png_uint_32)size != length)
551 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600552
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600553 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600554}
555
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600556png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600557png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600558 png_uint_32 length)
559{
560 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600561
562 size = (png_size_t)length;
563 if ((png_uint_32)size != length)
564 png_error(png_ptr,"Overflow in png_memset_check.");
565
566 return (png_memset (s1, value, size));
567
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600568}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500569
570#ifdef PNG_USER_MEM_SUPPORTED
571/* This function is called when the application wants to use another method
572 * of allocating and freeing memory.
573 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500574void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500575png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
576 malloc_fn, png_free_ptr free_fn)
577{
578 png_ptr->mem_ptr = mem_ptr;
579 png_ptr->malloc_fn = malloc_fn;
580 png_ptr->free_fn = free_fn;
581}
582
583/* This function returns a pointer to the mem_ptr associated with the user
584 * functions. The application should free any memory associated with this
585 * pointer before png_write_destroy and png_read_destroy are called.
586 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500587png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500588png_get_mem_ptr(png_structp png_ptr)
589{
590 return ((png_voidp)png_ptr->mem_ptr);
591}
592#endif /* PNG_USER_MEM_SUPPORTED */