fukasawa e60969
fukasawa e60969
/* pngmem.c - stub functions for memory allocation
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.15 [November 20, 2014]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2014 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * This file provides a location for all memory allocation.  Users who
fukasawa e60969
 * need special memory handling are expected to supply replacement
fukasawa e60969
 * functions for png_malloc() and png_free(), and to use
fukasawa e60969
 * png_create_read_struct_2() and png_create_write_struct_2() to
fukasawa e60969
 * identify the replacement functions.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include "pngpriv.h"
fukasawa e60969
fukasawa e60969
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
fukasawa e60969
/* Free a png_struct */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_destroy_png_struct(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      /* png_free might call png_error and may certainly call
fukasawa e60969
       * png_get_mem_ptr, so fake a temporary png_struct to support this.
fukasawa e60969
       */
fukasawa e60969
      png_struct dummy_struct = *png_ptr;
fukasawa e60969
      memset(png_ptr, 0, (sizeof *png_ptr));
fukasawa e60969
      png_free(&dummy_struct, png_ptr);
fukasawa e60969
fukasawa e60969
#     ifdef PNG_SETJMP_SUPPORTED
fukasawa e60969
         /* We may have a jmp_buf left to deallocate. */
fukasawa e60969
         png_free_jmpbuf(&dummy_struct);
fukasawa e60969
#     endif
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Allocate memory.  For reasonable files, size should never exceed
fukasawa e60969
 * 64K.  However, zlib may allocate more than 64K if you don't tell
fukasawa e60969
 * it not to.  See zconf.h and png.h for more information.  zlib does
fukasawa e60969
 * need to allocate exactly 64K, so whatever you call here must
fukasawa e60969
 * have the ability to do that.
fukasawa e60969
 */
fukasawa e60969
PNG_FUNCTION(png_voidp,PNGAPI
fukasawa e60969
png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   png_voidp ret;
fukasawa e60969
fukasawa e60969
   ret = png_malloc(png_ptr, size);
fukasawa e60969
fukasawa e60969
   if (ret != NULL)
fukasawa e60969
      memset(ret, 0, size);
fukasawa e60969
fukasawa e60969
   return ret;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
fukasawa e60969
 * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
fukasawa e60969
 * Checking and error handling must happen outside this routine; it returns NULL
fukasawa e60969
 * if the allocation cannot be done (for any reason.)
fukasawa e60969
 */
fukasawa e60969
PNG_FUNCTION(png_voidp /* PRIVATE */,
fukasawa e60969
png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size),
fukasawa e60969
   PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
fukasawa e60969
    * allocators have also been removed in 1.6.0, so any 16-bit system now has
fukasawa e60969
    * to implement a user memory handler.  This checks to be sure it isn't
fukasawa e60969
    * called with big numbers.
fukasawa e60969
    */
fukasawa e60969
#ifndef PNG_USER_MEM_SUPPORTED
fukasawa e60969
   PNG_UNUSED(png_ptr)
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* Some compilers complain that this is always true.  However, it
fukasawa e60969
    * can be false when integer overflow happens.
fukasawa e60969
    */
fukasawa e60969
   if (size > 0 && size <= PNG_SIZE_MAX
fukasawa e60969
#     ifdef PNG_MAX_MALLOC_64K
fukasawa e60969
         && size <= 65536U
fukasawa e60969
#     endif
fukasawa e60969
      )
fukasawa e60969
   {
fukasawa e60969
#ifdef PNG_USER_MEM_SUPPORTED
fukasawa e60969
      if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
fukasawa e60969
         return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
#endif
fukasawa e60969
         return malloc((size_t)size); /* checked for truncation above */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
fukasawa e60969
   defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
fukasawa e60969
/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
fukasawa e60969
 * that arises because of the checks in png_realloc_array that are repeated in
fukasawa e60969
 * png_malloc_array.
fukasawa e60969
 */
fukasawa e60969
static png_voidp
fukasawa e60969
png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
fukasawa e60969
   size_t element_size)
fukasawa e60969
{
fukasawa e60969
   png_alloc_size_t req = nelements; /* known to be > 0 */
fukasawa e60969
fukasawa e60969
   if (req <= PNG_SIZE_MAX/element_size)
fukasawa e60969
      return png_malloc_base(png_ptr, req * element_size);
fukasawa e60969
fukasawa e60969
   /* The failure case when the request is too large */
fukasawa e60969
   return NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
PNG_FUNCTION(png_voidp /* PRIVATE */,
fukasawa e60969
png_malloc_array,(png_const_structrp png_ptr, int nelements,
fukasawa e60969
   size_t element_size),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   if (nelements <= 0 || element_size == 0)
fukasawa e60969
      png_error(png_ptr, "internal error: array alloc");
fukasawa e60969
fukasawa e60969
   return png_malloc_array_checked(png_ptr, nelements, element_size);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
PNG_FUNCTION(png_voidp /* PRIVATE */,
fukasawa e60969
png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
fukasawa e60969
   int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   /* These are internal errors: */
fukasawa e60969
   if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
fukasawa e60969
      (old_array == NULL && old_elements > 0))
fukasawa e60969
      png_error(png_ptr, "internal error: array realloc");
fukasawa e60969
fukasawa e60969
   /* Check for overflow on the elements count (so the caller does not have to
fukasawa e60969
    * check.)
fukasawa e60969
    */
fukasawa e60969
   if (add_elements <= INT_MAX - old_elements)
fukasawa e60969
   {
fukasawa e60969
      png_voidp new_array = png_malloc_array_checked(png_ptr,
fukasawa e60969
         old_elements+add_elements, element_size);
fukasawa e60969
fukasawa e60969
      if (new_array != NULL)
fukasawa e60969
      {
fukasawa e60969
         /* Because png_malloc_array worked the size calculations below cannot
fukasawa e60969
          * overflow.
fukasawa e60969
          */
fukasawa e60969
         if (old_elements > 0)
fukasawa e60969
            memcpy(new_array, old_array, element_size*(unsigned)old_elements);
fukasawa e60969
fukasawa e60969
         memset((char*)new_array + element_size*(unsigned)old_elements, 0,
fukasawa e60969
            element_size*(unsigned)add_elements);
fukasawa e60969
fukasawa e60969
         return new_array;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return NULL; /* error */
fukasawa e60969
}
fukasawa e60969
#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
fukasawa e60969
fukasawa e60969
/* Various functions that have different error handling are derived from this.
fukasawa e60969
 * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
fukasawa e60969
 * function png_malloc_default is also provided.
fukasawa e60969
 */
fukasawa e60969
PNG_FUNCTION(png_voidp,PNGAPI
fukasawa e60969
png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   png_voidp ret;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   ret = png_malloc_base(png_ptr, size);
fukasawa e60969
fukasawa e60969
   if (ret == NULL)
fukasawa e60969
       png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
fukasawa e60969
fukasawa e60969
   return ret;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_USER_MEM_SUPPORTED
fukasawa e60969
PNG_FUNCTION(png_voidp,PNGAPI
fukasawa e60969
png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
fukasawa e60969
   PNG_ALLOCATED PNG_DEPRECATED)
fukasawa e60969
{
fukasawa e60969
   png_voidp ret;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   /* Passing 'NULL' here bypasses the application provided memory handler. */
fukasawa e60969
   ret = png_malloc_base(NULL/*use malloc*/, size);
fukasawa e60969
fukasawa e60969
   if (ret == NULL)
fukasawa e60969
      png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
fukasawa e60969
fukasawa e60969
   return ret;
fukasawa e60969
}
fukasawa e60969
#endif /* USER_MEM */
fukasawa e60969
fukasawa e60969
/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
fukasawa e60969
 * function will issue a png_warning and return NULL instead of issuing a
fukasawa e60969
 * png_error, if it fails to allocate the requested memory.
fukasawa e60969
 */
fukasawa e60969
PNG_FUNCTION(png_voidp,PNGAPI
fukasawa e60969
png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
fukasawa e60969
   PNG_ALLOCATED)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_voidp ret = png_malloc_base(png_ptr, size);
fukasawa e60969
fukasawa e60969
      if (ret != NULL)
fukasawa e60969
         return ret;
fukasawa e60969
fukasawa e60969
      png_warning(png_ptr, "Out of memory");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return NULL;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
fukasawa e60969
 * without taking any action.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_free(png_const_structrp png_ptr, png_voidp ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_USER_MEM_SUPPORTED
fukasawa e60969
   if (png_ptr->free_fn != NULL)
fukasawa e60969
      png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      png_free_default(png_ptr, ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
PNG_FUNCTION(void,PNGAPI
fukasawa e60969
png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
#endif /* USER_MEM */
fukasawa e60969
fukasawa e60969
   free(ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_USER_MEM_SUPPORTED
fukasawa e60969
/* This function is called when the application wants to use another method
fukasawa e60969
 * of allocating and freeing memory.
fukasawa e60969
 */
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
fukasawa e60969
  malloc_fn, png_free_ptr free_fn)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->mem_ptr = mem_ptr;
fukasawa e60969
      png_ptr->malloc_fn = malloc_fn;
fukasawa e60969
      png_ptr->free_fn = free_fn;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* This function returns a pointer to the mem_ptr associated with the user
fukasawa e60969
 * functions.  The application should free any memory associated with this
fukasawa e60969
 * pointer before png_write_destroy and png_read_destroy are called.
fukasawa e60969
 */
fukasawa e60969
png_voidp PNGAPI
fukasawa e60969
png_get_mem_ptr(png_const_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return NULL;
fukasawa e60969
fukasawa e60969
   return png_ptr->mem_ptr;
fukasawa e60969
}
fukasawa e60969
#endif /* USER_MEM */
fukasawa e60969
#endif /* READ || WRITE */