blob: 81eb654f0ba8eaa58948ddc2acafb91098352876 [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-Pehrson1ce08362006-03-08 23:35:59 -06004 * Last changed in libpng 1.2.9 March 9, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 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
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
21
Guy Schalnat4ee97b01996-01-16 01:51:56 -060022/* Borland DOS special memory handler */
23#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
24/* if you change this, be sure to change the one in png.h also */
25
Guy Schalnate5a37791996-06-05 15:50:50 -050026/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060027 by a single call to calloc() if this is thought to improve performance. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050028png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060029png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050030{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050031#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050032 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050033}
34
35/* Alternate version of png_create_struct, for use with user-defined malloc. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050036png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050037png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050038{
39#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060040 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050041 png_voidp struct_ptr;
42
43 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050044 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -050045 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050046 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -050047 else
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050048 return (png_get_copyright(NULL));
Guy Schalnate5a37791996-06-05 15:50:50 -050049
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050050#ifdef PNG_USER_MEM_SUPPORTED
51 if(malloc_fn != NULL)
52 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050053 png_struct dummy_struct;
54 png_structp png_ptr = &dummy_struct;
55 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -060056 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050057 }
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050058 else
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050059#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -050060 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050061 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050062 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050063 return (struct_ptr);
64}
65
Guy Schalnate5a37791996-06-05 15:50:50 -050066/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050067void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050068png_destroy_struct(png_voidp struct_ptr)
69{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050070#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050071 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050072}
73
74/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050075void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050076png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
77 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050078{
79#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060081 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050082#ifdef PNG_USER_MEM_SUPPORTED
83 if(free_fn != NULL)
84 {
85 png_struct dummy_struct;
86 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050087 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050088 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050089 return;
90 }
91#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050092 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060093 }
Guy Schalnate5a37791996-06-05 15:50:50 -050094}
95
Guy Schalnat0d580581995-07-20 02:43:20 -050096/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 * 64K. However, zlib may allocate more then 64K if you don't tell
98 * it not to. See zconf.h and png.h for more information. zlib does
99 * need to allocate exactly 64K, so whatever you call here must
100 * have the ability to do that.
101 *
102 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500103 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 * memory stuff). zlib doesn't like this at all, so we have to
105 * detect and deal with it. This code should not be needed in
106 * Windows or OS/2 modes, and only in 16 bit mode. This code has
107 * been updated by Alexander Lehmann for version 0.89 to waste less
108 * memory.
109 *
110 * Note that we can't use png_size_t for the "size" declaration,
111 * since on some systems a png_size_t is a 16-bit quantity, and as a
112 * result, we would be truncating potentially larger memory requests
113 * (which should cause a fatal error) and introducing major problems.
114 */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500115
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500116png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500117png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600118{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600119 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500120
Andreas Dilger47a0c421997-05-16 02:46:07 -0500121 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500122 return (NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600123
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500124#ifdef PNG_USER_MEM_SUPPORTED
125 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600126 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500127 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500128 ret = (png_malloc_default(png_ptr, size));
129 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
130 png_error(png_ptr, "Out of memory!");
131 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500132}
133
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500134png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500135png_malloc_default(png_structp png_ptr, png_uint_32 size)
136{
137 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600138#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500139
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600140#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500142 {
143 png_warning(png_ptr, "Cannot Allocate > 64K");
144 ret = NULL;
145 }
146 else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600147#endif
148
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500149 if (size != (size_t)size)
150 ret = NULL;
151 else if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600152 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500153 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 {
155 /* try to see if we need to do any of this fancy stuff */
156 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 {
159 int num_blocks;
160 png_uint_32 total_size;
161 png_bytep table;
162 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600163 png_byte huge * hptr;
164
Andreas Dilger47a0c421997-05-16 02:46:07 -0500165 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600166 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600167 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600168 ret = NULL;
169 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600170
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500171 if(png_ptr->zlib_window_bits > 14)
172 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
173 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600174 num_blocks = 1;
175 if (png_ptr->zlib_mem_level >= 7)
176 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
177 else
178 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600179
Guy Schalnate5a37791996-06-05 15:50:50 -0500180 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600181
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600183
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600185 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500186#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500187 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600188 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
189 else
190 png_warning(png_ptr, "Out Of Memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500191#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600192 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600193 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600194
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600196 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500197#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500198 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600199 png_error(png_ptr,
200 "Farmalloc didn't return normalized pointer");
201 else
202 png_warning(png_ptr,
203 "Farmalloc didn't return normalized pointer");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500204#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600205 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600206 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600207
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 png_ptr->offset_table_ptr = farmalloc(num_blocks *
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500210 png_sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500211
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 if (png_ptr->offset_table_ptr == NULL)
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, "Out Of memory."); /* Note "O" and "M" */
217 else
218 png_warning(png_ptr, "Out Of memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500219#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600220 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500221 }
222
223 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500224 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500225 {
226 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500227 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600228 }
229 for (i = 0; i < num_blocks; i++)
230 {
231 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500232 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600233 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600234
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600235 png_ptr->offset_table_number = num_blocks;
236 png_ptr->offset_table_count = 0;
237 png_ptr->offset_table_count_free = 0;
238 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600239 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500240
241 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600242 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500243#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500244 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600245 png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
246 else
247 png_warning(png_ptr, "Out of Memory.");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500248#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600249 return (NULL);
250 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500251
252 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600253 }
254 else
255 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500257#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500258 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500259 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500260 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600261 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
262 else
263 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500264 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500265#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500266
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600267 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500268}
269
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500270/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600271 configuration, png_ptr is not used, but is passed in case it
272 is needed. If ptr is NULL, return without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500273void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500274png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500275{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600277 return;
278
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500279#ifdef PNG_USER_MEM_SUPPORTED
280 if (png_ptr->free_fn != NULL)
281 {
282 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500283 return;
284 }
285 else png_free_default(png_ptr, ptr);
286}
287
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500288void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500289png_free_default(png_structp png_ptr, png_voidp ptr)
290{
291#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600292
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600294 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600296
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 for (i = 0; i < png_ptr->offset_table_count; i++)
298 {
299 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600300 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 ptr = NULL;
302 png_ptr->offset_table_count_free++;
303 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600304 }
305 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
307 {
308 farfree(png_ptr->offset_table);
309 farfree(png_ptr->offset_table_ptr);
310 png_ptr->offset_table = NULL;
311 png_ptr->offset_table_ptr = NULL;
312 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600313 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314
315 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600316 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600318 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600319}
320
321#else /* Not the Borland DOS special memory handler */
322
Guy Schalnate5a37791996-06-05 15:50:50 -0500323/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600324 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500325 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500326png_voidp /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600327png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500328{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500329#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500330 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500331}
332
333/* Allocate memory for a png_struct or a png_info. The malloc and
334 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500335 to improve performance noticably. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500336png_voidp /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500337png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500338{
339#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500341 png_voidp struct_ptr;
342
343 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500344 size = png_sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500345 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500346 size = png_sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500347 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500348 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500349
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500350#ifdef PNG_USER_MEM_SUPPORTED
351 if(malloc_fn != NULL)
352 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600353 png_struct dummy_struct;
354 png_structp png_ptr = &dummy_struct;
355 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -0600356 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500357 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500358 png_memset(struct_ptr, 0, size);
359 return (struct_ptr);
360 }
361#endif /* PNG_USER_MEM_SUPPORTED */
362
Guy Schalnate5a37791996-06-05 15:50:50 -0500363#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500364 struct_ptr = (png_voidp)farmalloc(size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500365#else
366# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500367 struct_ptr = (png_voidp)halloc(size,1);
Guy Schalnate5a37791996-06-05 15:50:50 -0500368# else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500369 struct_ptr = (png_voidp)malloc(size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500370# endif
371#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500372 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500373 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500374
375 return (struct_ptr);
376}
377
378
379/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500380void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -0500381png_destroy_struct(png_voidp struct_ptr)
382{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500383#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500384 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500385}
386
387/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500388void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500389png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
390 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500391{
392#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600394 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500395#ifdef PNG_USER_MEM_SUPPORTED
396 if(free_fn != NULL)
397 {
398 png_struct dummy_struct;
399 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500400 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500401 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500402 return;
403 }
404#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500405#if defined(__TURBOC__) && !defined(__FLAT__)
406 farfree(struct_ptr);
407#else
408# if defined(_MSC_VER) && defined(MAXSEG_64K)
409 hfree(struct_ptr);
410# else
411 free(struct_ptr);
412# endif
413#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600414 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500415}
416
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600417/* Allocate memory. For reasonable files, size should never exceed
418 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600420 need to allocate exactly 64K, so whatever you call here must
421 have the ability to do that. */
422
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500423png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600425{
426 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500427
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500428#ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500430 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600431
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500432 if(png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500433 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500434 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500435 ret = (png_malloc_default(png_ptr, size));
436 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
437 png_error(png_ptr, "Out of Memory!");
438 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500439}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500440
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600441png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500442png_malloc_default(png_structp png_ptr, png_uint_32 size)
443{
444 png_voidp ret;
445#endif /* PNG_USER_MEM_SUPPORTED */
446
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500447 if (png_ptr == NULL || size == 0)
448 return (NULL);
449
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600450#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600451 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600452 {
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500453#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600454 if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
455 png_error(png_ptr, "Cannot Allocate > 64K");
456 else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500457#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600458 return NULL;
459 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600460#endif
461
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500462 /* Check for overflow */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600463#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500464 if (size != (unsigned long)size)
465 ret = NULL;
466 else
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600467 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600468#else
469# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500470 if (size != (unsigned long)size)
471 ret = NULL;
472 else
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600473 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600474# else
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500475 if (size != (size_t)size)
476 ret = NULL;
477 else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600478 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600479# endif
480#endif
481
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500482#ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600483 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600484 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500485#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600486
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600487 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600488}
489
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500490/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
491 without taking any action. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500492void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500493png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600494{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600496 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500497
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500498#ifdef PNG_USER_MEM_SUPPORTED
499 if (png_ptr->free_fn != NULL)
500 {
501 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502 return;
503 }
504 else png_free_default(png_ptr, ptr);
505}
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600506void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500507png_free_default(png_structp png_ptr, png_voidp ptr)
508{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500509 if (png_ptr == NULL || ptr == NULL)
510 return;
511
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500512#endif /* PNG_USER_MEM_SUPPORTED */
513
Guy Schalnat6d764711995-12-19 03:22:19 -0600514#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500515 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500516#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600517# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600519# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500520 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600521# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500522#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500523}
524
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600525#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600526
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500527#if defined(PNG_1_0_X)
528# define png_malloc_warn png_malloc
529#else
530/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500531 * function will set up png_malloc() to issue a png_warning and return NULL
532 * instead of issuing a png_error, if it fails to allocate the requested
533 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500534 */
535png_voidp PNGAPI
536png_malloc_warn(png_structp png_ptr, png_uint_32 size)
537{
538 png_voidp ptr;
539 png_uint_32 save_flags=png_ptr->flags;
540
541 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
542 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
543 png_ptr->flags=save_flags;
544 return(ptr);
545}
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500546#endif
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500547
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600548png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600549png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600550 png_uint_32 length)
551{
552 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600553
554 size = (png_size_t)length;
555 if ((png_uint_32)size != length)
556 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600557
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600558 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600559}
560
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600561png_voidp PNGAPI
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600562png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600563 png_uint_32 length)
564{
565 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600566
567 size = (png_size_t)length;
568 if ((png_uint_32)size != length)
569 png_error(png_ptr,"Overflow in png_memset_check.");
570
571 return (png_memset (s1, value, size));
572
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600573}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500574
575#ifdef PNG_USER_MEM_SUPPORTED
576/* This function is called when the application wants to use another method
577 * of allocating and freeing memory.
578 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500579void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500580png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
581 malloc_fn, png_free_ptr free_fn)
582{
583 png_ptr->mem_ptr = mem_ptr;
584 png_ptr->malloc_fn = malloc_fn;
585 png_ptr->free_fn = free_fn;
586}
587
588/* This function returns a pointer to the mem_ptr associated with the user
589 * functions. The application should free any memory associated with this
590 * pointer before png_write_destroy and png_read_destroy are called.
591 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500592png_voidp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500593png_get_mem_ptr(png_structp png_ptr)
594{
595 return ((png_voidp)png_ptr->mem_ptr);
596}
597#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600598#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */