2 Copyright (C) 2006-2024 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
33 xrealloc_gpgrt (void *p
, size_t n
)
38 return xrealloc (p
, n
);
47 /* Borrowed from g10code:
48 * https://lists.gnupg.org/pipermail/gnupg-devel/2018-November/034060.html
51 wipememory (void *ptr
, int c
, size_t len
)
53 #if defined(HAVE_W32_SYSTEM) && defined(SecureZeroMemory)
55 SecureZeroMemory (ptr
, len
);
56 #elif defined(HAVE_EXPLICIT_BZERO)
58 explicit_bzero (ptr
, len
);
59 #elif defined(HAVE_MEMSET_S)
60 memset_s (ptr
, len
, c
, len
);
62 /* Prevent compiler from optimizing away the call to memset by accessing
63 memset through volatile pointer. */
64 static void *(*volatile memset_ptr
)(void *, int, size_t) = (void *)memset
;
65 memset_ptr (ptr
, c
, len
);
74 } __attribute ((packed
));
85 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
86 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
87 wipememory (p
, 0, m
->size
);
99 m
= malloc (sizeof (struct memchunk_s
)+size
);
104 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
108 xcalloc (size_t nmemb
, size_t size
)
111 struct memchunk_s
*m
;
113 m
= malloc (sizeof (struct memchunk_s
)+(nmemb
*size
));
117 m
->size
= nmemb
*size
;
118 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
119 memset (p
, 0, m
->size
);
124 xrealloc (void *ptr
, size_t size
)
127 struct memchunk_s
*m
, *mp
;
132 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
133 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
134 wipememory (p
, 0, m
->size
);
139 return xmalloc (size
);
141 m
= malloc (sizeof (struct memchunk_s
)+size
);
146 np
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
148 mp
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
149 p
= (void *)((char *)mp
+(offsetof (struct memchunk_s
, data
)));
151 n
= size
> mp
->size
? mp
->size
: size
;
153 wipememory (p
, 0, mp
->size
);
156 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));