src/3rdparty/libpng/pngmem.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     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.37 [June 4, 2009]
     4  * Last changed in libpng 1.4.0 [January 3, 2010]
     5  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
     5  * Copyright (c) 1998-2010 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
    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 #define PNG_NO_PEDANTIC_WARNINGS
    21 #include "png.h"
    21 #include "png.h"
    22 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    22 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
       
    23 #include "pngpriv.h"
    23 
    24 
    24 /* Borland DOS special memory handler */
    25 /* Borland DOS special memory handler */
    25 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
    26 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
    26 /* If you change this, be sure to change the one in png.h also */
    27 /* If you change this, be sure to change the one in png.h also */
    27 
    28 
    29    by a single call to calloc() if this is thought to improve performance. */
    30    by a single call to calloc() if this is thought to improve performance. */
    30 png_voidp /* PRIVATE */
    31 png_voidp /* PRIVATE */
    31 png_create_struct(int type)
    32 png_create_struct(int type)
    32 {
    33 {
    33 #ifdef PNG_USER_MEM_SUPPORTED
    34 #ifdef PNG_USER_MEM_SUPPORTED
    34    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
    35    return (png_create_struct_2(type, NULL, NULL));
    35 }
    36 }
    36 
    37 
    37 /* Alternate version of png_create_struct, for use with user-defined malloc. */
    38 /* Alternate version of png_create_struct, for use with user-defined malloc. */
    38 png_voidp /* PRIVATE */
    39 png_voidp /* PRIVATE */
    39 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
    40 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
    68 /* Free memory allocated by a png_create_struct() call */
    69 /* Free memory allocated by a png_create_struct() call */
    69 void /* PRIVATE */
    70 void /* PRIVATE */
    70 png_destroy_struct(png_voidp struct_ptr)
    71 png_destroy_struct(png_voidp struct_ptr)
    71 {
    72 {
    72 #ifdef PNG_USER_MEM_SUPPORTED
    73 #ifdef PNG_USER_MEM_SUPPORTED
    73    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
    74    png_destroy_struct_2(struct_ptr, NULL, NULL);
    74 }
    75 }
    75 
    76 
    76 /* Free memory allocated by a png_create_struct() call */
    77 /* Free memory allocated by a png_create_struct() call */
    77 void /* PRIVATE */
    78 void /* PRIVATE */
    78 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
    79 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   112  * Note that we can't use png_size_t for the "size" declaration,
   113  * Note that we can't use png_size_t for the "size" declaration,
   113  * since on some systems a png_size_t is a 16-bit quantity, and as a
   114  * since on some systems a png_size_t is a 16-bit quantity, and as a
   114  * result, we would be truncating potentially larger memory requests
   115  * result, we would be truncating potentially larger memory requests
   115  * (which should cause a fatal error) and introducing major problems.
   116  * (which should cause a fatal error) and introducing major problems.
   116  */
   117  */
   117 
   118 png_voidp PNGAPI
   118 png_voidp PNGAPI
   119 png_calloc(png_structp png_ptr, png_alloc_size_t size)
   119 png_malloc(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_alloc_size_t size)
   120 {
   131 {
   121    png_voidp ret;
   132    png_voidp ret;
   122 
   133 
   123    if (png_ptr == NULL || size == 0)
   134    if (png_ptr == NULL || size == 0)
   124       return (NULL);
   135       return (NULL);
   127    if (png_ptr->malloc_fn != NULL)
   138    if (png_ptr->malloc_fn != NULL)
   128       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   139       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   129    else
   140    else
   130       ret = (png_malloc_default(png_ptr, size));
   141       ret = (png_malloc_default(png_ptr, size));
   131    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   142    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   132        png_error(png_ptr, "Out of memory!");
   143        png_error(png_ptr, "Out of memory");
   133    return (ret);
   144    return (ret);
   134 }
   145 }
   135 
   146 
   136 png_voidp PNGAPI
   147 png_voidp PNGAPI
   137 png_malloc_default(png_structp png_ptr, png_uint_32 size)
   148 png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
   138 {
   149 {
   139    png_voidp ret;
   150    png_voidp ret;
   140 #endif /* PNG_USER_MEM_SUPPORTED */
   151 #endif /* PNG_USER_MEM_SUPPORTED */
   141 
   152 
   142    if (png_ptr == NULL || size == 0)
   153    if (png_ptr == NULL || size == 0)
   188 
   199 
   189             if (table == NULL)
   200             if (table == NULL)
   190             {
   201             {
   191 #ifndef PNG_USER_MEM_SUPPORTED
   202 #ifndef PNG_USER_MEM_SUPPORTED
   192                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   203                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   193                   png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
   204                   png_error(png_ptr, "Out Of Memory"); /* Note "O" and "M" */
   194                else
   205                else
   195                   png_warning(png_ptr, "Out Of Memory.");
   206                   png_warning(png_ptr, "Out Of Memory");
   196 #endif
   207 #endif
   197                return (NULL);
   208                return (NULL);
   198             }
   209             }
   199 
   210 
   200             if ((png_size_t)table & 0xfff0)
   211             if ((png_size_t)table & 0xfff0)
   216 
   227 
   217             if (png_ptr->offset_table_ptr == NULL)
   228             if (png_ptr->offset_table_ptr == NULL)
   218             {
   229             {
   219 #ifndef PNG_USER_MEM_SUPPORTED
   230 #ifndef PNG_USER_MEM_SUPPORTED
   220                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   231                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   221                   png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
   232                   png_error(png_ptr, "Out Of memory"); /* Note "O" and "M" */
   222                else
   233                else
   223                   png_warning(png_ptr, "Out Of memory.");
   234                   png_warning(png_ptr, "Out Of memory");
   224 #endif
   235 #endif
   225                return (NULL);
   236                return (NULL);
   226             }
   237             }
   227 
   238 
   228             hptr = (png_byte huge *)table;
   239             hptr = (png_byte huge *)table;
   245 
   256 
   246       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
   257       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
   247       {
   258       {
   248 #ifndef PNG_USER_MEM_SUPPORTED
   259 #ifndef PNG_USER_MEM_SUPPORTED
   249          if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   260          if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   250             png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
   261             png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
   251          else
   262          else
   252             png_warning(png_ptr, "Out of Memory.");
   263             png_warning(png_ptr, "Out of Memory");
   253 #endif
   264 #endif
   254          return (NULL);
   265          return (NULL);
   255       }
   266       }
   256 
   267 
   257       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
   268       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
   261 
   272 
   262 #ifndef PNG_USER_MEM_SUPPORTED
   273 #ifndef PNG_USER_MEM_SUPPORTED
   263    if (ret == NULL)
   274    if (ret == NULL)
   264    {
   275    {
   265       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   276       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   266          png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
   277          png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
   267       else
   278       else
   268          png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
   279          png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
   269    }
   280    }
   270 #endif
   281 #endif
   271 
   282 
   272    return (ret);
   283    return (ret);
   273 }
   284 }
   335    to improve performance noticably. */
   346    to improve performance noticably. */
   336 png_voidp /* PRIVATE */
   347 png_voidp /* PRIVATE */
   337 png_create_struct(int type)
   348 png_create_struct(int type)
   338 {
   349 {
   339 #ifdef PNG_USER_MEM_SUPPORTED
   350 #ifdef PNG_USER_MEM_SUPPORTED
   340    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
   351    return (png_create_struct_2(type, NULL, NULL));
   341 }
   352 }
   342 
   353 
   343 /* Allocate memory for a png_struct or a png_info.  The malloc and
   354 /* Allocate memory for a png_struct or a png_info.  The malloc and
   344    memset can be replaced by a single call to calloc() if this is thought
   355    memset can be replaced by a single call to calloc() if this is thought
   345    to improve performance noticably. */
   356    to improve performance noticably. */
   389 /* Free memory allocated by a png_create_struct() call */
   400 /* Free memory allocated by a png_create_struct() call */
   390 void /* PRIVATE */
   401 void /* PRIVATE */
   391 png_destroy_struct(png_voidp struct_ptr)
   402 png_destroy_struct(png_voidp struct_ptr)
   392 {
   403 {
   393 #ifdef PNG_USER_MEM_SUPPORTED
   404 #ifdef PNG_USER_MEM_SUPPORTED
   394    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
   405    png_destroy_struct_2(struct_ptr, NULL, NULL);
   395 }
   406 }
   396 
   407 
   397 /* Free memory allocated by a png_create_struct() call */
   408 /* Free memory allocated by a png_create_struct() call */
   398 void /* PRIVATE */
   409 void /* PRIVATE */
   399 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   410 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
   429  * it not to.  See zconf.h and png.h for more information.  zlib does
   440  * it not to.  See zconf.h and png.h for more information.  zlib does
   430  * need to allocate exactly 64K, so whatever you call here must
   441  * need to allocate exactly 64K, so whatever you call here must
   431  * have the ability to do that.
   442  * have the ability to do that.
   432  */
   443  */
   433 
   444 
   434 
   445 png_voidp PNGAPI
   435 png_voidp PNGAPI
   446 png_calloc(png_structp png_ptr, png_alloc_size_t size)
   436 png_malloc(png_structp png_ptr, png_uint_32 size)
   447 {
       
   448    png_voidp ret;
       
   449 
       
   450    ret = (png_malloc(png_ptr, size));
       
   451    if (ret != NULL)
       
   452       png_memset(ret,0,(png_size_t)size);
       
   453    return (ret);
       
   454 }
       
   455 
       
   456 png_voidp PNGAPI
       
   457 png_malloc(png_structp png_ptr, png_alloc_size_t size)
   437 {
   458 {
   438    png_voidp ret;
   459    png_voidp ret;
   439 
   460 
   440 #ifdef PNG_USER_MEM_SUPPORTED
   461 #ifdef PNG_USER_MEM_SUPPORTED
   441    if (png_ptr == NULL || size == 0)
   462    if (png_ptr == NULL || size == 0)
   444    if (png_ptr->malloc_fn != NULL)
   465    if (png_ptr->malloc_fn != NULL)
   445       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   466       ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
   446    else
   467    else
   447       ret = (png_malloc_default(png_ptr, size));
   468       ret = (png_malloc_default(png_ptr, size));
   448    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   469    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
   449        png_error(png_ptr, "Out of Memory!");
   470        png_error(png_ptr, "Out of Memory");
   450    return (ret);
   471    return (ret);
   451 }
   472 }
   452 
   473 
   453 png_voidp PNGAPI
   474 png_voidp PNGAPI
   454 png_malloc_default(png_structp png_ptr, png_uint_32 size)
   475 png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
   455 {
   476 {
   456    png_voidp ret;
   477    png_voidp ret;
   457 #endif /* PNG_USER_MEM_SUPPORTED */
   478 #endif /* PNG_USER_MEM_SUPPORTED */
   458 
   479 
   459    if (png_ptr == NULL || size == 0)
   480    if (png_ptr == NULL || size == 0)
   536 #endif
   557 #endif
   537 }
   558 }
   538 
   559 
   539 #endif /* Not Borland DOS special memory handler */
   560 #endif /* Not Borland DOS special memory handler */
   540 
   561 
   541 #if defined(PNG_1_0_X)
       
   542 #  define png_malloc_warn png_malloc
       
   543 #else
       
   544 /* This function was added at libpng version 1.2.3.  The png_malloc_warn()
   562 /* This function was added at libpng version 1.2.3.  The png_malloc_warn()
   545  * function will set up png_malloc() to issue a png_warning and return NULL
   563  * function will set up png_malloc() to issue a png_warning and return NULL
   546  * instead of issuing a png_error, if it fails to allocate the requested
   564  * instead of issuing a png_error, if it fails to allocate the requested
   547  * memory.
   565  * memory.
   548  */
   566  */
   549 png_voidp PNGAPI
   567 png_voidp PNGAPI
   550 png_malloc_warn(png_structp png_ptr, png_uint_32 size)
   568 png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
   551 {
   569 {
   552    png_voidp ptr;
   570    png_voidp ptr;
   553    png_uint_32 save_flags;
   571    png_uint_32 save_flags;
   554    if (png_ptr == NULL)
   572    if (png_ptr == NULL)
   555       return (NULL);
   573       return (NULL);
   558    png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   576    png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
   559    ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
   577    ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
   560    png_ptr->flags=save_flags;
   578    png_ptr->flags=save_flags;
   561    return(ptr);
   579    return(ptr);
   562 }
   580 }
   563 #endif
   581 
   564 
       
   565 png_voidp PNGAPI
       
   566 png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
       
   567    png_uint_32 length)
       
   568 {
       
   569    png_size_t size;
       
   570 
       
   571    size = (png_size_t)length;
       
   572    if ((png_uint_32)size != length)
       
   573       png_error(png_ptr, "Overflow in png_memcpy_check.");
       
   574 
       
   575    return(png_memcpy (s1, s2, size));
       
   576 }
       
   577 
       
   578 png_voidp PNGAPI
       
   579 png_memset_check (png_structp png_ptr, png_voidp s1, int value,
       
   580    png_uint_32 length)
       
   581 {
       
   582    png_size_t size;
       
   583 
       
   584    size = (png_size_t)length;
       
   585    if ((png_uint_32)size != length)
       
   586       png_error(png_ptr, "Overflow in png_memset_check.");
       
   587 
       
   588    return (png_memset (s1, value, size));
       
   589 
       
   590 }
       
   591 
   582 
   592 #ifdef PNG_USER_MEM_SUPPORTED
   583 #ifdef PNG_USER_MEM_SUPPORTED
   593 /* This function is called when the application wants to use another method
   584 /* This function is called when the application wants to use another method
   594  * of allocating and freeing memory.
   585  * of allocating and freeing memory.
   595  */
   586  */