pngmem.c
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:17k
源码类别:

界面编程

开发平台:

Visual C++

  1. /* pngmem.c - stub functions for memory allocation
  2.  *
  3.  * Last changed in libpng 1.2.30 [August 15, 2008]
  4.  * For conditions of distribution and use, see copyright notice in png.h
  5.  * Copyright (c) 1998-2008 Glenn Randers-Pehrson
  6.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8.  *
  9.  * This file provides a location for all memory allocation.  Users who
  10.  * need special memory handling are expected to supply replacement
  11.  * functions for png_malloc() and png_free(), and to use
  12.  * png_create_read_struct_2() and png_create_write_struct_2() to
  13.  * identify the replacement functions.
  14.  */
  15. #define PNG_INTERNAL
  16. #include "png.h"
  17. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  18. /* Borland DOS special memory handler */
  19. #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
  20. /* if you change this, be sure to change the one in png.h also */
  21. /* Allocate memory for a png_struct.  The malloc and memset can be replaced
  22.    by a single call to calloc() if this is thought to improve performance. */
  23. png_voidp /* PRIVATE */
  24. png_create_struct(int type)
  25. {
  26. #ifdef PNG_USER_MEM_SUPPORTED
  27.    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  28. }
  29. /* Alternate version of png_create_struct, for use with user-defined malloc. */
  30. png_voidp /* PRIVATE */
  31. png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  32. {
  33. #endif /* PNG_USER_MEM_SUPPORTED */
  34.    png_size_t size;
  35.    png_voidp struct_ptr;
  36.    if (type == PNG_STRUCT_INFO)
  37.      size = png_sizeof(png_info);
  38.    else if (type == PNG_STRUCT_PNG)
  39.      size = png_sizeof(png_struct);
  40.    else
  41.      return (png_get_copyright(NULL));
  42. #ifdef PNG_USER_MEM_SUPPORTED
  43.    if (malloc_fn != NULL)
  44.    {
  45.       png_struct dummy_struct;
  46.       png_structp png_ptr = &dummy_struct;
  47.       png_ptr->mem_ptr=mem_ptr;
  48.       struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
  49.    }
  50.    else
  51. #endif /* PNG_USER_MEM_SUPPORTED */
  52.       struct_ptr = (png_voidp)farmalloc(size);
  53.    if (struct_ptr != NULL)
  54.       png_memset(struct_ptr, 0, size);
  55.    return (struct_ptr);
  56. }
  57. /* Free memory allocated by a png_create_struct() call */
  58. void /* PRIVATE */
  59. png_destroy_struct(png_voidp struct_ptr)
  60. {
  61. #ifdef PNG_USER_MEM_SUPPORTED
  62.    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  63. }
  64. /* Free memory allocated by a png_create_struct() call */
  65. void /* PRIVATE */
  66. png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  67.     png_voidp mem_ptr)
  68. {
  69. #endif
  70.    if (struct_ptr != NULL)
  71.    {
  72. #ifdef PNG_USER_MEM_SUPPORTED
  73.       if (free_fn != NULL)
  74.       {
  75.          png_struct dummy_struct;
  76.          png_structp png_ptr = &dummy_struct;
  77.          png_ptr->mem_ptr=mem_ptr;
  78.          (*(free_fn))(png_ptr, struct_ptr);
  79.          return;
  80.       }
  81. #endif /* PNG_USER_MEM_SUPPORTED */
  82.       farfree (struct_ptr);
  83.    }
  84. }
  85. /* Allocate memory.  For reasonable files, size should never exceed
  86.  * 64K.  However, zlib may allocate more then 64K if you don't tell
  87.  * it not to.  See zconf.h and png.h for more information. zlib does
  88.  * need to allocate exactly 64K, so whatever you call here must
  89.  * have the ability to do that.
  90.  *
  91.  * Borland seems to have a problem in DOS mode for exactly 64K.
  92.  * It gives you a segment with an offset of 8 (perhaps to store its
  93.  * memory stuff).  zlib doesn't like this at all, so we have to
  94.  * detect and deal with it.  This code should not be needed in
  95.  * Windows or OS/2 modes, and only in 16 bit mode.  This code has
  96.  * been updated by Alexander Lehmann for version 0.89 to waste less
  97.  * memory.
  98.  *
  99.  * Note that we can't use png_size_t for the "size" declaration,
  100.  * since on some systems a png_size_t is a 16-bit quantity, and as a
  101.  * result, we would be truncating potentially larger memory requests
  102.  * (which should cause a fatal error) and introducing major problems.
  103.  */
  104. png_voidp PNGAPI
  105. png_malloc(png_structp png_ptr, png_uint_32 size)
  106. {
  107.    png_voidp ret;
  108.    if (png_ptr == NULL || size == 0)
  109.       return (NULL);
  110. #ifdef PNG_USER_MEM_SUPPORTED
  111.    if (png_ptr->malloc_fn != NULL)
  112.        ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  113.    else
  114.        ret = (png_malloc_default(png_ptr, size));
  115.    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  116.        png_error(png_ptr, "Out of memory!");
  117.    return (ret);
  118. }
  119. png_voidp PNGAPI
  120. png_malloc_default(png_structp png_ptr, png_uint_32 size)
  121. {
  122.    png_voidp ret;
  123. #endif /* PNG_USER_MEM_SUPPORTED */
  124.    if (png_ptr == NULL || size == 0)
  125.       return (NULL);
  126. #ifdef PNG_MAX_MALLOC_64K
  127.    if (size > (png_uint_32)65536L)
  128.    {
  129.       png_warning(png_ptr, "Cannot Allocate > 64K");
  130.       ret = NULL;
  131.    }
  132.    else
  133. #endif
  134.    if (size != (size_t)size)
  135.      ret = NULL;
  136.    else if (size == (png_uint_32)65536L)
  137.    {
  138.       if (png_ptr->offset_table == NULL)
  139.       {
  140.          /* try to see if we need to do any of this fancy stuff */
  141.          ret = farmalloc(size);
  142.          if (ret == NULL || ((png_size_t)ret & 0xffff))
  143.          {
  144.             int num_blocks;
  145.             png_uint_32 total_size;
  146.             png_bytep table;
  147.             int i;
  148.             png_byte huge * hptr;
  149.             if (ret != NULL)
  150.             {
  151.                farfree(ret);
  152.                ret = NULL;
  153.             }
  154.             if (png_ptr->zlib_window_bits > 14)
  155.                num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  156.             else
  157.                num_blocks = 1;
  158.             if (png_ptr->zlib_mem_level >= 7)
  159.                num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  160.             else
  161.                num_blocks++;
  162.             total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  163.             table = farmalloc(total_size);
  164.             if (table == NULL)
  165.             {
  166. #ifndef PNG_USER_MEM_SUPPORTED
  167.                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  168.                   png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
  169.                else
  170.                   png_warning(png_ptr, "Out Of Memory.");
  171. #endif
  172.                return (NULL);
  173.             }
  174.             if ((png_size_t)table & 0xfff0)
  175.             {
  176. #ifndef PNG_USER_MEM_SUPPORTED
  177.                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  178.                   png_error(png_ptr,
  179.                     "Farmalloc didn't return normalized pointer");
  180.                else
  181.                   png_warning(png_ptr,
  182.                     "Farmalloc didn't return normalized pointer");
  183. #endif
  184.                return (NULL);
  185.             }
  186.             png_ptr->offset_table = table;
  187.             png_ptr->offset_table_ptr = farmalloc(num_blocks *
  188.                png_sizeof(png_bytep));
  189.             if (png_ptr->offset_table_ptr == NULL)
  190.             {
  191. #ifndef PNG_USER_MEM_SUPPORTED
  192.                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  193.                   png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
  194.                else
  195.                   png_warning(png_ptr, "Out Of memory.");
  196. #endif
  197.                return (NULL);
  198.             }
  199.             hptr = (png_byte huge *)table;
  200.             if ((png_size_t)hptr & 0xf)
  201.             {
  202.                hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  203.                hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
  204.             }
  205.             for (i = 0; i < num_blocks; i++)
  206.             {
  207.                png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  208.                hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
  209.             }
  210.             png_ptr->offset_table_number = num_blocks;
  211.             png_ptr->offset_table_count = 0;
  212.             png_ptr->offset_table_count_free = 0;
  213.          }
  214.       }
  215.       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  216.       {
  217. #ifndef PNG_USER_MEM_SUPPORTED
  218.          if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  219.             png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
  220.          else
  221.             png_warning(png_ptr, "Out of Memory.");
  222. #endif
  223.          return (NULL);
  224.       }
  225.       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  226.    }
  227.    else
  228.       ret = farmalloc(size);
  229. #ifndef PNG_USER_MEM_SUPPORTED
  230.    if (ret == NULL)
  231.    {
  232.       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  233.          png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
  234.       else
  235.          png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
  236.    }
  237. #endif
  238.    return (ret);
  239. }
  240. /* free a pointer allocated by png_malloc().  In the default
  241.    configuration, png_ptr is not used, but is passed in case it
  242.    is needed.  If ptr is NULL, return without taking any action. */
  243. void PNGAPI
  244. png_free(png_structp png_ptr, png_voidp ptr)
  245. {
  246.    if (png_ptr == NULL || ptr == NULL)
  247.       return;
  248. #ifdef PNG_USER_MEM_SUPPORTED
  249.    if (png_ptr->free_fn != NULL)
  250.    {
  251.       (*(png_ptr->free_fn))(png_ptr, ptr);
  252.       return;
  253.    }
  254.    else png_free_default(png_ptr, ptr);
  255. }
  256. void PNGAPI
  257. png_free_default(png_structp png_ptr, png_voidp ptr)
  258. {
  259. #endif /* PNG_USER_MEM_SUPPORTED */
  260.    if (png_ptr == NULL || ptr == NULL) return;
  261.    if (png_ptr->offset_table != NULL)
  262.    {
  263.       int i;
  264.       for (i = 0; i < png_ptr->offset_table_count; i++)
  265.       {
  266.          if (ptr == png_ptr->offset_table_ptr[i])
  267.          {
  268.             ptr = NULL;
  269.             png_ptr->offset_table_count_free++;
  270.             break;
  271.          }
  272.       }
  273.       if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  274.       {
  275.          farfree(png_ptr->offset_table);
  276.          farfree(png_ptr->offset_table_ptr);
  277.          png_ptr->offset_table = NULL;
  278.          png_ptr->offset_table_ptr = NULL;
  279.       }
  280.    }
  281.    if (ptr != NULL)
  282.    {
  283.       farfree(ptr);
  284.    }
  285. }
  286. #else /* Not the Borland DOS special memory handler */
  287. /* Allocate memory for a png_struct or a png_info.  The malloc and
  288.    memset can be replaced by a single call to calloc() if this is thought
  289.    to improve performance noticably. */
  290. png_voidp /* PRIVATE */
  291. png_create_struct(int type)
  292. {
  293. #ifdef PNG_USER_MEM_SUPPORTED
  294.    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
  295. }
  296. /* Allocate memory for a png_struct or a png_info.  The malloc and
  297.    memset can be replaced by a single call to calloc() if this is thought
  298.    to improve performance noticably. */
  299. png_voidp /* PRIVATE */
  300. png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
  301. {
  302. #endif /* PNG_USER_MEM_SUPPORTED */
  303.    png_size_t size;
  304.    png_voidp struct_ptr;
  305.    if (type == PNG_STRUCT_INFO)
  306.       size = png_sizeof(png_info);
  307.    else if (type == PNG_STRUCT_PNG)
  308.       size = png_sizeof(png_struct);
  309.    else
  310.       return (NULL);
  311. #ifdef PNG_USER_MEM_SUPPORTED
  312.    if (malloc_fn != NULL)
  313.    {
  314.       png_struct dummy_struct;
  315.       png_structp png_ptr = &dummy_struct;
  316.       png_ptr->mem_ptr=mem_ptr;
  317.       struct_ptr = (*(malloc_fn))(png_ptr, size);
  318.       if (struct_ptr != NULL)
  319.          png_memset(struct_ptr, 0, size);
  320.       return (struct_ptr);
  321.    }
  322. #endif /* PNG_USER_MEM_SUPPORTED */
  323. #if defined(__TURBOC__) && !defined(__FLAT__)
  324.    struct_ptr = (png_voidp)farmalloc(size);
  325. #else
  326. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  327.    struct_ptr = (png_voidp)halloc(size, 1);
  328. # else
  329.    struct_ptr = (png_voidp)malloc(size);
  330. # endif
  331. #endif
  332.    if (struct_ptr != NULL)
  333.       png_memset(struct_ptr, 0, size);
  334.    return (struct_ptr);
  335. }
  336. /* Free memory allocated by a png_create_struct() call */
  337. void /* PRIVATE */
  338. png_destroy_struct(png_voidp struct_ptr)
  339. {
  340. #ifdef PNG_USER_MEM_SUPPORTED
  341.    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
  342. }
  343. /* Free memory allocated by a png_create_struct() call */
  344. void /* PRIVATE */
  345. png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
  346.     png_voidp mem_ptr)
  347. {
  348. #endif /* PNG_USER_MEM_SUPPORTED */
  349.    if (struct_ptr != NULL)
  350.    {
  351. #ifdef PNG_USER_MEM_SUPPORTED
  352.       if (free_fn != NULL)
  353.       {
  354.          png_struct dummy_struct;
  355.          png_structp png_ptr = &dummy_struct;
  356.          png_ptr->mem_ptr=mem_ptr;
  357.          (*(free_fn))(png_ptr, struct_ptr);
  358.          return;
  359.       }
  360. #endif /* PNG_USER_MEM_SUPPORTED */
  361. #if defined(__TURBOC__) && !defined(__FLAT__)
  362.       farfree(struct_ptr);
  363. #else
  364. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  365.       hfree(struct_ptr);
  366. # else
  367.       free(struct_ptr);
  368. # endif
  369. #endif
  370.    }
  371. }
  372. /* Allocate memory.  For reasonable files, size should never exceed
  373.    64K.  However, zlib may allocate more then 64K if you don't tell
  374.    it not to.  See zconf.h and png.h for more information.  zlib does
  375.    need to allocate exactly 64K, so whatever you call here must
  376.    have the ability to do that. */
  377. png_voidp PNGAPI
  378. png_malloc(png_structp png_ptr, png_uint_32 size)
  379. {
  380.    png_voidp ret;
  381. #ifdef PNG_USER_MEM_SUPPORTED
  382.    if (png_ptr == NULL || size == 0)
  383.       return (NULL);
  384.    if (png_ptr->malloc_fn != NULL)
  385.        ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
  386.    else
  387.        ret = (png_malloc_default(png_ptr, size));
  388.    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  389.        png_error(png_ptr, "Out of Memory!");
  390.    return (ret);
  391. }
  392. png_voidp PNGAPI
  393. png_malloc_default(png_structp png_ptr, png_uint_32 size)
  394. {
  395.    png_voidp ret;
  396. #endif /* PNG_USER_MEM_SUPPORTED */
  397.    if (png_ptr == NULL || size == 0)
  398.       return (NULL);
  399. #ifdef PNG_MAX_MALLOC_64K
  400.    if (size > (png_uint_32)65536L)
  401.    {
  402. #ifndef PNG_USER_MEM_SUPPORTED
  403.       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  404.          png_error(png_ptr, "Cannot Allocate > 64K");
  405.       else
  406. #endif
  407.          return NULL;
  408.    }
  409. #endif
  410.  /* Check for overflow */
  411. #if defined(__TURBOC__) && !defined(__FLAT__)
  412.  if (size != (unsigned long)size)
  413.    ret = NULL;
  414.  else
  415.    ret = farmalloc(size);
  416. #else
  417. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  418.  if (size != (unsigned long)size)
  419.    ret = NULL;
  420.  else
  421.    ret = halloc(size, 1);
  422. # else
  423.  if (size != (size_t)size)
  424.    ret = NULL;
  425.  else
  426.    ret = malloc((size_t)size);
  427. # endif
  428. #endif
  429. #ifndef PNG_USER_MEM_SUPPORTED
  430.    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
  431.       png_error(png_ptr, "Out of Memory");
  432. #endif
  433.    return (ret);
  434. }
  435. /* Free a pointer allocated by png_malloc().  If ptr is NULL, return
  436.    without taking any action. */
  437. void PNGAPI
  438. png_free(png_structp png_ptr, png_voidp ptr)
  439. {
  440.    if (png_ptr == NULL || ptr == NULL)
  441.       return;
  442. #ifdef PNG_USER_MEM_SUPPORTED
  443.    if (png_ptr->free_fn != NULL)
  444.    {
  445.       (*(png_ptr->free_fn))(png_ptr, ptr);
  446.       return;
  447.    }
  448.    else png_free_default(png_ptr, ptr);
  449. }
  450. void PNGAPI
  451. png_free_default(png_structp png_ptr, png_voidp ptr)
  452. {
  453.    if (png_ptr == NULL || ptr == NULL)
  454.       return;
  455. #endif /* PNG_USER_MEM_SUPPORTED */
  456. #if defined(__TURBOC__) && !defined(__FLAT__)
  457.    farfree(ptr);
  458. #else
  459. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  460.    hfree(ptr);
  461. # else
  462.    free(ptr);
  463. # endif
  464. #endif
  465. }
  466. #endif /* Not Borland DOS special memory handler */
  467. #if defined(PNG_1_0_X)
  468. #  define png_malloc_warn png_malloc
  469. #else
  470. /* This function was added at libpng version 1.2.3.  The png_malloc_warn()
  471.  * function will set up png_malloc() to issue a png_warning and return NULL
  472.  * instead of issuing a png_error, if it fails to allocate the requested
  473.  * memory.
  474.  */
  475. png_voidp PNGAPI
  476. png_malloc_warn(png_structp png_ptr, png_uint_32 size)
  477. {
  478.    png_voidp ptr;
  479.    png_uint_32 save_flags;
  480.    if (png_ptr == NULL) return (NULL);
  481.    save_flags = png_ptr->flags;
  482.    png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
  483.    ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
  484.    png_ptr->flags=save_flags;
  485.    return(ptr);
  486. }
  487. #endif
  488. png_voidp PNGAPI
  489. png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
  490.    png_uint_32 length)
  491. {
  492.    png_size_t size;
  493.    size = (png_size_t)length;
  494.    if ((png_uint_32)size != length)
  495.       png_error(png_ptr, "Overflow in png_memcpy_check.");
  496.    return(png_memcpy (s1, s2, size));
  497. }
  498. png_voidp PNGAPI
  499. png_memset_check (png_structp png_ptr, png_voidp s1, int value,
  500.    png_uint_32 length)
  501. {
  502.    png_size_t size;
  503.    size = (png_size_t)length;
  504.    if ((png_uint_32)size != length)
  505.       png_error(png_ptr, "Overflow in png_memset_check.");
  506.    return (png_memset (s1, value, size));
  507. }
  508. #ifdef PNG_USER_MEM_SUPPORTED
  509. /* This function is called when the application wants to use another method
  510.  * of allocating and freeing memory.
  511.  */
  512. void PNGAPI
  513. png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  514.   malloc_fn, png_free_ptr free_fn)
  515. {
  516.    if (png_ptr != NULL)
  517.    {
  518.       png_ptr->mem_ptr = mem_ptr;
  519.       png_ptr->malloc_fn = malloc_fn;
  520.       png_ptr->free_fn = free_fn;
  521.    }
  522. }
  523. /* This function returns a pointer to the mem_ptr associated with the user
  524.  * functions.  The application should free any memory associated with this
  525.  * pointer before png_write_destroy and png_read_destroy are called.
  526.  */
  527. png_voidp PNGAPI
  528. png_get_mem_ptr(png_structp png_ptr)
  529. {
  530.    if (png_ptr == NULL) return (NULL);
  531.    return ((png_voidp)png_ptr->mem_ptr);
  532. }
  533. #endif /* PNG_USER_MEM_SUPPORTED */
  534. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */