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 | 917648e | 2004-12-02 18:14:51 -0600 | [diff] [blame^] | 4 | * libpng version 1.2.8 - December 3, 2004 |
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 | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 6 | * Copyright (c) 1998-2004 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 | |
| 17 | #define PNG_INTERNAL |
| 18 | #include "png.h" |
| 19 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 20 | /* 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 24 | /* 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] | 25 | 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] | 26 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 27 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 28 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 29 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 30 | return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* 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] | 34 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 35 | 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] | 36 | { |
| 37 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 38 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 39 | png_voidp struct_ptr; |
| 40 | |
| 41 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 42 | size = png_sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 43 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 44 | size = png_sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 45 | else |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 46 | return (png_get_copyright(NULL)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 47 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 48 | #ifdef PNG_USER_MEM_SUPPORTED |
| 49 | if(malloc_fn != NULL) |
| 50 | { |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 51 | png_struct dummy_struct; |
| 52 | png_structp png_ptr = &dummy_struct; |
| 53 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 54 | struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 55 | } |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 56 | else |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 57 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 58 | struct_ptr = (png_voidp)farmalloc(size); |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 59 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 60 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 61 | return (struct_ptr); |
| 62 | } |
| 63 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 64 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 65 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 66 | png_destroy_struct(png_voidp struct_ptr) |
| 67 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 68 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 69 | png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 73 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 74 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 75 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 76 | { |
| 77 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 78 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 79 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 80 | #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-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 85 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 86 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 90 | farfree (struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 91 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 94 | /* Allocate memory. For reasonable files, size should never exceed |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 95 | * 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-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 101 | * 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] | 102 | * 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-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 113 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 114 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 115 | png_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 116 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 117 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 118 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 119 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 120 | return (NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 121 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 122 | #ifdef PNG_USER_MEM_SUPPORTED |
| 123 | if(png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 124 | 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] | 125 | else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 126 | 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-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 132 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 133 | png_malloc_default(png_structp png_ptr, png_uint_32 size) |
| 134 | { |
| 135 | png_voidp ret; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 136 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 137 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 138 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 139 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 140 | { |
| 141 | png_warning(png_ptr, "Cannot Allocate > 64K"); |
| 142 | ret = NULL; |
| 143 | } |
| 144 | else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 145 | #endif |
| 146 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 147 | if (size != (size_t)size) |
| 148 | ret = NULL; |
| 149 | else if (size == (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 150 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 151 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 152 | { |
| 153 | /* try to see if we need to do any of this fancy stuff */ |
| 154 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 155 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 156 | { |
| 157 | int num_blocks; |
| 158 | png_uint_32 total_size; |
| 159 | png_bytep table; |
| 160 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 161 | png_byte huge * hptr; |
| 162 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 163 | if (ret != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 164 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 165 | farfree(ret); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 166 | ret = NULL; |
| 167 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 168 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 169 | if(png_ptr->zlib_window_bits > 14) |
| 170 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 171 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 172 | 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 Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 177 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 178 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 179 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 180 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 181 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 182 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 183 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 184 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 185 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 186 | png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ |
| 187 | else |
| 188 | png_warning(png_ptr, "Out Of Memory."); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 189 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 190 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 191 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 192 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 193 | if ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 194 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 195 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 196 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 197 | 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-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 202 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 203 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 204 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 205 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 206 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 207 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 208 | png_sizeof (png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 209 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 210 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 211 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 212 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 213 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 214 | png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */ |
| 215 | else |
| 216 | png_warning(png_ptr, "Out Of memory."); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 217 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 218 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 222 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 223 | { |
| 224 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 225 | hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 226 | } |
| 227 | for (i = 0; i < num_blocks; i++) |
| 228 | { |
| 229 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 230 | hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 231 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 232 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 233 | png_ptr->offset_table_number = num_blocks; |
| 234 | png_ptr->offset_table_count = 0; |
| 235 | png_ptr->offset_table_count_free = 0; |
| 236 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 237 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 238 | |
| 239 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 240 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 241 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 242 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 243 | png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */ |
| 244 | else |
| 245 | png_warning(png_ptr, "Out of Memory."); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 246 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 247 | return (NULL); |
| 248 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 249 | |
| 250 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 254 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 255 | #ifndef PNG_USER_MEM_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 256 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 257 | { |
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 | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 259 | 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 262 | } |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 263 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 264 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 265 | return (ret); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 268 | /* free a pointer allocated by png_malloc(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 269 | 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-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 271 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 272 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 273 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 274 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 275 | return; |
| 276 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 277 | #ifdef PNG_USER_MEM_SUPPORTED |
| 278 | if (png_ptr->free_fn != NULL) |
| 279 | { |
| 280 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | else png_free_default(png_ptr, ptr); |
| 284 | } |
| 285 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 286 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 287 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 288 | { |
| 289 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 290 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 291 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 292 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 293 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 294 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 295 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 296 | { |
| 297 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 298 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 299 | ptr = NULL; |
| 300 | png_ptr->offset_table_count_free++; |
| 301 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 302 | } |
| 303 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 304 | 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 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 | |
| 313 | if (ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 314 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 315 | farfree(ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 316 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | #else /* Not the Borland DOS special memory handler */ |
| 320 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 321 | /* 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] | 322 | 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] | 323 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 324 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 325 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 326 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 327 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 328 | return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 329 | } |
| 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-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 333 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 334 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 335 | 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] | 336 | { |
| 337 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 338 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 339 | png_voidp struct_ptr; |
| 340 | |
| 341 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 342 | size = png_sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 343 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 344 | size = png_sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 345 | else |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 346 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 347 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 348 | #ifdef PNG_USER_MEM_SUPPORTED |
| 349 | if(malloc_fn != NULL) |
| 350 | { |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 351 | png_struct dummy_struct; |
| 352 | png_structp png_ptr = &dummy_struct; |
| 353 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 354 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 355 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 356 | png_memset(struct_ptr, 0, size); |
| 357 | return (struct_ptr); |
| 358 | } |
| 359 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 360 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 361 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 362 | struct_ptr = (png_voidp)farmalloc(size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 363 | #else |
| 364 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 365 | struct_ptr = (png_voidp)halloc(size,1); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 366 | # else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 367 | struct_ptr = (png_voidp)malloc(size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 368 | # endif |
| 369 | #endif |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 370 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 371 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 372 | |
| 373 | return (struct_ptr); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 378 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 379 | png_destroy_struct(png_voidp struct_ptr) |
| 380 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 381 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 382 | png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 386 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 387 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 388 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 389 | { |
| 390 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 391 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 392 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 393 | #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-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 398 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 399 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 403 | #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-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 412 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 415 | /* Allocate memory. For reasonable files, size should never exceed |
| 416 | 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] | 417 | 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] | 418 | need to allocate exactly 64K, so whatever you call here must |
| 419 | have the ability to do that. */ |
| 420 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 421 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 422 | png_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 423 | { |
| 424 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 425 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 426 | #ifdef PNG_USER_MEM_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 427 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 428 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 429 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 430 | if(png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 431 | 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] | 432 | else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 433 | 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-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 437 | } |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 438 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 439 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 440 | png_malloc_default(png_structp png_ptr, png_uint_32 size) |
| 441 | { |
| 442 | png_voidp ret; |
| 443 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 444 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 445 | if (png_ptr == NULL || size == 0) |
| 446 | return (NULL); |
| 447 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 448 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 449 | if (size > (png_uint_32)65536L) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 450 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 451 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 452 | if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 453 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 454 | else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 455 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 458 | #endif |
| 459 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 460 | /* Check for overflow */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 461 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 462 | if (size != (unsigned long)size) |
| 463 | ret = NULL; |
| 464 | else |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 465 | ret = farmalloc(size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 466 | #else |
| 467 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 468 | if (size != (unsigned long)size) |
| 469 | ret = NULL; |
| 470 | else |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 471 | ret = halloc(size, 1); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 472 | # else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 473 | if (size != (size_t)size) |
| 474 | ret = NULL; |
| 475 | else |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 476 | ret = malloc((size_t)size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 477 | # endif |
| 478 | #endif |
| 479 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 480 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 481 | 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] | 482 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 483 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 484 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 485 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 486 | } |
| 487 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 488 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
| 489 | without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 490 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 491 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 492 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 493 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 494 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 495 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 496 | #ifdef PNG_USER_MEM_SUPPORTED |
| 497 | if (png_ptr->free_fn != NULL) |
| 498 | { |
| 499 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | else png_free_default(png_ptr, ptr); |
| 503 | } |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 504 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 505 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 506 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 507 | if (png_ptr == NULL || ptr == NULL) |
| 508 | return; |
| 509 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 510 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 511 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 512 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 513 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 514 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 515 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 516 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 517 | # else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 518 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 519 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 520 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 521 | } |
| 522 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 523 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 524 | |
Glenn Randers-Pehrson | 2ae022d | 2002-07-01 22:23:46 -0500 | [diff] [blame] | 525 | #if defined(PNG_1_0_X) |
| 526 | # define png_malloc_warn png_malloc |
| 527 | #else |
| 528 | /* 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] | 529 | * function will set up png_malloc() to issue a png_warning and return NULL |
| 530 | * instead of issuing a png_error, if it fails to allocate the requested |
| 531 | * memory. |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 532 | */ |
| 533 | png_voidp PNGAPI |
| 534 | png_malloc_warn(png_structp png_ptr, png_uint_32 size) |
| 535 | { |
| 536 | png_voidp ptr; |
| 537 | png_uint_32 save_flags=png_ptr->flags; |
| 538 | |
| 539 | png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 540 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); |
| 541 | png_ptr->flags=save_flags; |
| 542 | return(ptr); |
| 543 | } |
Glenn Randers-Pehrson | 2ae022d | 2002-07-01 22:23:46 -0500 | [diff] [blame] | 544 | #endif |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 545 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 546 | png_voidp PNGAPI |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 547 | png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 548 | png_uint_32 length) |
| 549 | { |
| 550 | png_size_t size; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 551 | |
| 552 | size = (png_size_t)length; |
| 553 | if ((png_uint_32)size != length) |
| 554 | png_error(png_ptr,"Overflow in png_memcpy_check."); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 555 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 556 | return(png_memcpy (s1, s2, size)); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 557 | } |
| 558 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 559 | png_voidp PNGAPI |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 560 | png_memset_check (png_structp png_ptr, png_voidp s1, int value, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 561 | png_uint_32 length) |
| 562 | { |
| 563 | png_size_t size; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 564 | |
| 565 | size = (png_size_t)length; |
| 566 | if ((png_uint_32)size != length) |
| 567 | png_error(png_ptr,"Overflow in png_memset_check."); |
| 568 | |
| 569 | return (png_memset (s1, value, size)); |
| 570 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 571 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 572 | |
| 573 | #ifdef PNG_USER_MEM_SUPPORTED |
| 574 | /* This function is called when the application wants to use another method |
| 575 | * of allocating and freeing memory. |
| 576 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 577 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 578 | png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
| 579 | malloc_fn, png_free_ptr free_fn) |
| 580 | { |
| 581 | png_ptr->mem_ptr = mem_ptr; |
| 582 | png_ptr->malloc_fn = malloc_fn; |
| 583 | png_ptr->free_fn = free_fn; |
| 584 | } |
| 585 | |
| 586 | /* This function returns a pointer to the mem_ptr associated with the user |
| 587 | * functions. The application should free any memory associated with this |
| 588 | * pointer before png_write_destroy and png_read_destroy are called. |
| 589 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 590 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 591 | png_get_mem_ptr(png_structp png_ptr) |
| 592 | { |
| 593 | return ((png_voidp)png_ptr->mem_ptr); |
| 594 | } |
| 595 | #endif /* PNG_USER_MEM_SUPPORTED */ |