Prusa MINI Firmware overview
pngmem.c File Reference
#include "pngpriv.h"

Functions

void png_destroy_png_struct (png_structrp png_ptr)
 
 PNG_FUNCTION (png_voidp, PNGAPI png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size), PNG_ALLOCATED)
 
 PNG_FUNCTION (png_voidp, png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), PNG_ALLOCATED)
 
static png_voidp png_malloc_array_checked (png_const_structrp png_ptr, int nelements, size_t element_size)
 
 PNG_FUNCTION (png_voidp, png_malloc_array,(png_const_structrp png_ptr, int nelements, size_t element_size), PNG_ALLOCATED)
 
 PNG_FUNCTION (png_voidp, png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array, int old_elements, int add_elements, size_t element_size), PNG_ALLOCATED)
 
void PNGAPI png_free (png_const_structrp png_ptr, png_voidp ptr)
 
 PNG_FUNCTION (void, PNGAPI png_free_default,(png_const_structrp png_ptr, png_voidp ptr), PNG_DEPRECATED)
 
void PNGAPI png_set_mem_fn (png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)
 
png_voidp PNGAPI png_get_mem_ptr (png_const_structrp png_ptr)
 

Function Documentation

◆ png_destroy_png_struct()

void png_destroy_png_struct ( png_structrp  png_ptr)
26 {
27  if (png_ptr != NULL)
28  {
29  /* png_free might call png_error and may certainly call
30  * png_get_mem_ptr, so fake a temporary png_struct to support this.
31  */
32  png_struct dummy_struct = *png_ptr;
33  memset(png_ptr, 0, (sizeof *png_ptr));
34  png_free(&dummy_struct, png_ptr);
35 
36 # ifdef PNG_SETJMP_SUPPORTED
37  /* We may have a jmp_buf left to deallocate. */
38  png_free_jmpbuf(&dummy_struct);
39 # endif
40  }
41 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PNG_FUNCTION() [1/5]

PNG_FUNCTION ( png_voidp  ,
PNGAPI  png_calloc,
(png_const_structrp png_ptr, png_alloc_size_t size ,
PNG_ALLOCATED  PNG_DEPRECATED 
)
51 {
52  png_voidp ret;
53 
54  ret = png_malloc(png_ptr, size);
55 
56  if (ret != NULL)
57  memset(ret, 0, size);
58 
59  return ret;
60 }

◆ PNG_FUNCTION() [2/5]

PNG_FUNCTION ( png_voidp  ,
png_malloc_base  ,
(png_const_structrp png_ptr, png_alloc_size_t size ,
PNG_ALLOCATED   
)
70 {
71  /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
72  * allocators have also been removed in 1.6.0, so any 16-bit system now has
73  * to implement a user memory handler. This checks to be sure it isn't
74  * called with big numbers.
75  */
76 #ifndef PNG_USER_MEM_SUPPORTED
78 #endif
79 
80  /* Some compilers complain that this is always true. However, it
81  * can be false when integer overflow happens.
82  */
83  if (size > 0 && size <= PNG_SIZE_MAX
84 # ifdef PNG_MAX_MALLOC_64K
85  && size <= 65536U
86 # endif
87  )
88  {
89 #ifdef PNG_USER_MEM_SUPPORTED
90  if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
91  return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
92 
93  else
94 #endif
95  return malloc((size_t)size); /* checked for truncation above */
96  }
97 
98  else
99  return NULL;
100 }

◆ png_malloc_array_checked()

static png_voidp png_malloc_array_checked ( png_const_structrp  png_ptr,
int  nelements,
size_t  element_size 
)
static
111 {
112  png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */
113 
114  if (req <= PNG_SIZE_MAX/element_size)
115  return png_malloc_base(png_ptr, req * element_size);
116 
117  /* The failure case when the request is too large */
118  return NULL;
119 }
Here is the caller graph for this function:

◆ PNG_FUNCTION() [3/5]

PNG_FUNCTION ( png_voidp  ,
png_malloc_array  ,
(png_const_structrp png_ptr, int nelements, size_t element_size)  ,
PNG_ALLOCATED   
)
124 {
125  if (nelements <= 0 || element_size == 0)
126  png_error(png_ptr, "internal error: array alloc");
127 
128  return png_malloc_array_checked(png_ptr, nelements, element_size);
129 }
Here is the call graph for this function:

◆ PNG_FUNCTION() [4/5]

PNG_FUNCTION ( png_voidp  ,
png_realloc_array  ,
(png_const_structrp png_ptr, png_const_voidp old_array, int old_elements, int add_elements, size_t element_size)  ,
PNG_ALLOCATED   
)
134 {
135  /* These are internal errors: */
136  if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
137  (old_array == NULL && old_elements > 0))
138  png_error(png_ptr, "internal error: array realloc");
139 
140  /* Check for overflow on the elements count (so the caller does not have to
141  * check.)
142  */
143  if (add_elements <= INT_MAX - old_elements)
144  {
146  old_elements+add_elements, element_size);
147 
148  if (new_array != NULL)
149  {
150  /* Because png_malloc_array worked the size calculations below cannot
151  * overflow.
152  */
153  if (old_elements > 0)
154  memcpy(new_array, old_array, element_size*(unsigned)old_elements);
155 
156  memset((char*)new_array + element_size*(unsigned)old_elements, 0,
157  element_size*(unsigned)add_elements);
158 
159  return new_array;
160  }
161  }
162 
163  return NULL; /* error */
164 }
Here is the call graph for this function:

◆ png_free()

void PNGAPI png_free ( png_const_structrp  png_ptr,
png_voidp  ptr 
)
233 {
234  if (png_ptr == NULL || ptr == NULL)
235  return;
236 
237 #ifdef PNG_USER_MEM_SUPPORTED
238  if (png_ptr->free_fn != NULL)
239  png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr);
240 
241  else
242  png_free_default(png_ptr, ptr);
243 }
Here is the caller graph for this function:

◆ PNG_FUNCTION() [5/5]

PNG_FUNCTION ( void  ,
PNGAPI  png_free_default,
(png_const_structrp png_ptr, png_voidp ptr)  ,
PNG_DEPRECATED   
)
247 {
248  if (png_ptr == NULL || ptr == NULL)
249  return;
250 #endif /* USER_MEM */
251 
252  free(ptr);
253 }

◆ png_set_mem_fn()

void PNGAPI png_set_mem_fn ( png_structrp  png_ptr,
png_voidp  mem_ptr,
png_malloc_ptr  malloc_fn,
png_free_ptr  free_fn 
)
262 {
263  if (png_ptr != NULL)
264  {
265  png_ptr->mem_ptr = mem_ptr;
266  png_ptr->malloc_fn = malloc_fn;
267  png_ptr->free_fn = free_fn;
268  }
269 }
Here is the caller graph for this function:

◆ png_get_mem_ptr()

png_voidp PNGAPI png_get_mem_ptr ( png_const_structrp  png_ptr)
277 {
278  if (png_ptr == NULL)
279  return NULL;
280 
281  return png_ptr->mem_ptr;
282 }
png_malloc_array_checked
static png_voidp png_malloc_array_checked(png_const_structrp png_ptr, int nelements, size_t element_size)
Definition: pngmem.c:109
PNG_UNUSED
#define PNG_UNUSED(param)
Definition: pngpriv.h:438
png_voidp
void * png_voidp
Definition: pngconf.h:577
png_free
void PNGAPI png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:232
PNG_SIZE_MAX
#define PNG_SIZE_MAX
Definition: png.h:651
NULL
#define NULL
Definition: usbd_def.h:53
png_ptr
png_structrp png_ptr
Definition: png.h:1083
png_structrp
png_struct *PNG_RESTRICT png_structrp
Definition: png.h:469
png_free_jmpbuf
void png_free_jmpbuf(png_structrp png_ptr)
Definition: pngerror.c:666
png_struct_def
Definition: pngstruct.h:143
png_error
else png_error(png_ptr, "png_image_write_to_memory: PNG too big")
png_alloc_size_t
size_t png_alloc_size_t
Definition: pngconf.h:557
png_constcast
#define png_constcast(type, value)
Definition: pngpriv.h:504
size
static png_bytep size_t size
Definition: pngwrite.c:2170