Path: blob/master/src/java.desktop/share/native/libsplashscreen/libpng/pngmem.c
41155 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* pngmem.c - stub functions for memory allocation25*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file and, per its terms, should not be removed:30*31* Copyright (c) 2018 Cosmin Truta32* Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson33* Copyright (c) 1996-1997 Andreas Dilger34* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.35*36* This code is released under the libpng license.37* For conditions of distribution and use, see the disclaimer38* and license in png.h39*40* This file provides a location for all memory allocation. Users who41* need special memory handling are expected to supply replacement42* functions for png_malloc() and png_free(), and to use43* png_create_read_struct_2() and png_create_write_struct_2() to44* identify the replacement functions.45*/4647#include "pngpriv.h"4849#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)50/* Free a png_struct */51void /* PRIVATE */52png_destroy_png_struct(png_structrp png_ptr)53{54if (png_ptr != NULL)55{56/* png_free might call png_error and may certainly call57* png_get_mem_ptr, so fake a temporary png_struct to support this.58*/59png_struct dummy_struct = *png_ptr;60memset(png_ptr, 0, (sizeof *png_ptr));61png_free(&dummy_struct, png_ptr);6263# ifdef PNG_SETJMP_SUPPORTED64/* We may have a jmp_buf left to deallocate. */65png_free_jmpbuf(&dummy_struct);66# endif67}68}6970/* Allocate memory. For reasonable files, size should never exceed71* 64K. However, zlib may allocate more than 64K if you don't tell72* it not to. See zconf.h and png.h for more information. zlib does73* need to allocate exactly 64K, so whatever you call here must74* have the ability to do that.75*/76PNG_FUNCTION(png_voidp,PNGAPI77png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)78{79png_voidp ret;8081ret = png_malloc(png_ptr, size);8283if (ret != NULL)84memset(ret, 0, size);8586return ret;87}8889/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of90* allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.91* Checking and error handling must happen outside this routine; it returns NULL92* if the allocation cannot be done (for any reason.)93*/94PNG_FUNCTION(png_voidp /* PRIVATE */,95png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),96PNG_ALLOCATED)97{98/* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS99* allocators have also been removed in 1.6.0, so any 16-bit system now has100* to implement a user memory handler. This checks to be sure it isn't101* called with big numbers.102*/103#ifndef PNG_USER_MEM_SUPPORTED104PNG_UNUSED(png_ptr)105#endif106107/* Some compilers complain that this is always true. However, it108* can be false when integer overflow happens.109*/110if (size > 0 && size <= PNG_SIZE_MAX111# ifdef PNG_MAX_MALLOC_64K112&& size <= 65536U113# endif114)115{116#ifdef PNG_USER_MEM_SUPPORTED117if (png_ptr != NULL && png_ptr->malloc_fn != NULL)118return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);119120else121#endif122return malloc((size_t)size); /* checked for truncation above */123}124125else126return NULL;127}128129#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\130defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)131/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7132* that arises because of the checks in png_realloc_array that are repeated in133* png_malloc_array.134*/135static png_voidp136png_malloc_array_checked(png_const_structrp png_ptr, int nelements,137size_t element_size)138{139png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */140141if (req <= PNG_SIZE_MAX/element_size)142return png_malloc_base(png_ptr, req * element_size);143144/* The failure case when the request is too large */145return NULL;146}147148PNG_FUNCTION(png_voidp /* PRIVATE */,149png_malloc_array,(png_const_structrp png_ptr, int nelements,150size_t element_size),PNG_ALLOCATED)151{152if (nelements <= 0 || element_size == 0)153png_error(png_ptr, "internal error: array alloc");154155return png_malloc_array_checked(png_ptr, nelements, element_size);156}157158PNG_FUNCTION(png_voidp /* PRIVATE */,159png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,160int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)161{162/* These are internal errors: */163if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||164(old_array == NULL && old_elements > 0))165png_error(png_ptr, "internal error: array realloc");166167/* Check for overflow on the elements count (so the caller does not have to168* check.)169*/170if (add_elements <= INT_MAX - old_elements)171{172png_voidp new_array = png_malloc_array_checked(png_ptr,173old_elements+add_elements, element_size);174175if (new_array != NULL)176{177/* Because png_malloc_array worked the size calculations below cannot178* overflow.179*/180if (old_elements > 0)181memcpy(new_array, old_array, element_size*(unsigned)old_elements);182183memset((char*)new_array + element_size*(unsigned)old_elements, 0,184element_size*(unsigned)add_elements);185186return new_array;187}188}189190return NULL; /* error */191}192#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */193194/* Various functions that have different error handling are derived from this.195* png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate196* function png_malloc_default is also provided.197*/198PNG_FUNCTION(png_voidp,PNGAPI199png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)200{201png_voidp ret;202203if (png_ptr == NULL)204return NULL;205206ret = png_malloc_base(png_ptr, size);207208if (ret == NULL)209png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */210211return ret;212}213214#ifdef PNG_USER_MEM_SUPPORTED215PNG_FUNCTION(png_voidp,PNGAPI216png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),217PNG_ALLOCATED PNG_DEPRECATED)218{219png_voidp ret;220221if (png_ptr == NULL)222return NULL;223224/* Passing 'NULL' here bypasses the application provided memory handler. */225ret = png_malloc_base(NULL/*use malloc*/, size);226227if (ret == NULL)228png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */229230return ret;231}232#endif /* USER_MEM */233234/* This function was added at libpng version 1.2.3. The png_malloc_warn()235* function will issue a png_warning and return NULL instead of issuing a236* png_error, if it fails to allocate the requested memory.237*/238PNG_FUNCTION(png_voidp,PNGAPI239png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),240PNG_ALLOCATED)241{242if (png_ptr != NULL)243{244png_voidp ret = png_malloc_base(png_ptr, size);245246if (ret != NULL)247return ret;248249png_warning(png_ptr, "Out of memory");250}251252return NULL;253}254255/* Free a pointer allocated by png_malloc(). If ptr is NULL, return256* without taking any action.257*/258void PNGAPI259png_free(png_const_structrp png_ptr, png_voidp ptr)260{261if (png_ptr == NULL || ptr == NULL)262return;263264#ifdef PNG_USER_MEM_SUPPORTED265if (png_ptr->free_fn != NULL)266png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);267268else269png_free_default(png_ptr, ptr);270}271272PNG_FUNCTION(void,PNGAPI273png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)274{275if (png_ptr == NULL || ptr == NULL)276return;277#endif /* USER_MEM */278279free(ptr);280}281282#ifdef PNG_USER_MEM_SUPPORTED283/* This function is called when the application wants to use another method284* of allocating and freeing memory.285*/286void PNGAPI287png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr288malloc_fn, png_free_ptr free_fn)289{290if (png_ptr != NULL)291{292png_ptr->mem_ptr = mem_ptr;293png_ptr->malloc_fn = malloc_fn;294png_ptr->free_fn = free_fn;295}296}297298/* This function returns a pointer to the mem_ptr associated with the user299* functions. The application should free any memory associated with this300* pointer before png_write_destroy and png_read_destroy are called.301*/302png_voidp PNGAPI303png_get_mem_ptr(png_const_structrp png_ptr)304{305if (png_ptr == NULL)306return NULL;307308return png_ptr->mem_ptr;309}310#endif /* USER_MEM */311#endif /* READ || WRITE */312313314