home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / flashsource-r0_7_8.zip / libpng / pngmem.c < prev    next >
C/C++ Source or Header  |  2001-04-27  |  14KB  |  510 lines

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