home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / png / pngmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  15.5 KB  |  572 lines  |  [TEXT/CWIE]

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