Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pngmem.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* pngmem.c - stub functions for memory allocation
  2. *
  3. * Last changed in libpng 1.6.0 [February 14, 2013]
  4. * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file provides a location for all memory allocation. Users who
  13. * need special memory handling are expected to supply replacement
  14. * functions for png_malloc() and png_free(), and to use
  15. * png_create_read_struct_2() and png_create_write_struct_2() to
  16. * identify the replacement functions.
  17. */
  18. #include "pngpriv.h"
  19. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  20. /* Free a png_struct */
  21. void /* PRIVATE */
  22. png_destroy_png_struct(png_structrp png_ptr)
  23. {
  24. if (png_ptr != NULL)
  25. {
  26. /* png_free might call png_error and may certainly call
  27. * png_get_mem_ptr, so fake a temporary png_struct to support this.
  28. */
  29. png_struct dummy_struct = *png_ptr;
  30. memset(png_ptr, 0, (sizeof *png_ptr));
  31. png_free(&dummy_struct, png_ptr);
  32. # ifdef PNG_SETJMP_SUPPORTED
  33. /* We may have a jmp_buf left to deallocate. */
  34. png_free_jmpbuf(&dummy_struct);
  35. # endif
  36. }
  37. }
  38. /* Allocate memory. For reasonable files, size should never exceed
  39. * 64K. However, zlib may allocate more then 64K if you don't tell
  40. * it not to. See zconf.h and png.h for more information. zlib does
  41. * need to allocate exactly 64K, so whatever you call here must
  42. * have the ability to do that.
  43. */
  44. PNG_FUNCTION(png_voidp,PNGAPI
  45. png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  46. {
  47. png_voidp ret;
  48. ret = png_malloc(png_ptr, size);
  49. if (ret != NULL)
  50. memset(ret, 0, size);
  51. return ret;
  52. }
  53. /* png_malloc_base, an internal function added at libpng 1.6.0, does the work of
  54. * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED.
  55. * Checking and error handling must happen outside this routine; it returns NULL
  56. * if the allocation cannot be done (for any reason.)
  57. */
  58. PNG_FUNCTION(png_voidp /* PRIVATE */,
  59. png_malloc_base,(png_const_structrp, png_alloc_size_t size),
  60. PNG_ALLOCATED)
  61. {
  62. /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
  63. * allocators have also been removed in 1.6.0, so any 16-bit system now has
  64. * to implement a user memory handler. This checks to be sure it isn't
  65. * called with big numbers.
  66. */
  67. #ifdef PNG_USER_MEM_SUPPORTED
  68. PNG_UNUSED(png_ptr)
  69. #endif
  70. if (size > 0 && size <= PNG_SIZE_MAX
  71. # ifdef PNG_MAX_MALLOC_64K
  72. && size <= 65536U
  73. # endif
  74. )
  75. {
  76. #ifdef PNG_USER_MEM_SUPPORTED
  77. if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
  78. return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
  79. else
  80. #endif
  81. return malloc((size_t)size); /* checked for truncation above */
  82. }
  83. else
  84. return NULL;
  85. }
  86. /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
  87. * that arises because of the checks in png_realloc_array that are repeated in
  88. * png_malloc_array.
  89. */
  90. static png_voidp
  91. png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
  92. size_t element_size)
  93. {
  94. png_alloc_size_t req = nelements; /* known to be > 0 */
  95. if (req <= PNG_SIZE_MAX/element_size)
  96. return png_malloc_base(png_ptr, req * element_size);
  97. /* The failure case when the request is too large */
  98. return NULL;
  99. }
  100. PNG_FUNCTION(png_voidp /* PRIVATE */,
  101. png_malloc_array,(png_const_structrp png_ptr, int nelements,
  102. size_t element_size),PNG_ALLOCATED)
  103. {
  104. if (nelements <= 0 || element_size == 0)
  105. png_error(png_ptr, "internal error: array alloc");
  106. return png_malloc_array_checked(png_ptr, nelements, element_size);
  107. }
  108. PNG_FUNCTION(png_voidp /* PRIVATE */,
  109. png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
  110. int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
  111. {
  112. /* These are internal errors: */
  113. if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
  114. (old_array == NULL && old_elements > 0))
  115. png_error(png_ptr, "internal error: array realloc");
  116. /* Check for overflow on the elements count (so the caller does not have to
  117. * check.)
  118. */
  119. if (add_elements <= INT_MAX - old_elements)
  120. {
  121. png_voidp new_array = png_malloc_array_checked(png_ptr,
  122. old_elements+add_elements, element_size);
  123. if (new_array != NULL)
  124. {
  125. /* Because png_malloc_array worked the size calculations below cannot
  126. * overflow.
  127. */
  128. if (old_elements > 0)
  129. memcpy(new_array, old_array, element_size*(unsigned)old_elements);
  130. memset((char*)new_array + element_size*(unsigned)old_elements, 0,
  131. element_size*(unsigned)add_elements);
  132. return new_array;
  133. }
  134. }
  135. return NULL; /* error */
  136. }
  137. /* Various functions that have different error handling are derived from this.
  138. * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
  139. * function png_malloc_default is also provided.
  140. */
  141. PNG_FUNCTION(png_voidp,PNGAPI
  142. png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  143. {
  144. png_voidp ret;
  145. if (png_ptr == NULL)
  146. return NULL;
  147. ret = png_malloc_base(png_ptr, size);
  148. if (ret == NULL)
  149. png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
  150. return ret;
  151. }
  152. #ifdef PNG_USER_MEM_SUPPORTED
  153. PNG_FUNCTION(png_voidp,PNGAPI
  154. png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
  155. PNG_ALLOCATED PNG_DEPRECATED)
  156. {
  157. png_voidp ret;
  158. if (png_ptr == NULL)
  159. return NULL;
  160. /* Passing 'NULL' here bypasses the application provided memory handler. */
  161. ret = png_malloc_base(NULL/*use malloc*/, size);
  162. if (ret == NULL)
  163. png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
  164. return ret;
  165. }
  166. #endif /* PNG_USER_MEM_SUPPORTED */
  167. /* This function was added at libpng version 1.2.3. The png_malloc_warn()
  168. * function will issue a png_warning and return NULL instead of issuing a
  169. * png_error, if it fails to allocate the requested memory.
  170. */
  171. PNG_FUNCTION(png_voidp,PNGAPI
  172. png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
  173. PNG_ALLOCATED)
  174. {
  175. if (png_ptr != NULL)
  176. {
  177. png_voidp ret = png_malloc_base(png_ptr, size);
  178. if (ret != NULL)
  179. return ret;
  180. png_warning(png_ptr, "Out of memory");
  181. }
  182. return NULL;
  183. }
  184. /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
  185. * without taking any action.
  186. */
  187. void PNGAPI
  188. png_free(png_const_structrp png_ptr, png_voidp ptr)
  189. {
  190. if (png_ptr == NULL || ptr == NULL)
  191. return;
  192. #ifdef PNG_USER_MEM_SUPPORTED
  193. if (png_ptr->free_fn != NULL)
  194. png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
  195. else
  196. png_free_default(png_ptr, ptr);
  197. }
  198. PNG_FUNCTION(void,PNGAPI
  199. png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
  200. {
  201. if (png_ptr == NULL || ptr == NULL)
  202. return;
  203. #endif /* PNG_USER_MEM_SUPPORTED */
  204. free(ptr);
  205. }
  206. #ifdef PNG_USER_MEM_SUPPORTED
  207. /* This function is called when the application wants to use another method
  208. * of allocating and freeing memory.
  209. */
  210. void PNGAPI
  211. png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  212. malloc_fn, png_free_ptr free_fn)
  213. {
  214. if (png_ptr != NULL)
  215. {
  216. png_ptr->mem_ptr = mem_ptr;
  217. png_ptr->malloc_fn = malloc_fn;
  218. png_ptr->free_fn = free_fn;
  219. }
  220. }
  221. /* This function returns a pointer to the mem_ptr associated with the user
  222. * functions. The application should free any memory associated with this
  223. * pointer before png_write_destroy and png_read_destroy are called.
  224. */
  225. png_voidp PNGAPI
  226. png_get_mem_ptr(png_const_structrp png_ptr)
  227. {
  228. if (png_ptr == NULL)
  229. return NULL;
  230. return png_ptr->mem_ptr;
  231. }
  232. #endif /* PNG_USER_MEM_SUPPORTED */
  233. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */