Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 2 | /* pngmem.c - stub functions for memory allocation |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson glennrp@comcast.net | 11a3c7b | 2009-05-15 20:33:24 -0500 | [diff] [blame^] | 4 | * Last changed in libpng 1.4.0 [May 15, 2009] |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 5 | * For conditions of distribution and use, see copyright notice in png.h |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 6 | * Copyright (c) 1998-2009 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (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-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | * |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 10 | * This file provides a location for all memory allocation. Users who |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 11 | * 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-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 15 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 16 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 17 | #include "png.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 18 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 19 | #include "pngpriv.h" |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 20 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 21 | /* Borland DOS special memory handler */ |
| 22 | #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) |
| 23 | /* if you change this, be sure to change the one in png.h also */ |
| 24 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 25 | /* Allocate memory for a png_struct. The malloc and memset can be replaced |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 26 | by a single call to calloc() if this is thought to improve performance. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 27 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 28 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 29 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 30 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 31 | return (png_create_struct_2(type, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* Alternate version of png_create_struct, for use with user-defined malloc. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 35 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 36 | png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 37 | { |
| 38 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 39 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 40 | png_voidp struct_ptr; |
| 41 | |
| 42 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 43 | size = png_sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 44 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 45 | size = png_sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 46 | else |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 47 | return (png_get_copyright(NULL)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 48 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 49 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 50 | if (malloc_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 51 | { |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 52 | png_struct dummy_struct; |
| 53 | png_structp png_ptr = &dummy_struct; |
| 54 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 55 | struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 56 | } |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 57 | else |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 58 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 59 | struct_ptr = (png_voidp)farmalloc(size); |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 60 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 61 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 62 | return (struct_ptr); |
| 63 | } |
| 64 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 65 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 66 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 67 | png_destroy_struct(png_voidp struct_ptr) |
| 68 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 69 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 70 | png_destroy_struct_2(struct_ptr, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 74 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 75 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 76 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 77 | { |
| 78 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 79 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 80 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 81 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 82 | if (free_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 83 | { |
| 84 | png_struct dummy_struct; |
| 85 | png_structp png_ptr = &dummy_struct; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 86 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 87 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 91 | farfree (struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 92 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 95 | /* Allocate memory. For reasonable files, size should never exceed |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 96 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 97 | * it not to. See zconf.h and png.h for more information. zlib does |
| 98 | * need to allocate exactly 64K, so whatever you call here must |
| 99 | * have the ability to do that. |
| 100 | * |
| 101 | * Borland seems to have a problem in DOS mode for exactly 64K. |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 102 | * It gives you a segment with an offset of 8 (perhaps to store its |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 103 | * memory stuff). zlib doesn't like this at all, so we have to |
| 104 | * detect and deal with it. This code should not be needed in |
| 105 | * Windows or OS/2 modes, and only in 16 bit mode. This code has |
| 106 | * been updated by Alexander Lehmann for version 0.89 to waste less |
| 107 | * memory. |
| 108 | * |
| 109 | * Note that we can't use png_size_t for the "size" declaration, |
| 110 | * since on some systems a png_size_t is a 16-bit quantity, and as a |
| 111 | * result, we would be truncating potentially larger memory requests |
| 112 | * (which should cause a fatal error) and introducing major problems. |
| 113 | */ |
Glenn Randers-Pehrson | 0ffb71a | 2009-02-28 06:08:20 -0600 | [diff] [blame] | 114 | #ifdef PNG_CALLOC_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 115 | png_voidp PNGAPI |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 116 | png_calloc(png_structp png_ptr, png_alloc_size_t size) |
| 117 | { |
| 118 | png_voidp ret; |
| 119 | |
| 120 | ret = (png_malloc(png_ptr, size)); |
| 121 | if (ret != NULL) |
| 122 | png_memset(ret,0,(png_size_t)size); |
| 123 | return (ret); |
| 124 | } |
Glenn Randers-Pehrson | 0ffb71a | 2009-02-28 06:08:20 -0600 | [diff] [blame] | 125 | #endif |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 126 | |
| 127 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 128 | png_malloc(png_structp png_ptr, png_alloc_size_t size) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 129 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 130 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 131 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 132 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 133 | return (NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 134 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 135 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 136 | if (png_ptr->malloc_fn != NULL) |
| 137 | ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 138 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 139 | ret = (png_malloc_default(png_ptr, size)); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 140 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 141 | png_error(png_ptr, "Out of memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 142 | return (ret); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 145 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 146 | png_malloc_default(png_structp png_ptr, png_alloc_size_t size) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 147 | { |
| 148 | png_voidp ret; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 149 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 150 | |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 151 | if (png_ptr == NULL || size == 0) |
| 152 | return (NULL); |
| 153 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 154 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 155 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 156 | { |
| 157 | png_warning(png_ptr, "Cannot Allocate > 64K"); |
| 158 | ret = NULL; |
| 159 | } |
| 160 | else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 161 | #endif |
| 162 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 163 | if (size != (size_t)size) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 164 | ret = NULL; |
| 165 | else if (size == (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 166 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 167 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 168 | { |
| 169 | /* try to see if we need to do any of this fancy stuff */ |
| 170 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 171 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 172 | { |
| 173 | int num_blocks; |
| 174 | png_uint_32 total_size; |
| 175 | png_bytep table; |
| 176 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 177 | png_byte huge * hptr; |
| 178 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 179 | if (ret != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 180 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 181 | farfree(ret); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 182 | ret = NULL; |
| 183 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 184 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 185 | if (png_ptr->zlib_window_bits > 14) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 186 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 187 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 188 | num_blocks = 1; |
| 189 | if (png_ptr->zlib_mem_level >= 7) |
| 190 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 191 | else |
| 192 | num_blocks++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 193 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 194 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 195 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 196 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 197 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 198 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 199 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 200 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 201 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 202 | png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 203 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 204 | png_warning(png_ptr, "Out Of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 205 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 206 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 207 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 208 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 209 | if ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 210 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 211 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 212 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 213 | png_error(png_ptr, |
| 214 | "Farmalloc didn't return normalized pointer"); |
| 215 | else |
| 216 | png_warning(png_ptr, |
| 217 | "Farmalloc didn't return normalized pointer"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 218 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 219 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 220 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 221 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 222 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 223 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 224 | png_sizeof(png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 225 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 226 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 227 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 228 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 229 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 230 | png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 231 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 232 | png_warning(png_ptr, "Out Of memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 233 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 234 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 238 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 239 | { |
| 240 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 241 | hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 242 | } |
| 243 | for (i = 0; i < num_blocks; i++) |
| 244 | { |
| 245 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 246 | hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 247 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 248 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 249 | png_ptr->offset_table_number = num_blocks; |
| 250 | png_ptr->offset_table_count = 0; |
| 251 | png_ptr->offset_table_count_free = 0; |
| 252 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 253 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 254 | |
| 255 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 256 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 257 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 258 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 259 | png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 260 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 261 | png_warning(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 262 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 263 | return (NULL); |
| 264 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 265 | |
| 266 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 270 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 271 | #ifndef PNG_USER_MEM_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 272 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 273 | { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 274 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 275 | png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 276 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 277 | png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 278 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 279 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 280 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 281 | return (ret); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 284 | /* free a pointer allocated by png_malloc(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 285 | configuration, png_ptr is not used, but is passed in case it |
| 286 | is needed. If ptr is NULL, return without taking any action. */ |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 287 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 288 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 289 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 290 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 291 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 292 | return; |
| 293 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 294 | #ifdef PNG_USER_MEM_SUPPORTED |
| 295 | if (png_ptr->free_fn != NULL) |
| 296 | { |
| 297 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | else png_free_default(png_ptr, ptr); |
| 301 | } |
| 302 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 303 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 304 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 305 | { |
| 306 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 307 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 308 | if (png_ptr == NULL || ptr == NULL) return; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 309 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 310 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 311 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 312 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 313 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 314 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 315 | { |
| 316 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 317 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 318 | ptr = NULL; |
| 319 | png_ptr->offset_table_count_free++; |
| 320 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 321 | } |
| 322 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 323 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 324 | { |
| 325 | farfree(png_ptr->offset_table); |
| 326 | farfree(png_ptr->offset_table_ptr); |
| 327 | png_ptr->offset_table = NULL; |
| 328 | png_ptr->offset_table_ptr = NULL; |
| 329 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 330 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 331 | |
| 332 | if (ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 333 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 334 | farfree(ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 335 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | #else /* Not the Borland DOS special memory handler */ |
| 339 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 340 | /* Allocate memory for a png_struct or a png_info. The malloc and |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 341 | memset can be replaced by a single call to calloc() if this is thought |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 342 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 343 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 344 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 345 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 346 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 347 | return (png_create_struct_2(type, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* Allocate memory for a png_struct or a png_info. The malloc and |
| 351 | memset can be replaced by a single call to calloc() if this is thought |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 352 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 353 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 354 | png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 355 | { |
| 356 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 357 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 358 | png_voidp struct_ptr; |
| 359 | |
| 360 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 361 | size = png_sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 362 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 363 | size = png_sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 364 | else |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 365 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 366 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 367 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 368 | if (malloc_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 369 | { |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 370 | png_struct dummy_struct; |
| 371 | png_structp png_ptr = &dummy_struct; |
| 372 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 373 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 374 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 375 | png_memset(struct_ptr, 0, size); |
| 376 | return (struct_ptr); |
| 377 | } |
| 378 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 379 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 380 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 381 | struct_ptr = (png_voidp)farmalloc(size); |
| 382 | #else |
| 383 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 384 | struct_ptr = (png_voidp)halloc(size, 1); |
| 385 | # else |
| 386 | struct_ptr = (png_voidp)malloc(size); |
| 387 | # endif |
| 388 | #endif |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 389 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 390 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 391 | |
| 392 | return (struct_ptr); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 397 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 398 | png_destroy_struct(png_voidp struct_ptr) |
| 399 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 400 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 401 | png_destroy_struct_2(struct_ptr, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 405 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 406 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 407 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 408 | { |
| 409 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 410 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 411 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 412 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 413 | if (free_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 414 | { |
| 415 | png_struct dummy_struct; |
| 416 | png_structp png_ptr = &dummy_struct; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 417 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 418 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 422 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 423 | farfree(struct_ptr); |
| 424 | #else |
| 425 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 426 | hfree(struct_ptr); |
| 427 | # else |
| 428 | free(struct_ptr); |
| 429 | # endif |
| 430 | #endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 431 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 432 | } |
| 433 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 434 | /* Allocate memory. For reasonable files, size should never exceed |
| 435 | 64K. However, zlib may allocate more then 64K if you don't tell |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 436 | it not to. See zconf.h and png.h for more information. zlib does |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 437 | need to allocate exactly 64K, so whatever you call here must |
| 438 | have the ability to do that. */ |
| 439 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 440 | png_voidp PNGAPI |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 441 | png_calloc(png_structp png_ptr, png_alloc_size_t size) |
| 442 | { |
| 443 | png_voidp ret; |
| 444 | |
| 445 | ret = (png_malloc(png_ptr, size)); |
| 446 | if (ret != NULL) |
| 447 | png_memset(ret,0,(png_size_t)size); |
| 448 | return (ret); |
| 449 | } |
| 450 | |
| 451 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 452 | png_malloc(png_structp png_ptr, png_alloc_size_t size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 453 | { |
| 454 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 455 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 456 | #ifdef PNG_USER_MEM_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 457 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 458 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 459 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 460 | if (png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 461 | ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 462 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 463 | ret = (png_malloc_default(png_ptr, size)); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 464 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 465 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 466 | return (ret); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 467 | } |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 468 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 469 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 470 | png_malloc_default(png_structp png_ptr, png_alloc_size_t size) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 471 | { |
| 472 | png_voidp ret; |
| 473 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 474 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 475 | if (png_ptr == NULL || size == 0) |
| 476 | return (NULL); |
| 477 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 478 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 479 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 480 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 481 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 482 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 483 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 484 | else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 485 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 488 | #endif |
| 489 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 490 | /* Check for overflow */ |
| 491 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 492 | if (size != (unsigned long)size) |
| 493 | ret = NULL; |
| 494 | else |
| 495 | ret = farmalloc(size); |
| 496 | #else |
| 497 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 498 | if (size != (unsigned long)size) |
| 499 | ret = NULL; |
| 500 | else |
| 501 | ret = halloc(size, 1); |
| 502 | # else |
| 503 | if (size != (size_t)size) |
| 504 | ret = NULL; |
| 505 | else |
| 506 | ret = malloc((size_t)size); |
| 507 | # endif |
| 508 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 509 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 510 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 511 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 512 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 513 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 514 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 515 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 516 | } |
| 517 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 518 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
| 519 | without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 520 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 521 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 522 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 523 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 524 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 525 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 526 | #ifdef PNG_USER_MEM_SUPPORTED |
| 527 | if (png_ptr->free_fn != NULL) |
| 528 | { |
| 529 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | else png_free_default(png_ptr, ptr); |
| 533 | } |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 534 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 535 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 536 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 537 | if (png_ptr == NULL || ptr == NULL) |
| 538 | return; |
| 539 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 540 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 541 | |
| 542 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 543 | farfree(ptr); |
| 544 | #else |
| 545 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 546 | hfree(ptr); |
| 547 | # else |
| 548 | free(ptr); |
| 549 | # endif |
| 550 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 553 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 554 | |
Glenn Randers-Pehrson | 2ae022d | 2002-07-01 22:23:46 -0500 | [diff] [blame] | 555 | /* This function was added at libpng version 1.2.3. The png_malloc_warn() |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 556 | * function will set up png_malloc() to issue a png_warning and return NULL |
| 557 | * instead of issuing a png_error, if it fails to allocate the requested |
| 558 | * memory. |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 559 | */ |
| 560 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 561 | png_malloc_warn(png_structp png_ptr, png_alloc_size_t size) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 562 | { |
| 563 | png_voidp ptr; |
Glenn Randers-Pehrson | ae4bd5c | 2006-11-16 20:35:49 -0600 | [diff] [blame] | 564 | png_uint_32 save_flags; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 565 | if (png_ptr == NULL) return (NULL); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 566 | |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 567 | save_flags = png_ptr->flags; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 568 | png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 569 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); |
| 570 | png_ptr->flags=save_flags; |
| 571 | return(ptr); |
| 572 | } |
| 573 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 574 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 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-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 579 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 580 | png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
| 581 | malloc_fn, png_free_ptr free_fn) |
| 582 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 583 | if (png_ptr != NULL) |
| 584 | { |
| 585 | png_ptr->mem_ptr = mem_ptr; |
| 586 | png_ptr->malloc_fn = malloc_fn; |
| 587 | png_ptr->free_fn = free_fn; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 588 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* This function returns a pointer to the mem_ptr associated with the user |
| 592 | * functions. The application should free any memory associated with this |
| 593 | * pointer before png_write_destroy and png_read_destroy are called. |
| 594 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 595 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 596 | png_get_mem_ptr(png_structp png_ptr) |
| 597 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 598 | if (png_ptr == NULL) return (NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 599 | return ((png_voidp)png_ptr->mem_ptr); |
| 600 | } |
| 601 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 602 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |