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 | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 4 | * Last changed in libpng 1.4.0 November 14, 2006 |
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 | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 6 | * Copyright (c) 1998-2006 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 | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 18 | #include "pngpriv.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 19 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 20 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
| 21 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 22 | /* 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 26 | /* 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] | 27 | 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] | 28 | png_voidp /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 29 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 30 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 31 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 32 | return (png_create_struct_2(type, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* 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] | 36 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 37 | 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] | 38 | { |
| 39 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 40 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 41 | png_voidp struct_ptr; |
| 42 | |
| 43 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 44 | size = sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 45 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 46 | size = sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 47 | else |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 48 | return (png_get_copyright(NULL)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 49 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 50 | #ifdef PNG_USER_MEM_SUPPORTED |
| 51 | if(malloc_fn != NULL) |
| 52 | { |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 53 | png_struct dummy_struct; |
| 54 | png_structp png_ptr = &dummy_struct; |
| 55 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 56 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 57 | } |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 58 | else |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 59 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 60 | struct_ptr = (png_voidp)farmalloc(size); |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 61 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 62 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 63 | return (struct_ptr); |
| 64 | } |
| 65 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 66 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 67 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 68 | png_destroy_struct(png_voidp struct_ptr) |
| 69 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 70 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 71 | png_destroy_struct_2(struct_ptr, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 75 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 76 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 77 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 78 | { |
| 79 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 80 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 81 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 82 | #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-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 87 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 88 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 92 | farfree (struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 93 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 96 | /* Allocate memory. For reasonable files, size should never exceed |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 97 | * 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-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 103 | * 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] | 104 | * 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-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 115 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 116 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 117 | png_malloc(png_structp png_ptr, png_alloc_size_t size) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 118 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 119 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 120 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 121 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 122 | return (NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 123 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 124 | #ifdef PNG_USER_MEM_SUPPORTED |
| 125 | if(png_ptr->malloc_fn != NULL) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 126 | ret = (*(png_ptr->malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 127 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 128 | ret = png_malloc_default(png_ptr, size); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 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-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 134 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 135 | 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] | 136 | { |
| 137 | png_voidp ret; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 138 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 139 | |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 140 | if (png_ptr == NULL || size == 0) |
| 141 | return (NULL); |
| 142 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 143 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 144 | if (size > (png_alloc_size_t)65536L) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 145 | { |
| 146 | png_warning(png_ptr, "Cannot Allocate > 64K"); |
| 147 | ret = NULL; |
| 148 | } |
| 149 | else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 150 | #endif |
| 151 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 152 | #if !defined(INT_MAX) || (INT_MAX <= 0x7ffffffeL) |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 153 | if (size != (size_t)size) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 154 | ret = NULL; |
| 155 | else |
| 156 | #endif |
| 157 | if (size == (png_alloc_size_t)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 158 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 159 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 160 | { |
| 161 | /* try to see if we need to do any of this fancy stuff */ |
| 162 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 163 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 164 | { |
| 165 | int num_blocks; |
| 166 | png_uint_32 total_size; |
| 167 | png_bytep table; |
| 168 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 169 | png_byte huge * hptr; |
| 170 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 171 | if (ret != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 172 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 173 | farfree(ret); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 174 | ret = NULL; |
| 175 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 176 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 177 | if(png_ptr->zlib_window_bits > 14) |
| 178 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 179 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 180 | num_blocks = 1; |
| 181 | if (png_ptr->zlib_mem_level >= 7) |
| 182 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 183 | else |
| 184 | num_blocks++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 185 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 186 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 187 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 188 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 189 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 190 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 191 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 192 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 193 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 194 | png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 195 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 196 | png_warning(png_ptr, "Out Of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 197 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 198 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 199 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 200 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 201 | if ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 202 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 203 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 204 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 205 | png_error(png_ptr, |
| 206 | "Farmalloc didn't return normalized pointer"); |
| 207 | else |
| 208 | png_warning(png_ptr, |
| 209 | "Farmalloc didn't return normalized pointer"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 210 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 211 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 212 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 213 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 214 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 215 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 216 | sizeof(png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 217 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 218 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 219 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 220 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 221 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 222 | png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 223 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 224 | png_warning(png_ptr, "Out Of memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 225 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 226 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 230 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 231 | { |
| 232 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 233 | hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 234 | } |
| 235 | for (i = 0; i < num_blocks; i++) |
| 236 | { |
| 237 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 238 | hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 239 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 240 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 241 | png_ptr->offset_table_number = num_blocks; |
| 242 | png_ptr->offset_table_count = 0; |
| 243 | png_ptr->offset_table_count_free = 0; |
| 244 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 245 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 246 | |
| 247 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 248 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 249 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 250 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 251 | png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 252 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 253 | png_warning(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 254 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 255 | return (NULL); |
| 256 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 257 | |
| 258 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 259 | } |
| 260 | else |
| 261 | ret = farmalloc(size); |
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 | #ifndef PNG_USER_MEM_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 264 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 265 | { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 266 | if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 267 | png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */ |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 268 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 269 | png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */ |
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 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 272 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 273 | return (ret); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 276 | /* free a pointer allocated by png_malloc(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 277 | configuration, png_ptr is not used, but is passed in case it |
| 278 | is needed. If ptr is NULL, return without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 279 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 280 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 281 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 282 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 283 | return; |
| 284 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 285 | #ifdef PNG_USER_MEM_SUPPORTED |
| 286 | if (png_ptr->free_fn != NULL) |
| 287 | { |
| 288 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | else png_free_default(png_ptr, ptr); |
| 292 | } |
| 293 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 294 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 295 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 296 | { |
| 297 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 298 | |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 299 | if(png_ptr == NULL) return; |
| 300 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 301 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 302 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 303 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 304 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 305 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 306 | { |
| 307 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 308 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 309 | ptr = NULL; |
| 310 | png_ptr->offset_table_count_free++; |
| 311 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 312 | } |
| 313 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 314 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 315 | { |
| 316 | farfree(png_ptr->offset_table); |
| 317 | farfree(png_ptr->offset_table_ptr); |
| 318 | png_ptr->offset_table = NULL; |
| 319 | png_ptr->offset_table_ptr = NULL; |
| 320 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 321 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 322 | |
| 323 | if (ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 324 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 325 | farfree(ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 326 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | #else /* Not the Borland DOS special memory handler */ |
| 330 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 331 | /* 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] | 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 */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 335 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 336 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 337 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 338 | return (png_create_struct_2(type, NULL, NULL)); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* Allocate memory for a png_struct or a png_info. The malloc and |
| 342 | 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] | 343 | to improve performance noticably. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 344 | png_voidp /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 345 | 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] | 346 | { |
| 347 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 348 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 349 | png_voidp struct_ptr; |
| 350 | |
| 351 | if (type == PNG_STRUCT_INFO) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 352 | size = sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 353 | else if (type == PNG_STRUCT_PNG) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 354 | size = sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 355 | else |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 356 | return (NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 357 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 358 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 359 | if (malloc_fn != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 360 | { |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 361 | png_struct dummy_struct; |
| 362 | png_structp png_ptr = &dummy_struct; |
| 363 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | ae498dc | 2001-11-24 14:53:31 -0600 | [diff] [blame] | 364 | struct_ptr = (*(malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 365 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 366 | png_memset(struct_ptr, 0, size); |
| 367 | return (struct_ptr); |
| 368 | } |
| 369 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 370 | |
Glenn Randers-Pehrson | 1721829 | 2006-04-20 07:20:46 -0500 | [diff] [blame] | 371 | struct_ptr = (png_voidp)png_mem_alloc(size); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 372 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 373 | png_memset(struct_ptr, 0, size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 374 | |
| 375 | return (struct_ptr); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 380 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 381 | png_destroy_struct(png_voidp struct_ptr) |
| 382 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 383 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 384 | png_destroy_struct_2(struct_ptr, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* Free memory allocated by a png_create_struct() call */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 388 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 389 | png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, |
| 390 | png_voidp mem_ptr) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 391 | { |
| 392 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 393 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 394 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 395 | #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-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 400 | png_ptr->mem_ptr=mem_ptr; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 401 | (*(free_fn))(png_ptr, struct_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 1721829 | 2006-04-20 07:20:46 -0500 | [diff] [blame] | 405 | png_mem_free(struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 406 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 409 | /* Allocate memory. For reasonable files, size should never exceed |
| 410 | 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] | 411 | 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] | 412 | need to allocate exactly 64K, so whatever you call here must |
| 413 | have the ability to do that. */ |
| 414 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 415 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 416 | png_malloc(png_structp png_ptr, png_alloc_size_t size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 417 | { |
| 418 | png_voidp ret; |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 419 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 420 | #ifdef PNG_USER_MEM_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 421 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 422 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 423 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 424 | if (png_ptr->malloc_fn != NULL) |
| 425 | ret = (*(png_ptr->malloc_fn))(png_ptr, size); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 426 | else |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 427 | ret = png_malloc_default(png_ptr, size); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 428 | if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 429 | png_error(png_ptr, "Out of Memory!"); |
| 430 | return (ret); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 431 | } |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 432 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 433 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 434 | 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] | 435 | { |
| 436 | png_voidp ret; |
| 437 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 438 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 439 | if (png_ptr == NULL || size == 0) |
| 440 | return (NULL); |
| 441 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 442 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 443 | if (size > (png_alloc_size_t)65536L) |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 444 | { |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 445 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 446 | if(png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) |
| 447 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 448 | else |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 449 | #endif |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 452 | #endif |
| 453 | |
Glenn Randers-Pehrson | 1721829 | 2006-04-20 07:20:46 -0500 | [diff] [blame] | 454 | ret = png_mem_alloc(size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 455 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 456 | #ifndef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 457 | 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] | 458 | png_error(png_ptr, "Out of Memory"); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 459 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 460 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 461 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 462 | } |
| 463 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 464 | /* Free a pointer allocated by png_malloc(). If ptr is NULL, return |
| 465 | without taking any action. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 466 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 467 | png_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 468 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 469 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 470 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 471 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 472 | #ifdef PNG_USER_MEM_SUPPORTED |
| 473 | if (png_ptr->free_fn != NULL) |
| 474 | { |
| 475 | (*(png_ptr->free_fn))(png_ptr, ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | else png_free_default(png_ptr, ptr); |
| 479 | } |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 480 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 481 | png_free_default(png_structp png_ptr, png_voidp ptr) |
| 482 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 483 | if (png_ptr == NULL || ptr == NULL) |
| 484 | return; |
| 485 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 486 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | 1721829 | 2006-04-20 07:20:46 -0500 | [diff] [blame] | 487 | png_mem_free(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 490 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 491 | |
Glenn Randers-Pehrson | 2ae022d | 2002-07-01 22:23:46 -0500 | [diff] [blame] | 492 | /* 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] | 493 | * function will set up png_malloc() to issue a png_warning and return NULL |
| 494 | * instead of issuing a png_error, if it fails to allocate the requested |
| 495 | * memory. |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 496 | */ |
| 497 | png_voidp PNGAPI |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 498 | 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] | 499 | { |
| 500 | png_voidp ptr; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 501 | if(png_ptr == NULL) return (NULL); |
Glenn Randers-Pehrson | 07748d1 | 2002-05-25 11:12:10 -0500 | [diff] [blame] | 502 | png_uint_32 save_flags=png_ptr->flags; |
| 503 | |
| 504 | png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 505 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); |
| 506 | png_ptr->flags=save_flags; |
| 507 | return(ptr); |
| 508 | } |
| 509 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 510 | #ifdef PNG_USER_MEM_SUPPORTED |
| 511 | /* This function is called when the application wants to use another method |
| 512 | * of allocating and freeing memory. |
| 513 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 514 | void PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 515 | png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr |
| 516 | malloc_fn, png_free_ptr free_fn) |
| 517 | { |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 518 | if(png_ptr != NULL) { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 519 | png_ptr->mem_ptr = mem_ptr; |
| 520 | png_ptr->malloc_fn = malloc_fn; |
| 521 | png_ptr->free_fn = free_fn; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 522 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* This function returns a pointer to the mem_ptr associated with the user |
| 526 | * functions. The application should free any memory associated with this |
| 527 | * pointer before png_write_destroy and png_read_destroy are called. |
| 528 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 529 | png_voidp PNGAPI |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 530 | png_get_mem_ptr(png_structp png_ptr) |
| 531 | { |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame^] | 532 | if(png_ptr == NULL) return (NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 533 | return ((png_voidp)png_ptr->mem_ptr); |
| 534 | } |
| 535 | #endif /* PNG_USER_MEM_SUPPORTED */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 536 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 537 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |