Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(665)

Side by Side Diff: third_party/libpng/pngmem.c

Issue 2021403002: Update libpng to 1.6.22 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rearrange pnglibconf.h Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libpng/pnglibconf.h ('k') | third_party/libpng/pngpread.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* pngmem.c - stub functions for memory allocation 2 /* pngmem.c - stub functions for memory allocation
3 * 3 *
4 * Last changed in libpng 1.2.41 [February 25, 2010] 4 * Last changed in libpng 1.6.15 [November 20, 2014]
5 * Copyright (c) 1998-2002,2004,2006-2010 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 * 8 *
9 * This code is released under the libpng license. 9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h 11 * and license in png.h
12 * 12 *
13 * This file provides a location for all memory allocation. Users who 13 * This file provides a location for all memory allocation. Users who
14 * need special memory handling are expected to supply replacement 14 * need special memory handling are expected to supply replacement
15 * functions for png_malloc() and png_free(), and to use 15 * functions for png_malloc() and png_free(), and to use
16 * png_create_read_struct_2() and png_create_write_struct_2() to 16 * png_create_read_struct_2() and png_create_write_struct_2() to
17 * identify the replacement functions. 17 * identify the replacement functions.
18 */ 18 */
19 19
20 #define PNG_INTERNAL 20 #include "pngpriv.h"
21 #define PNG_NO_PEDANTIC_WARNINGS 21
22 #include "png.h"
23 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) 22 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
24 23 /* Free a png_struct */
25 /* Borland DOS special memory handler */
26 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
27 /* If you change this, be sure to change the one in png.h also */
28
29 /* Allocate memory for a png_struct. The malloc and memset can be replaced
30 by a single call to calloc() if this is thought to improve performance. */
31 png_voidp /* PRIVATE */
32 png_create_struct(int type)
33 {
34 #ifdef PNG_USER_MEM_SUPPORTED
35 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
36 }
37
38 /* Alternate version of png_create_struct, for use with user-defined malloc. */
39 png_voidp /* PRIVATE */
40 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
41 {
42 #endif /* PNG_USER_MEM_SUPPORTED */
43 png_size_t size;
44 png_voidp struct_ptr;
45
46 if (type == PNG_STRUCT_INFO)
47 size = png_sizeof(png_info);
48 else if (type == PNG_STRUCT_PNG)
49 size = png_sizeof(png_struct);
50 else
51 return (png_get_copyright(NULL));
52
53 #ifdef PNG_USER_MEM_SUPPORTED
54 if (malloc_fn != NULL)
55 {
56 png_struct dummy_struct;
57 png_structp png_ptr = &dummy_struct;
58 png_ptr->mem_ptr=mem_ptr;
59 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
60 }
61 else
62 #endif /* PNG_USER_MEM_SUPPORTED */
63 struct_ptr = (png_voidp)farmalloc(size);
64 if (struct_ptr != NULL)
65 png_memset(struct_ptr, 0, size);
66 return (struct_ptr);
67 }
68
69 /* Free memory allocated by a png_create_struct() call */
70 void /* PRIVATE */ 24 void /* PRIVATE */
71 png_destroy_struct(png_voidp struct_ptr) 25 png_destroy_png_struct(png_structrp png_ptr)
72 { 26 {
73 #ifdef PNG_USER_MEM_SUPPORTED 27 if (png_ptr != NULL)
74 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL); 28 {
75 } 29 /* png_free might call png_error and may certainly call
76 30 * png_get_mem_ptr, so fake a temporary png_struct to support this.
77 /* Free memory allocated by a png_create_struct() call */ 31 */
78 void /* PRIVATE */ 32 png_struct dummy_struct = *png_ptr;
79 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, 33 memset(png_ptr, 0, (sizeof *png_ptr));
80 png_voidp mem_ptr) 34 png_free(&dummy_struct, png_ptr);
81 { 35
82 #endif 36 # ifdef PNG_SETJMP_SUPPORTED
83 if (struct_ptr != NULL) 37 /* We may have a jmp_buf left to deallocate. */
84 { 38 png_free_jmpbuf(&dummy_struct);
85 #ifdef PNG_USER_MEM_SUPPORTED 39 # endif
86 if (free_fn != NULL)
87 {
88 png_struct dummy_struct;
89 png_structp png_ptr = &dummy_struct;
90 png_ptr->mem_ptr=mem_ptr;
91 (*(free_fn))(png_ptr, struct_ptr);
92 return;
93 }
94 #endif /* PNG_USER_MEM_SUPPORTED */
95 farfree (struct_ptr);
96 } 40 }
97 } 41 }
98 42
99 /* Allocate memory. For reasonable files, size should never exceed 43 /* Allocate memory. For reasonable files, size should never exceed
100 * 64K. However, zlib may allocate more then 64K if you don't tell 44 * 64K. However, zlib may allocate more than 64K if you don't tell
101 * it not to. See zconf.h and png.h for more information. zlib does
102 * need to allocate exactly 64K, so whatever you call here must
103 * have the ability to do that.
104 *
105 * Borland seems to have a problem in DOS mode for exactly 64K.
106 * It gives you a segment with an offset of 8 (perhaps to store its
107 * memory stuff). zlib doesn't like this at all, so we have to
108 * detect and deal with it. This code should not be needed in
109 * Windows or OS/2 modes, and only in 16 bit mode. This code has
110 * been updated by Alexander Lehmann for version 0.89 to waste less
111 * memory.
112 *
113 * Note that we can't use png_size_t for the "size" declaration,
114 * since on some systems a png_size_t is a 16-bit quantity, and as a
115 * result, we would be truncating potentially larger memory requests
116 * (which should cause a fatal error) and introducing major problems.
117 */
118 png_voidp /* PRIVATE */
119 png_calloc(png_structp png_ptr, png_uint_32 size)
120 {
121 png_voidp ret;
122
123 ret = (png_malloc(png_ptr, size));
124 if (ret != NULL)
125 png_memset(ret,0,(png_size_t)size);
126 return (ret);
127 }
128
129 png_voidp PNGAPI
130 png_malloc(png_structp png_ptr, png_uint_32 size)
131 {
132 png_voidp ret;
133
134 if (png_ptr == NULL || size == 0)
135 return (NULL);
136
137 #ifdef PNG_USER_MEM_SUPPORTED
138 if (png_ptr->malloc_fn != NULL)
139 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
140 else
141 ret = (png_malloc_default(png_ptr, size));
142 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
143 png_error(png_ptr, "Out of memory!");
144 return (ret);
145 }
146
147 png_voidp PNGAPI
148 png_malloc_default(png_structp png_ptr, png_uint_32 size)
149 {
150 png_voidp ret;
151 #endif /* PNG_USER_MEM_SUPPORTED */
152
153 if (png_ptr == NULL || size == 0)
154 return (NULL);
155
156 #ifdef PNG_MAX_MALLOC_64K
157 if (size > (png_uint_32)65536L)
158 {
159 png_warning(png_ptr, "Cannot Allocate > 64K");
160 ret = NULL;
161 }
162 else
163 #endif
164
165 if (size != (size_t)size)
166 ret = NULL;
167 else if (size == (png_uint_32)65536L)
168 {
169 if (png_ptr->offset_table == NULL)
170 {
171 /* Try to see if we need to do any of this fancy stuff */
172 ret = farmalloc(size);
173 if (ret == NULL || ((png_size_t)ret & 0xffff))
174 {
175 int num_blocks;
176 png_uint_32 total_size;
177 png_bytep table;
178 int i;
179 png_byte huge * hptr;
180
181 if (ret != NULL)
182 {
183 farfree(ret);
184 ret = NULL;
185 }
186
187 if (png_ptr->zlib_window_bits > 14)
188 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
189 else
190 num_blocks = 1;
191 if (png_ptr->zlib_mem_level >= 7)
192 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
193 else
194 num_blocks++;
195
196 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
197
198 table = farmalloc(total_size);
199
200 if (table == NULL)
201 {
202 #ifndef PNG_USER_MEM_SUPPORTED
203 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
204 png_error(png_ptr, "Out Of Memory."); /* Note "O", "M" */
205 else
206 png_warning(png_ptr, "Out Of Memory.");
207 #endif
208 return (NULL);
209 }
210
211 if ((png_size_t)table & 0xfff0)
212 {
213 #ifndef PNG_USER_MEM_SUPPORTED
214 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
215 png_error(png_ptr,
216 "Farmalloc didn't return normalized pointer");
217 else
218 png_warning(png_ptr,
219 "Farmalloc didn't return normalized pointer");
220 #endif
221 return (NULL);
222 }
223
224 png_ptr->offset_table = table;
225 png_ptr->offset_table_ptr = farmalloc(num_blocks *
226 png_sizeof(png_bytep));
227
228 if (png_ptr->offset_table_ptr == NULL)
229 {
230 #ifndef PNG_USER_MEM_SUPPORTED
231 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
232 png_error(png_ptr, "Out Of memory."); /* Note "O", "m" */
233 else
234 png_warning(png_ptr, "Out Of memory.");
235 #endif
236 return (NULL);
237 }
238
239 hptr = (png_byte huge *)table;
240 if ((png_size_t)hptr & 0xf)
241 {
242 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
243 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
244 }
245 for (i = 0; i < num_blocks; i++)
246 {
247 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
248 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
249 }
250
251 png_ptr->offset_table_number = num_blocks;
252 png_ptr->offset_table_count = 0;
253 png_ptr->offset_table_count_free = 0;
254 }
255 }
256
257 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
258 {
259 #ifndef PNG_USER_MEM_SUPPORTED
260 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
261 png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
262 else
263 png_warning(png_ptr, "Out of Memory.");
264 #endif
265 return (NULL);
266 }
267
268 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
269 }
270 else
271 ret = farmalloc(size);
272
273 #ifndef PNG_USER_MEM_SUPPORTED
274 if (ret == NULL)
275 {
276 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
277 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
278 else
279 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
280 }
281 #endif
282
283 return (ret);
284 }
285
286 /* Free a pointer allocated by png_malloc(). In the default
287 * configuration, png_ptr is not used, but is passed in case it
288 * is needed. If ptr is NULL, return without taking any action.
289 */
290 void PNGAPI
291 png_free(png_structp png_ptr, png_voidp ptr)
292 {
293 if (png_ptr == NULL || ptr == NULL)
294 return;
295
296 #ifdef PNG_USER_MEM_SUPPORTED
297 if (png_ptr->free_fn != NULL)
298 {
299 (*(png_ptr->free_fn))(png_ptr, ptr);
300 return;
301 }
302 else
303 png_free_default(png_ptr, ptr);
304 }
305
306 void PNGAPI
307 png_free_default(png_structp png_ptr, png_voidp ptr)
308 {
309 #endif /* PNG_USER_MEM_SUPPORTED */
310
311 if (png_ptr == NULL || ptr == NULL)
312 return;
313
314 if (png_ptr->offset_table != NULL)
315 {
316 int i;
317
318 for (i = 0; i < png_ptr->offset_table_count; i++)
319 {
320 if (ptr == png_ptr->offset_table_ptr[i])
321 {
322 ptr = NULL;
323 png_ptr->offset_table_count_free++;
324 break;
325 }
326 }
327 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
328 {
329 farfree(png_ptr->offset_table);
330 farfree(png_ptr->offset_table_ptr);
331 png_ptr->offset_table = NULL;
332 png_ptr->offset_table_ptr = NULL;
333 }
334 }
335
336 if (ptr != NULL)
337 {
338 farfree(ptr);
339 }
340 }
341
342 #else /* Not the Borland DOS special memory handler */
343
344 /* Allocate memory for a png_struct or a png_info. The malloc and
345 memset can be replaced by a single call to calloc() if this is thought
346 to improve performance noticably. */
347 png_voidp /* PRIVATE */
348 png_create_struct(int type)
349 {
350 #ifdef PNG_USER_MEM_SUPPORTED
351 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
352 }
353
354 /* Allocate memory for a png_struct or a png_info. The malloc and
355 memset can be replaced by a single call to calloc() if this is thought
356 to improve performance noticably. */
357 png_voidp /* PRIVATE */
358 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
359 {
360 #endif /* PNG_USER_MEM_SUPPORTED */
361 png_size_t size;
362 png_voidp struct_ptr;
363
364 if (type == PNG_STRUCT_INFO)
365 size = png_sizeof(png_info);
366 else if (type == PNG_STRUCT_PNG)
367 size = png_sizeof(png_struct);
368 else
369 return (NULL);
370
371 #ifdef PNG_USER_MEM_SUPPORTED
372 if (malloc_fn != NULL)
373 {
374 png_struct dummy_struct;
375 png_structp png_ptr = &dummy_struct;
376 png_ptr->mem_ptr=mem_ptr;
377 struct_ptr = (*(malloc_fn))(png_ptr, size);
378 if (struct_ptr != NULL)
379 png_memset(struct_ptr, 0, size);
380 return (struct_ptr);
381 }
382 #endif /* PNG_USER_MEM_SUPPORTED */
383
384 #if defined(__TURBOC__) && !defined(__FLAT__)
385 struct_ptr = (png_voidp)farmalloc(size);
386 #else
387 # if defined(_MSC_VER) && defined(MAXSEG_64K)
388 struct_ptr = (png_voidp)halloc(size, 1);
389 # else
390 struct_ptr = (png_voidp)malloc(size);
391 # endif
392 #endif
393 if (struct_ptr != NULL)
394 png_memset(struct_ptr, 0, size);
395
396 return (struct_ptr);
397 }
398
399
400 /* Free memory allocated by a png_create_struct() call */
401 void /* PRIVATE */
402 png_destroy_struct(png_voidp struct_ptr)
403 {
404 #ifdef PNG_USER_MEM_SUPPORTED
405 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
406 }
407
408 /* Free memory allocated by a png_create_struct() call */
409 void /* PRIVATE */
410 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
411 png_voidp mem_ptr)
412 {
413 #endif /* PNG_USER_MEM_SUPPORTED */
414 if (struct_ptr != NULL)
415 {
416 #ifdef PNG_USER_MEM_SUPPORTED
417 if (free_fn != NULL)
418 {
419 png_struct dummy_struct;
420 png_structp png_ptr = &dummy_struct;
421 png_ptr->mem_ptr=mem_ptr;
422 (*(free_fn))(png_ptr, struct_ptr);
423 return;
424 }
425 #endif /* PNG_USER_MEM_SUPPORTED */
426 #if defined(__TURBOC__) && !defined(__FLAT__)
427 farfree(struct_ptr);
428 #else
429 # if defined(_MSC_VER) && defined(MAXSEG_64K)
430 hfree(struct_ptr);
431 # else
432 free(struct_ptr);
433 # endif
434 #endif
435 }
436 }
437
438 /* Allocate memory. For reasonable files, size should never exceed
439 * 64K. However, zlib may allocate more then 64K if you don't tell
440 * it not to. See zconf.h and png.h for more information. zlib does 45 * it not to. See zconf.h and png.h for more information. zlib does
441 * need to allocate exactly 64K, so whatever you call here must 46 * need to allocate exactly 64K, so whatever you call here must
442 * have the ability to do that. 47 * have the ability to do that.
443 */ 48 */
444 49 PNG_FUNCTION(png_voidp,PNGAPI
445 png_voidp /* PRIVATE */ 50 png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
446 png_calloc(png_structp png_ptr, png_uint_32 size)
447 { 51 {
448 png_voidp ret; 52 png_voidp ret;
449 53
450 ret = (png_malloc(png_ptr, size)); 54 ret = png_malloc(png_ptr, size);
55
451 if (ret != NULL) 56 if (ret != NULL)
452 png_memset(ret,0,(png_size_t)size); 57 memset(ret, 0, size);
453 return (ret); 58
454 } 59 return ret;
455 60 }
456 png_voidp PNGAPI 61
457 png_malloc(png_structp png_ptr, png_uint_32 size) 62 /* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
458 { 63 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
459 png_voidp ret; 64 * Checking and error handling must happen outside this routine; it returns NULL
460 65 * if the allocation cannot be done (for any reason.)
461 #ifdef PNG_USER_MEM_SUPPORTED 66 */
462 if (png_ptr == NULL || size == 0) 67 PNG_FUNCTION(png_voidp /* PRIVATE */,
463 return (NULL); 68 png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
464 69 PNG_ALLOCATED)
465 if (png_ptr->malloc_fn != NULL) 70 {
466 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); 71 /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
467 else 72 * allocators have also been removed in 1.6.0, so any 16-bit system now has
468 ret = (png_malloc_default(png_ptr, size)); 73 * to implement a user memory handler. This checks to be sure it isn't
469 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) 74 * called with big numbers.
470 png_error(png_ptr, "Out of Memory!"); 75 */
471 return (ret);
472 }
473
474 png_voidp PNGAPI
475 png_malloc_default(png_structp png_ptr, png_uint_32 size)
476 {
477 png_voidp ret;
478 #endif /* PNG_USER_MEM_SUPPORTED */
479
480 if (png_ptr == NULL || size == 0)
481 return (NULL);
482
483 #ifdef PNG_MAX_MALLOC_64K
484 if (size > (png_uint_32)65536L)
485 {
486 #ifndef PNG_USER_MEM_SUPPORTED 76 #ifndef PNG_USER_MEM_SUPPORTED
487 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) 77 PNG_UNUSED(png_ptr)
488 png_error(png_ptr, "Cannot Allocate > 64K"); 78 #endif
79
80 /* Some compilers complain that this is always true. However, it
81 * can be false when integer overflow happens.
82 */
83 if (size > 0 && size <= PNG_SIZE_MAX
84 # ifdef PNG_MAX_MALLOC_64K
85 && size <= 65536U
86 # endif
87 )
88 {
89 #ifdef PNG_USER_MEM_SUPPORTED
90 if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
91 return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
92
489 else 93 else
490 #endif 94 #endif
491 return NULL; 95 return malloc((size_t)size); /* checked for truncation above */
492 } 96 }
493 #endif 97
494
495 /* Check for overflow */
496 #if defined(__TURBOC__) && !defined(__FLAT__)
497 if (size != (unsigned long)size)
498 ret = NULL;
499 else 98 else
500 ret = farmalloc(size); 99 return NULL;
501 #else 100 }
502 # if defined(_MSC_VER) && defined(MAXSEG_64K) 101
503 if (size != (unsigned long)size) 102 #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
504 ret = NULL; 103 defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
505 else 104 /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
506 ret = halloc(size, 1); 105 * that arises because of the checks in png_realloc_array that are repeated in
507 # else 106 * png_malloc_array.
508 if (size != (size_t)size) 107 */
509 ret = NULL; 108 static png_voidp
510 else 109 png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
511 ret = malloc((size_t)size); 110 size_t element_size)
512 # endif 111 {
513 #endif 112 png_alloc_size_t req = nelements; /* known to be > 0 */
514 113
515 #ifndef PNG_USER_MEM_SUPPORTED 114 if (req <= PNG_SIZE_MAX/element_size)
516 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) 115 return png_malloc_base(png_ptr, req * element_size);
517 png_error(png_ptr, "Out of Memory"); 116
518 #endif 117 /* The failure case when the request is too large */
519 118 return NULL;
520 return (ret); 119 }
120
121 PNG_FUNCTION(png_voidp /* PRIVATE */,
122 png_malloc_array,(png_const_structrp png_ptr, int nelements,
123 size_t element_size),PNG_ALLOCATED)
124 {
125 if (nelements <= 0 || element_size == 0)
126 png_error(png_ptr, "internal error: array alloc");
127
128 return png_malloc_array_checked(png_ptr, nelements, element_size);
129 }
130
131 PNG_FUNCTION(png_voidp /* PRIVATE */,
132 png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
133 int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
134 {
135 /* These are internal errors: */
136 if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
137 (old_array == NULL && old_elements > 0))
138 png_error(png_ptr, "internal error: array realloc");
139
140 /* Check for overflow on the elements count (so the caller does not have to
141 * check.)
142 */
143 if (add_elements <= INT_MAX - old_elements)
144 {
145 png_voidp new_array = png_malloc_array_checked(png_ptr,
146 old_elements+add_elements, element_size);
147
148 if (new_array != NULL)
149 {
150 /* Because png_malloc_array worked the size calculations below cannot
151 * overflow.
152 */
153 if (old_elements > 0)
154 memcpy(new_array, old_array, element_size*(unsigned)old_elements);
155
156 memset((char*)new_array + element_size*(unsigned)old_elements, 0,
157 element_size*(unsigned)add_elements);
158
159 return new_array;
160 }
161 }
162
163 return NULL; /* error */
164 }
165 #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
166
167 /* Various functions that have different error handling are derived from this.
168 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
169 * function png_malloc_default is also provided.
170 */
171 PNG_FUNCTION(png_voidp,PNGAPI
172 png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
173 {
174 png_voidp ret;
175
176 if (png_ptr == NULL)
177 return NULL;
178
179 ret = png_malloc_base(png_ptr, size);
180
181 if (ret == NULL)
182 png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
183
184 return ret;
185 }
186
187 #ifdef PNG_USER_MEM_SUPPORTED
188 PNG_FUNCTION(png_voidp,PNGAPI
189 png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
190 PNG_ALLOCATED PNG_DEPRECATED)
191 {
192 png_voidp ret;
193
194 if (png_ptr == NULL)
195 return NULL;
196
197 /* Passing 'NULL' here bypasses the application provided memory handler. */
198 ret = png_malloc_base(NULL/*use malloc*/, size);
199
200 if (ret == NULL)
201 png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
202
203 return ret;
204 }
205 #endif /* USER_MEM */
206
207 /* This function was added at libpng version 1.2.3. The png_malloc_warn()
208 * function will issue a png_warning and return NULL instead of issuing a
209 * png_error, if it fails to allocate the requested memory.
210 */
211 PNG_FUNCTION(png_voidp,PNGAPI
212 png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
213 PNG_ALLOCATED)
214 {
215 if (png_ptr != NULL)
216 {
217 png_voidp ret = png_malloc_base(png_ptr, size);
218
219 if (ret != NULL)
220 return ret;
221
222 png_warning(png_ptr, "Out of memory");
223 }
224
225 return NULL;
521 } 226 }
522 227
523 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return 228 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
524 * without taking any action. 229 * without taking any action.
525 */ 230 */
526 void PNGAPI 231 void PNGAPI
527 png_free(png_structp png_ptr, png_voidp ptr) 232 png_free(png_const_structrp png_ptr, png_voidp ptr)
528 { 233 {
529 if (png_ptr == NULL || ptr == NULL) 234 if (png_ptr == NULL || ptr == NULL)
530 return; 235 return;
531 236
532 #ifdef PNG_USER_MEM_SUPPORTED 237 #ifdef PNG_USER_MEM_SUPPORTED
533 if (png_ptr->free_fn != NULL) 238 if (png_ptr->free_fn != NULL)
534 { 239 png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
535 (*(png_ptr->free_fn))(png_ptr, ptr); 240
536 return;
537 }
538 else 241 else
539 png_free_default(png_ptr, ptr); 242 png_free_default(png_ptr, ptr);
540 } 243 }
541 void PNGAPI 244
542 png_free_default(png_structp png_ptr, png_voidp ptr) 245 PNG_FUNCTION(void,PNGAPI
246 png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
543 { 247 {
544 if (png_ptr == NULL || ptr == NULL) 248 if (png_ptr == NULL || ptr == NULL)
545 return; 249 return;
546 250 #endif /* USER_MEM */
547 #endif /* PNG_USER_MEM_SUPPORTED */ 251
548
549 #if defined(__TURBOC__) && !defined(__FLAT__)
550 farfree(ptr);
551 #else
552 # if defined(_MSC_VER) && defined(MAXSEG_64K)
553 hfree(ptr);
554 # else
555 free(ptr); 252 free(ptr);
556 # endif 253 }
557 #endif 254
558 } 255 #ifdef PNG_USER_MEM_SUPPORTED
559
560 #endif /* Not Borland DOS special memory handler */
561
562 #ifdef PNG_1_0_X
563 # define png_malloc_warn png_malloc
564 #else
565 /* This function was added at libpng version 1.2.3. The png_malloc_warn()
566 * function will set up png_malloc() to issue a png_warning and return NULL
567 * instead of issuing a png_error, if it fails to allocate the requested
568 * memory.
569 */
570 png_voidp PNGAPI
571 png_malloc_warn(png_structp png_ptr, png_uint_32 size)
572 {
573 png_voidp ptr;
574 png_uint_32 save_flags;
575 if (png_ptr == NULL)
576 return (NULL);
577
578 save_flags = png_ptr->flags;
579 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
580 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
581 png_ptr->flags=save_flags;
582 return(ptr);
583 }
584 #endif
585
586 png_voidp PNGAPI
587 png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
588 png_uint_32 length)
589 {
590 png_size_t size;
591
592 size = (png_size_t)length;
593 if ((png_uint_32)size != length)
594 png_error(png_ptr, "Overflow in png_memcpy_check.");
595
596 return(png_memcpy (s1, s2, size));
597 }
598
599 png_voidp PNGAPI
600 png_memset_check (png_structp png_ptr, png_voidp s1, int value,
601 png_uint_32 length)
602 {
603 png_size_t size;
604
605 size = (png_size_t)length;
606 if ((png_uint_32)size != length)
607 png_error(png_ptr, "Overflow in png_memset_check.");
608
609 return (png_memset (s1, value, size));
610
611 }
612
613 #ifdef PNG_USER_MEM_SUPPORTED
614 /* This function is called when the application wants to use another method 256 /* This function is called when the application wants to use another method
615 * of allocating and freeing memory. 257 * of allocating and freeing memory.
616 */ 258 */
617 void PNGAPI 259 void PNGAPI
618 png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr 260 png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
619 malloc_fn, png_free_ptr free_fn) 261 malloc_fn, png_free_ptr free_fn)
620 { 262 {
621 if (png_ptr != NULL) 263 if (png_ptr != NULL)
622 { 264 {
623 png_ptr->mem_ptr = mem_ptr; 265 png_ptr->mem_ptr = mem_ptr;
624 png_ptr->malloc_fn = malloc_fn; 266 png_ptr->malloc_fn = malloc_fn;
625 png_ptr->free_fn = free_fn; 267 png_ptr->free_fn = free_fn;
626 } 268 }
627 } 269 }
628 270
629 /* This function returns a pointer to the mem_ptr associated with the user 271 /* This function returns a pointer to the mem_ptr associated with the user
630 * functions. The application should free any memory associated with this 272 * functions. The application should free any memory associated with this
631 * pointer before png_write_destroy and png_read_destroy are called. 273 * pointer before png_write_destroy and png_read_destroy are called.
632 */ 274 */
633 png_voidp PNGAPI 275 png_voidp PNGAPI
634 png_get_mem_ptr(png_structp png_ptr) 276 png_get_mem_ptr(png_const_structrp png_ptr)
635 { 277 {
636 if (png_ptr == NULL) 278 if (png_ptr == NULL)
637 return (NULL); 279 return NULL;
638 return ((png_voidp)png_ptr->mem_ptr); 280
281 return png_ptr->mem_ptr;
639 } 282 }
640 #endif /* PNG_USER_MEM_SUPPORTED */ 283 #endif /* USER_MEM */
641 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ 284 #endif /* READ || WRITE */
OLDNEW
« no previous file with comments | « third_party/libpng/pnglibconf.h ('k') | third_party/libpng/pngpread.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698