istereo

diff libs/libpng/pngmem.c @ 26:862a3329a8f0

wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 06:28:38 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libpng/pngmem.c	Thu Sep 08 06:28:38 2011 +0300
     1.3 @@ -0,0 +1,609 @@
     1.4 +
     1.5 +/* pngmem.c - stub functions for memory allocation
     1.6 + *
     1.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
     1.8 + * For conditions of distribution and use, see copyright notice in png.h
     1.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    1.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.12 + *
    1.13 + * This file provides a location for all memory allocation.  Users who
    1.14 + * need special memory handling are expected to supply replacement
    1.15 + * functions for png_malloc() and png_free(), and to use
    1.16 + * png_create_read_struct_2() and png_create_write_struct_2() to
    1.17 + * identify the replacement functions.
    1.18 + */
    1.19 +
    1.20 +#define PNG_INTERNAL
    1.21 +#include "png.h"
    1.22 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    1.23 +
    1.24 +/* Borland DOS special memory handler */
    1.25 +#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
    1.26 +/* if you change this, be sure to change the one in png.h also */
    1.27 +
    1.28 +/* Allocate memory for a png_struct.  The malloc and memset can be replaced
    1.29 +   by a single call to calloc() if this is thought to improve performance. */
    1.30 +png_voidp /* PRIVATE */
    1.31 +png_create_struct(int type)
    1.32 +{
    1.33 +#ifdef PNG_USER_MEM_SUPPORTED
    1.34 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
    1.35 +}
    1.36 +
    1.37 +/* Alternate version of png_create_struct, for use with user-defined malloc. */
    1.38 +png_voidp /* PRIVATE */
    1.39 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
    1.40 +{
    1.41 +#endif /* PNG_USER_MEM_SUPPORTED */
    1.42 +   png_size_t size;
    1.43 +   png_voidp struct_ptr;
    1.44 +
    1.45 +   if (type == PNG_STRUCT_INFO)
    1.46 +     size = png_sizeof(png_info);
    1.47 +   else if (type == PNG_STRUCT_PNG)
    1.48 +     size = png_sizeof(png_struct);
    1.49 +   else
    1.50 +     return (png_get_copyright(NULL));
    1.51 +
    1.52 +#ifdef PNG_USER_MEM_SUPPORTED
    1.53 +   if (malloc_fn != NULL)
    1.54 +   {
    1.55 +      png_struct dummy_struct;
    1.56 +      png_structp png_ptr = &dummy_struct;
    1.57 +      png_ptr->mem_ptr=mem_ptr;
    1.58 +      struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
    1.59 +   }
    1.60 +   else
    1.61 +#endif /* PNG_USER_MEM_SUPPORTED */
    1.62 +      struct_ptr = (png_voidp)farmalloc(size);
    1.63 +   if (struct_ptr != NULL)
    1.64 +      png_memset(struct_ptr, 0, size);
    1.65 +   return (struct_ptr);
    1.66 +}
    1.67 +
    1.68 +/* Free memory allocated by a png_create_struct() call */
    1.69 +void /* PRIVATE */
    1.70 +png_destroy_struct(png_voidp struct_ptr)
    1.71 +{
    1.72 +#ifdef PNG_USER_MEM_SUPPORTED
    1.73 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
    1.74 +}
    1.75 +
    1.76 +/* Free memory allocated by a png_create_struct() call */
    1.77 +void /* PRIVATE */
    1.78 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    1.79 +    png_voidp mem_ptr)
    1.80 +{
    1.81 +#endif
    1.82 +   if (struct_ptr != NULL)
    1.83 +   {
    1.84 +#ifdef PNG_USER_MEM_SUPPORTED
    1.85 +      if (free_fn != NULL)
    1.86 +      {
    1.87 +         png_struct dummy_struct;
    1.88 +         png_structp png_ptr = &dummy_struct;
    1.89 +         png_ptr->mem_ptr=mem_ptr;
    1.90 +         (*(free_fn))(png_ptr, struct_ptr);
    1.91 +         return;
    1.92 +      }
    1.93 +#endif /* PNG_USER_MEM_SUPPORTED */
    1.94 +      farfree (struct_ptr);
    1.95 +   }
    1.96 +}
    1.97 +
    1.98 +/* Allocate memory.  For reasonable files, size should never exceed
    1.99 + * 64K.  However, zlib may allocate more then 64K if you don't tell
   1.100 + * it not to.  See zconf.h and png.h for more information. zlib does
   1.101 + * need to allocate exactly 64K, so whatever you call here must
   1.102 + * have the ability to do that.
   1.103 + *
   1.104 + * Borland seems to have a problem in DOS mode for exactly 64K.
   1.105 + * It gives you a segment with an offset of 8 (perhaps to store its
   1.106 + * memory stuff).  zlib doesn't like this at all, so we have to
   1.107 + * detect and deal with it.  This code should not be needed in
   1.108 + * Windows or OS/2 modes, and only in 16 bit mode.  This code has
   1.109 + * been updated by Alexander Lehmann for version 0.89 to waste less
   1.110 + * memory.
   1.111 + *
   1.112 + * Note that we can't use png_size_t for the "size" declaration,
   1.113 + * since on some systems a png_size_t is a 16-bit quantity, and as a
   1.114 + * result, we would be truncating potentially larger memory requests
   1.115 + * (which should cause a fatal error) and introducing major problems.
   1.116 + */
   1.117 +
   1.118 +png_voidp PNGAPI
   1.119 +png_malloc(png_structp png_ptr, png_uint_32 size)
   1.120 +{
   1.121 +   png_voidp ret;
   1.122 +
   1.123 +   if (png_ptr == NULL || size == 0)
   1.124 +      return (NULL);
   1.125 +
   1.126 +#ifdef PNG_USER_MEM_SUPPORTED
   1.127 +   if (png_ptr->malloc_fn != NULL)
   1.128 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   1.129 +   else
   1.130 +       ret = (png_malloc_default(png_ptr, size));
   1.131 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.132 +       png_error(png_ptr, "Out of memory!");
   1.133 +   return (ret);
   1.134 +}
   1.135 +
   1.136 +png_voidp PNGAPI
   1.137 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
   1.138 +{
   1.139 +   png_voidp ret;
   1.140 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.141 +
   1.142 +   if (png_ptr == NULL || size == 0)
   1.143 +      return (NULL);
   1.144 +
   1.145 +#ifdef PNG_MAX_MALLOC_64K
   1.146 +   if (size > (png_uint_32)65536L)
   1.147 +   {
   1.148 +      png_warning(png_ptr, "Cannot Allocate > 64K");
   1.149 +      ret = NULL;
   1.150 +   }
   1.151 +   else
   1.152 +#endif
   1.153 +
   1.154 +   if (size != (size_t)size)
   1.155 +     ret = NULL;
   1.156 +   else if (size == (png_uint_32)65536L)
   1.157 +   {
   1.158 +      if (png_ptr->offset_table == NULL)
   1.159 +      {
   1.160 +         /* try to see if we need to do any of this fancy stuff */
   1.161 +         ret = farmalloc(size);
   1.162 +         if (ret == NULL || ((png_size_t)ret & 0xffff))
   1.163 +         {
   1.164 +            int num_blocks;
   1.165 +            png_uint_32 total_size;
   1.166 +            png_bytep table;
   1.167 +            int i;
   1.168 +            png_byte huge * hptr;
   1.169 +
   1.170 +            if (ret != NULL)
   1.171 +            {
   1.172 +               farfree(ret);
   1.173 +               ret = NULL;
   1.174 +            }
   1.175 +
   1.176 +            if (png_ptr->zlib_window_bits > 14)
   1.177 +               num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
   1.178 +            else
   1.179 +               num_blocks = 1;
   1.180 +            if (png_ptr->zlib_mem_level >= 7)
   1.181 +               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
   1.182 +            else
   1.183 +               num_blocks++;
   1.184 +
   1.185 +            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
   1.186 +
   1.187 +            table = farmalloc(total_size);
   1.188 +
   1.189 +            if (table == NULL)
   1.190 +            {
   1.191 +#ifndef PNG_USER_MEM_SUPPORTED
   1.192 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.193 +                  png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
   1.194 +               else
   1.195 +                  png_warning(png_ptr, "Out Of Memory.");
   1.196 +#endif
   1.197 +               return (NULL);
   1.198 +            }
   1.199 +
   1.200 +            if ((png_size_t)table & 0xfff0)
   1.201 +            {
   1.202 +#ifndef PNG_USER_MEM_SUPPORTED
   1.203 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.204 +                  png_error(png_ptr,
   1.205 +                    "Farmalloc didn't return normalized pointer");
   1.206 +               else
   1.207 +                  png_warning(png_ptr,
   1.208 +                    "Farmalloc didn't return normalized pointer");
   1.209 +#endif
   1.210 +               return (NULL);
   1.211 +            }
   1.212 +
   1.213 +            png_ptr->offset_table = table;
   1.214 +            png_ptr->offset_table_ptr = farmalloc(num_blocks *
   1.215 +               png_sizeof(png_bytep));
   1.216 +
   1.217 +            if (png_ptr->offset_table_ptr == NULL)
   1.218 +            {
   1.219 +#ifndef PNG_USER_MEM_SUPPORTED
   1.220 +               if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.221 +                  png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
   1.222 +               else
   1.223 +                  png_warning(png_ptr, "Out Of memory.");
   1.224 +#endif
   1.225 +               return (NULL);
   1.226 +            }
   1.227 +
   1.228 +            hptr = (png_byte huge *)table;
   1.229 +            if ((png_size_t)hptr & 0xf)
   1.230 +            {
   1.231 +               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
   1.232 +               hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
   1.233 +            }
   1.234 +            for (i = 0; i < num_blocks; i++)
   1.235 +            {
   1.236 +               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
   1.237 +               hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
   1.238 +            }
   1.239 +
   1.240 +            png_ptr->offset_table_number = num_blocks;
   1.241 +            png_ptr->offset_table_count = 0;
   1.242 +            png_ptr->offset_table_count_free = 0;
   1.243 +         }
   1.244 +      }
   1.245 +
   1.246 +      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
   1.247 +      {
   1.248 +#ifndef PNG_USER_MEM_SUPPORTED
   1.249 +         if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.250 +            png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
   1.251 +         else
   1.252 +            png_warning(png_ptr, "Out of Memory.");
   1.253 +#endif
   1.254 +         return (NULL);
   1.255 +      }
   1.256 +
   1.257 +      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
   1.258 +   }
   1.259 +   else
   1.260 +      ret = farmalloc(size);
   1.261 +
   1.262 +#ifndef PNG_USER_MEM_SUPPORTED
   1.263 +   if (ret == NULL)
   1.264 +   {
   1.265 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.266 +         png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
   1.267 +      else
   1.268 +         png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
   1.269 +   }
   1.270 +#endif
   1.271 +
   1.272 +   return (ret);
   1.273 +}
   1.274 +
   1.275 +/* free a pointer allocated by png_malloc().  In the default
   1.276 +   configuration, png_ptr is not used, but is passed in case it
   1.277 +   is needed.  If ptr is NULL, return without taking any action. */
   1.278 +
   1.279 +void PNGAPI
   1.280 +png_free(png_structp png_ptr, png_voidp ptr)
   1.281 +{
   1.282 +   if (png_ptr == NULL || ptr == NULL)
   1.283 +      return;
   1.284 +
   1.285 +#ifdef PNG_USER_MEM_SUPPORTED
   1.286 +   if (png_ptr->free_fn != NULL)
   1.287 +   {
   1.288 +      (*(png_ptr->free_fn))(png_ptr, ptr);
   1.289 +      return;
   1.290 +   }
   1.291 +   else png_free_default(png_ptr, ptr);
   1.292 +}
   1.293 +
   1.294 +void PNGAPI
   1.295 +png_free_default(png_structp png_ptr, png_voidp ptr)
   1.296 +{
   1.297 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.298 +
   1.299 +   if (png_ptr == NULL || ptr == NULL) return;
   1.300 +
   1.301 +   if (png_ptr->offset_table != NULL)
   1.302 +   {
   1.303 +      int i;
   1.304 +
   1.305 +      for (i = 0; i < png_ptr->offset_table_count; i++)
   1.306 +      {
   1.307 +         if (ptr == png_ptr->offset_table_ptr[i])
   1.308 +         {
   1.309 +            ptr = NULL;
   1.310 +            png_ptr->offset_table_count_free++;
   1.311 +            break;
   1.312 +         }
   1.313 +      }
   1.314 +      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
   1.315 +      {
   1.316 +         farfree(png_ptr->offset_table);
   1.317 +         farfree(png_ptr->offset_table_ptr);
   1.318 +         png_ptr->offset_table = NULL;
   1.319 +         png_ptr->offset_table_ptr = NULL;
   1.320 +      }
   1.321 +   }
   1.322 +
   1.323 +   if (ptr != NULL)
   1.324 +   {
   1.325 +      farfree(ptr);
   1.326 +   }
   1.327 +}
   1.328 +
   1.329 +#else /* Not the Borland DOS special memory handler */
   1.330 +
   1.331 +/* Allocate memory for a png_struct or a png_info.  The malloc and
   1.332 +   memset can be replaced by a single call to calloc() if this is thought
   1.333 +   to improve performance noticably. */
   1.334 +png_voidp /* PRIVATE */
   1.335 +png_create_struct(int type)
   1.336 +{
   1.337 +#ifdef PNG_USER_MEM_SUPPORTED
   1.338 +   return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
   1.339 +}
   1.340 +
   1.341 +/* Allocate memory for a png_struct or a png_info.  The malloc and
   1.342 +   memset can be replaced by a single call to calloc() if this is thought
   1.343 +   to improve performance noticably. */
   1.344 +png_voidp /* PRIVATE */
   1.345 +png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
   1.346 +{
   1.347 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.348 +   png_size_t size;
   1.349 +   png_voidp struct_ptr;
   1.350 +
   1.351 +   if (type == PNG_STRUCT_INFO)
   1.352 +      size = png_sizeof(png_info);
   1.353 +   else if (type == PNG_STRUCT_PNG)
   1.354 +      size = png_sizeof(png_struct);
   1.355 +   else
   1.356 +      return (NULL);
   1.357 +
   1.358 +#ifdef PNG_USER_MEM_SUPPORTED
   1.359 +   if (malloc_fn != NULL)
   1.360 +   {
   1.361 +      png_struct dummy_struct;
   1.362 +      png_structp png_ptr = &dummy_struct;
   1.363 +      png_ptr->mem_ptr=mem_ptr;
   1.364 +      struct_ptr = (*(malloc_fn))(png_ptr, size);
   1.365 +      if (struct_ptr != NULL)
   1.366 +         png_memset(struct_ptr, 0, size);
   1.367 +      return (struct_ptr);
   1.368 +   }
   1.369 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.370 +
   1.371 +#if defined(__TURBOC__) && !defined(__FLAT__)
   1.372 +   struct_ptr = (png_voidp)farmalloc(size);
   1.373 +#else
   1.374 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
   1.375 +   struct_ptr = (png_voidp)halloc(size, 1);
   1.376 +# else
   1.377 +   struct_ptr = (png_voidp)malloc(size);
   1.378 +# endif
   1.379 +#endif
   1.380 +   if (struct_ptr != NULL)
   1.381 +      png_memset(struct_ptr, 0, size);
   1.382 +
   1.383 +   return (struct_ptr);
   1.384 +}
   1.385 +
   1.386 +
   1.387 +/* Free memory allocated by a png_create_struct() call */
   1.388 +void /* PRIVATE */
   1.389 +png_destroy_struct(png_voidp struct_ptr)
   1.390 +{
   1.391 +#ifdef PNG_USER_MEM_SUPPORTED
   1.392 +   png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
   1.393 +}
   1.394 +
   1.395 +/* Free memory allocated by a png_create_struct() call */
   1.396 +void /* PRIVATE */
   1.397 +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   1.398 +    png_voidp mem_ptr)
   1.399 +{
   1.400 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.401 +   if (struct_ptr != NULL)
   1.402 +   {
   1.403 +#ifdef PNG_USER_MEM_SUPPORTED
   1.404 +      if (free_fn != NULL)
   1.405 +      {
   1.406 +         png_struct dummy_struct;
   1.407 +         png_structp png_ptr = &dummy_struct;
   1.408 +         png_ptr->mem_ptr=mem_ptr;
   1.409 +         (*(free_fn))(png_ptr, struct_ptr);
   1.410 +         return;
   1.411 +      }
   1.412 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.413 +#if defined(__TURBOC__) && !defined(__FLAT__)
   1.414 +      farfree(struct_ptr);
   1.415 +#else
   1.416 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
   1.417 +      hfree(struct_ptr);
   1.418 +# else
   1.419 +      free(struct_ptr);
   1.420 +# endif
   1.421 +#endif
   1.422 +   }
   1.423 +}
   1.424 +
   1.425 +/* Allocate memory.  For reasonable files, size should never exceed
   1.426 +   64K.  However, zlib may allocate more then 64K if you don't tell
   1.427 +   it not to.  See zconf.h and png.h for more information.  zlib does
   1.428 +   need to allocate exactly 64K, so whatever you call here must
   1.429 +   have the ability to do that. */
   1.430 +
   1.431 +png_voidp PNGAPI
   1.432 +png_malloc(png_structp png_ptr, png_uint_32 size)
   1.433 +{
   1.434 +   png_voidp ret;
   1.435 +
   1.436 +#ifdef PNG_USER_MEM_SUPPORTED
   1.437 +   if (png_ptr == NULL || size == 0)
   1.438 +      return (NULL);
   1.439 +
   1.440 +   if (png_ptr->malloc_fn != NULL)
   1.441 +       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   1.442 +   else
   1.443 +       ret = (png_malloc_default(png_ptr, size));
   1.444 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.445 +       png_error(png_ptr, "Out of Memory!");
   1.446 +   return (ret);
   1.447 +}
   1.448 +
   1.449 +png_voidp PNGAPI
   1.450 +png_malloc_default(png_structp png_ptr, png_uint_32 size)
   1.451 +{
   1.452 +   png_voidp ret;
   1.453 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.454 +
   1.455 +   if (png_ptr == NULL || size == 0)
   1.456 +      return (NULL);
   1.457 +
   1.458 +#ifdef PNG_MAX_MALLOC_64K
   1.459 +   if (size > (png_uint_32)65536L)
   1.460 +   {
   1.461 +#ifndef PNG_USER_MEM_SUPPORTED
   1.462 +      if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.463 +         png_error(png_ptr, "Cannot Allocate > 64K");
   1.464 +      else
   1.465 +#endif
   1.466 +         return NULL;
   1.467 +   }
   1.468 +#endif
   1.469 +
   1.470 + /* Check for overflow */
   1.471 +#if defined(__TURBOC__) && !defined(__FLAT__)
   1.472 + if (size != (unsigned long)size)
   1.473 +   ret = NULL;
   1.474 + else
   1.475 +   ret = farmalloc(size);
   1.476 +#else
   1.477 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
   1.478 + if (size != (unsigned long)size)
   1.479 +   ret = NULL;
   1.480 + else
   1.481 +   ret = halloc(size, 1);
   1.482 +# else
   1.483 + if (size != (size_t)size)
   1.484 +   ret = NULL;
   1.485 + else
   1.486 +   ret = malloc((size_t)size);
   1.487 +# endif
   1.488 +#endif
   1.489 +
   1.490 +#ifndef PNG_USER_MEM_SUPPORTED
   1.491 +   if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   1.492 +      png_error(png_ptr, "Out of Memory");
   1.493 +#endif
   1.494 +
   1.495 +   return (ret);
   1.496 +}
   1.497 +
   1.498 +/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
   1.499 +   without taking any action. */
   1.500 +void PNGAPI
   1.501 +png_free(png_structp png_ptr, png_voidp ptr)
   1.502 +{
   1.503 +   if (png_ptr == NULL || ptr == NULL)
   1.504 +      return;
   1.505 +
   1.506 +#ifdef PNG_USER_MEM_SUPPORTED
   1.507 +   if (png_ptr->free_fn != NULL)
   1.508 +   {
   1.509 +      (*(png_ptr->free_fn))(png_ptr, ptr);
   1.510 +      return;
   1.511 +   }
   1.512 +   else png_free_default(png_ptr, ptr);
   1.513 +}
   1.514 +void PNGAPI
   1.515 +png_free_default(png_structp png_ptr, png_voidp ptr)
   1.516 +{
   1.517 +   if (png_ptr == NULL || ptr == NULL)
   1.518 +      return;
   1.519 +
   1.520 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.521 +
   1.522 +#if defined(__TURBOC__) && !defined(__FLAT__)
   1.523 +   farfree(ptr);
   1.524 +#else
   1.525 +# if defined(_MSC_VER) && defined(MAXSEG_64K)
   1.526 +   hfree(ptr);
   1.527 +# else
   1.528 +   free(ptr);
   1.529 +# endif
   1.530 +#endif
   1.531 +}
   1.532 +
   1.533 +#endif /* Not Borland DOS special memory handler */
   1.534 +
   1.535 +#if defined(PNG_1_0_X)
   1.536 +#  define png_malloc_warn png_malloc
   1.537 +#else
   1.538 +/* This function was added at libpng version 1.2.3.  The png_malloc_warn()
   1.539 + * function will set up png_malloc() to issue a png_warning and return NULL
   1.540 + * instead of issuing a png_error, if it fails to allocate the requested
   1.541 + * memory.
   1.542 + */
   1.543 +png_voidp PNGAPI
   1.544 +png_malloc_warn(png_structp png_ptr, png_uint_32 size)
   1.545 +{
   1.546 +   png_voidp ptr;
   1.547 +   png_uint_32 save_flags;
   1.548 +   if (png_ptr == NULL) return (NULL);
   1.549 +
   1.550 +   save_flags = png_ptr->flags;
   1.551 +   png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   1.552 +   ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
   1.553 +   png_ptr->flags=save_flags;
   1.554 +   return(ptr);
   1.555 +}
   1.556 +#endif
   1.557 +
   1.558 +png_voidp PNGAPI
   1.559 +png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
   1.560 +   png_uint_32 length)
   1.561 +{
   1.562 +   png_size_t size;
   1.563 +
   1.564 +   size = (png_size_t)length;
   1.565 +   if ((png_uint_32)size != length)
   1.566 +      png_error(png_ptr, "Overflow in png_memcpy_check.");
   1.567 +
   1.568 +   return(png_memcpy (s1, s2, size));
   1.569 +}
   1.570 +
   1.571 +png_voidp PNGAPI
   1.572 +png_memset_check (png_structp png_ptr, png_voidp s1, int value,
   1.573 +   png_uint_32 length)
   1.574 +{
   1.575 +   png_size_t size;
   1.576 +
   1.577 +   size = (png_size_t)length;
   1.578 +   if ((png_uint_32)size != length)
   1.579 +      png_error(png_ptr, "Overflow in png_memset_check.");
   1.580 +
   1.581 +   return (png_memset (s1, value, size));
   1.582 +
   1.583 +}
   1.584 +
   1.585 +#ifdef PNG_USER_MEM_SUPPORTED
   1.586 +/* This function is called when the application wants to use another method
   1.587 + * of allocating and freeing memory.
   1.588 + */
   1.589 +void PNGAPI
   1.590 +png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
   1.591 +  malloc_fn, png_free_ptr free_fn)
   1.592 +{
   1.593 +   if (png_ptr != NULL)
   1.594 +   {
   1.595 +      png_ptr->mem_ptr = mem_ptr;
   1.596 +      png_ptr->malloc_fn = malloc_fn;
   1.597 +      png_ptr->free_fn = free_fn;
   1.598 +   }
   1.599 +}
   1.600 +
   1.601 +/* This function returns a pointer to the mem_ptr associated with the user
   1.602 + * functions.  The application should free any memory associated with this
   1.603 + * pointer before png_write_destroy and png_read_destroy are called.
   1.604 + */
   1.605 +png_voidp PNGAPI
   1.606 +png_get_mem_ptr(png_structp png_ptr)
   1.607 +{
   1.608 +   if (png_ptr == NULL) return (NULL);
   1.609 +   return ((png_voidp)png_ptr->mem_ptr);
   1.610 +}
   1.611 +#endif /* PNG_USER_MEM_SUPPORTED */
   1.612 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */