CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libpng17/pngmem.c
Views: 1401
1/* pngmem.c - stub functions for memory allocation2*3* Last changed in libpng 1.7.0 [(PENDING RELEASE)]4* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12* This file provides a location for all memory allocation. Users who13* need special memory handling are expected to supply replacement14* functions for png_malloc() and png_free(), and to use15* png_create_read_struct_2() and png_create_write_struct_2() to16* identify the replacement functions.17*/1819#include "pngpriv.h"20#define PNG_SRC_FILE PNG_SRC_FILE_pngmem2122#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)23/* Free a png_struct */24void /* PRIVATE */25png_destroy_png_struct(png_structrp png_ptr)26{27if (png_ptr != NULL)28{29/* png_free might call png_error and may certainly call30* png_get_mem_ptr, so fake a temporary png_struct to support this.31*/32png_struct dummy_struct = *png_ptr;33memset(png_ptr, 0, (sizeof *png_ptr));34png_free(&dummy_struct, png_ptr);3536# ifdef PNG_SETJMP_SUPPORTED37/* We may have a jmp_buf left to deallocate. */38png_free_jmpbuf(&dummy_struct);39# endif40}41}4243/* Allocate memory. For reasonable files, size should never exceed44* 64K. However, zlib may allocate more than 64K if you don't tell45* it not to. See zconf.h and png.h for more information. zlib does46* need to allocate exactly 64K, so whatever you call here must47* have the ability to do that.48*/49PNG_FUNCTION(png_voidp,PNGAPI50png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)51{52png_voidp ret;5354ret = png_malloc(png_ptr, size);5556if (ret != NULL)57memset(ret, 0, size);5859return ret;60}6162/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of63* allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.64* Checking and error handling must happen outside this routine; it returns NULL65* if the allocation cannot be done (for any reason.)66*/67PNG_FUNCTION(png_voidp /* PRIVATE */,68png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),69PNG_ALLOCATED)70{71/* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS72* allocators have also been removed in 1.6.0, so any 16-bit system now has73* to implement a user memory handler. This checks to be sure it isn't74* called with big numbers.75*/76#ifndef PNG_USER_MEM_SUPPORTED77PNG_UNUSED(png_ptr)78#endif7980/* Some compilers complain that this is always true. However, it81* can be false when integer overflow happens.82*/83#ifdef PNG_MAX_MALLOC_64K84if (size > 0 && size <= PNG_SIZE_MAX && size <= 65536U)85#else86if (size > 0 && size <= PNG_SIZE_MAX)87#endif88{89png_voidp result;9091#ifdef PNG_USER_MEM_SUPPORTED92if (png_ptr != NULL && png_ptr->malloc_fn != NULL)93result = png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);9495else96#endif97result = malloc((size_t)size); /* checked for truncation above */9899return result;100}101102else103return NULL;104}105106#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\107defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)108/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7109* that arises because of the checks in png_realloc_array that are repeated in110* png_malloc_array.111*/112static png_voidp113png_malloc_array_checked(png_const_structrp png_ptr, int nelements,114size_t element_size)115{116png_alloc_size_t req = nelements; /* known to be > 0 */117118if (req <= PNG_SIZE_MAX/element_size)119return png_malloc_base(png_ptr, req * element_size);120121/* The failure case when the request is too large */122return NULL;123}124125PNG_FUNCTION(png_voidp /* PRIVATE */,126png_malloc_array,(png_const_structrp png_ptr, int nelements,127size_t element_size),PNG_ALLOCATED)128{129affirm(nelements > 0 && element_size > 0);130return png_malloc_array_checked(png_ptr, nelements, element_size);131}132133PNG_FUNCTION(png_voidp /* PRIVATE */,134png_realloc_array,(png_structrp png_ptr, png_const_voidp old_array,135int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)136{137/* These are internal errors: */138affirm(add_elements > 0 && element_size > 0 && old_elements >= 0 &&139(old_array != NULL || old_elements == 0));140141/* Check for overflow on the elements count (so the caller does not have to142* check.)143*/144if (add_elements <= INT_MAX - old_elements)145{146png_voidp new_array = png_malloc_array_checked(png_ptr,147old_elements+add_elements, element_size);148149if (new_array != NULL)150{151/* Because png_malloc_array worked the size calculations below cannot152* overflow.153*/154if (old_elements > 0)155memcpy(new_array, old_array, element_size*(unsigned)old_elements);156157memset((char*)new_array + element_size*(unsigned)old_elements, 0,158element_size*(unsigned)add_elements);159160return new_array;161}162}163164#ifdef PNG_READ_SUPPORTED165# ifdef PNG_USER_LIMITS_SUPPORTED166/* The potential overflow case. Set the cache counter so libpng will167* not make any more attempts168*/169png_ptr->user_chunk_cache_max = 2;170# endif171#endif172173return NULL; /* error */174}175#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */176177/* Various functions that have different error handling are derived from this.178* png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate179* function png_malloc_default is also provided.180*/181PNG_FUNCTION(png_voidp,PNGAPI182png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)183{184png_voidp ret;185186if (png_ptr == NULL)187return NULL;188189ret = png_malloc_base(png_ptr, size);190191if (ret == NULL)192png_error(png_ptr, "Out of memory");193194return ret;195}196197/* This function was added at libpng version 1.2.3. The png_malloc_warn()198* function will issue a png_warning and return NULL instead of issuing a199* png_error, if it fails to allocate the requested memory.200*/201PNG_FUNCTION(png_voidp,PNGAPI202png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),203PNG_ALLOCATED)204{205if (png_ptr != NULL)206{207png_voidp ret = png_malloc_base(png_ptr, size);208209if (ret != NULL)210return ret;211212png_warning(png_ptr, "Out of memory");213}214215return NULL;216}217218/* Free a pointer allocated by png_malloc(). If ptr is NULL, return219* without taking any action.220*/221void PNGAPI222png_free(png_const_structrp png_ptr, png_voidp ptr)223{224if (png_ptr == NULL || ptr == NULL)225return;226227#ifdef PNG_USER_MEM_SUPPORTED228if (png_ptr->free_fn != NULL)229png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);230231else232free(ptr);233#else234free(ptr);235#endif /* USER_MEM */236237}238239#ifdef PNG_USER_MEM_SUPPORTED240/* This function is called when the application wants to use another method241* of allocating and freeing memory.242*/243void PNGAPI244png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr245malloc_fn, png_free_ptr free_fn)246{247if (png_ptr != NULL)248{249png_ptr->mem_ptr = mem_ptr;250png_ptr->malloc_fn = malloc_fn;251png_ptr->free_fn = free_fn;252}253}254255/* This function returns a pointer to the mem_ptr associated with the user256* functions. The application should free any memory associated with this257* pointer before png_write_destroy and png_read_destroy are called.258*/259png_voidp PNGAPI260png_get_mem_ptr(png_const_structrp png_ptr)261{262if (png_ptr == NULL)263return NULL;264265return png_ptr->mem_ptr;266}267#endif /* USER_MEM */268#endif /* READ || WRITE */269270271