pngmem.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* pngmem.c - stub functions for memory allocation
  2. *
  3. * Last changed in libpng 1.6.8 [December 19, 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_ptr, 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. #ifndef 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. #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
  87. defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
  88. /* This is really here only to work round a spurious warning in GCC 4.6 and 4.7
  89. * that arises because of the checks in png_realloc_array that are repeated in
  90. * png_malloc_array.
  91. */
  92. static png_voidp
  93. png_malloc_array_checked(png_const_structrp png_ptr, int nelements,
  94. size_t element_size)
  95. {
  96. png_alloc_size_t req = nelements; /* known to be > 0 */
  97. if (req <= PNG_SIZE_MAX/element_size)
  98. return png_malloc_base(png_ptr, req * element_size);
  99. /* The failure case when the request is too large */
  100. return NULL;
  101. }
  102. PNG_FUNCTION(png_voidp /* PRIVATE */,
  103. png_malloc_array,(png_const_structrp png_ptr, int nelements,
  104. size_t element_size),PNG_ALLOCATED)
  105. {
  106. if (nelements <= 0 || element_size == 0)
  107. png_error(png_ptr, "internal error: array alloc");
  108. return png_malloc_array_checked(png_ptr, nelements, element_size);
  109. }
  110. PNG_FUNCTION(png_voidp /* PRIVATE */,
  111. png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array,
  112. int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED)
  113. {
  114. /* These are internal errors: */
  115. if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
  116. (old_array == NULL && old_elements > 0))
  117. png_error(png_ptr, "internal error: array realloc");
  118. /* Check for overflow on the elements count (so the caller does not have to
  119. * check.)
  120. */
  121. if (add_elements <= INT_MAX - old_elements)
  122. {
  123. png_voidp new_array = png_malloc_array_checked(png_ptr,
  124. old_elements+add_elements, element_size);
  125. if (new_array != NULL)
  126. {
  127. /* Because png_malloc_array worked the size calculations below cannot
  128. * overflow.
  129. */
  130. if (old_elements > 0)
  131. memcpy(new_array, old_array, element_size*(unsigned)old_elements);
  132. memset((char*)new_array + element_size*(unsigned)old_elements, 0,
  133. element_size*(unsigned)add_elements);
  134. return new_array;
  135. }
  136. }
  137. return NULL; /* error */
  138. }
  139. #endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */
  140. /* Various functions that have different error handling are derived from this.
  141. * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate
  142. * function png_malloc_default is also provided.
  143. */
  144. PNG_FUNCTION(png_voidp,PNGAPI
  145. png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
  146. {
  147. png_voidp ret;
  148. if (png_ptr == NULL)
  149. return NULL;
  150. ret = png_malloc_base(png_ptr, size);
  151. if (ret == NULL)
  152. png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */
  153. return ret;
  154. }
  155. #ifdef PNG_USER_MEM_SUPPORTED
  156. PNG_FUNCTION(png_voidp,PNGAPI
  157. png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size),
  158. PNG_ALLOCATED PNG_DEPRECATED)
  159. {
  160. png_voidp ret;
  161. if (png_ptr == NULL)
  162. return NULL;
  163. /* Passing 'NULL' here bypasses the application provided memory handler. */
  164. ret = png_malloc_base(NULL/*use malloc*/, size);
  165. if (ret == NULL)
  166. png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */
  167. return ret;
  168. }
  169. #endif /* PNG_USER_MEM_SUPPORTED */
  170. /* This function was added at libpng version 1.2.3. The png_malloc_warn()
  171. * function will issue a png_warning and return NULL instead of issuing a
  172. * png_error, if it fails to allocate the requested memory.
  173. */
  174. PNG_FUNCTION(png_voidp,PNGAPI
  175. png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size),
  176. PNG_ALLOCATED)
  177. {
  178. if (png_ptr != NULL)
  179. {
  180. png_voidp ret = png_malloc_base(png_ptr, size);
  181. if (ret != NULL)
  182. return ret;
  183. png_warning(png_ptr, "Out of memory");
  184. }
  185. return NULL;
  186. }
  187. /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
  188. * without taking any action.
  189. */
  190. void PNGAPI
  191. png_free(png_const_structrp png_ptr, png_voidp ptr)
  192. {
  193. if (png_ptr == NULL || ptr == NULL)
  194. return;
  195. #ifdef PNG_USER_MEM_SUPPORTED
  196. if (png_ptr->free_fn != NULL)
  197. png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
  198. else
  199. png_free_default(png_ptr, ptr);
  200. }
  201. PNG_FUNCTION(void,PNGAPI
  202. png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED)
  203. {
  204. if (png_ptr == NULL || ptr == NULL)
  205. return;
  206. #endif /* PNG_USER_MEM_SUPPORTED */
  207. free(ptr);
  208. }
  209. #ifdef PNG_USER_MEM_SUPPORTED
  210. /* This function is called when the application wants to use another method
  211. * of allocating and freeing memory.
  212. */
  213. void PNGAPI
  214. png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr
  215. malloc_fn, png_free_ptr free_fn)
  216. {
  217. if (png_ptr != NULL)
  218. {
  219. png_ptr->mem_ptr = mem_ptr;
  220. png_ptr->malloc_fn = malloc_fn;
  221. png_ptr->free_fn = free_fn;
  222. }
  223. }
  224. /* This function returns a pointer to the mem_ptr associated with the user
  225. * functions. The application should free any memory associated with this
  226. * pointer before png_write_destroy and png_read_destroy are called.
  227. */
  228. png_voidp PNGAPI
  229. png_get_mem_ptr(png_const_structrp png_ptr)
  230. {
  231. if (png_ptr == NULL)
  232. return NULL;
  233. return png_ptr->mem_ptr;
  234. }
  235. #endif /* PNG_USER_MEM_SUPPORTED */
  236. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */