2 Copyright (C) 2006-2021 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 as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
32 xrealloc_gpgrt (void *p
, size_t n
)
37 return xrealloc (p
, n
);
46 /* Borrowed from g10code:
47 * https://lists.gnupg.org/pipermail/gnupg-devel/2018-November/034060.html
50 wipememory (void *ptr
, int c
, size_t len
)
52 #if defined(HAVE_W32_SYSTEM) && defined(SecureZeroMemory)
54 SecureZeroMemory (ptr
, len
);
55 #elif defined(HAVE_EXPLICIT_BZERO)
57 explicit_bzero (ptr
, len
);
58 #elif defined(HAVE_MEMSET_S)
59 memset_s (ptr
, len
, c
, len
);
61 /* Prevent compiler from optimizing away the call to memset by accessing
62 memset through volatile pointer. */
63 static void *(*volatile memset_ptr
)(void *, int, size_t) = (void *)memset
;
64 memset_ptr (ptr
, c
, len
);
73 } __attribute ((packed
));
84 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
85 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
86 wipememory (p
, 0, m
->size
);
98 m
= malloc (sizeof (struct memchunk_s
)+size
);
103 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
107 xcalloc (size_t nmemb
, size_t size
)
110 struct memchunk_s
*m
;
112 m
= malloc (sizeof (struct memchunk_s
)+(nmemb
*size
));
116 m
->size
= nmemb
*size
;
117 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
118 memset (p
, 0, m
->size
);
123 xrealloc (void *ptr
, size_t size
)
126 struct memchunk_s
*m
, *mp
;
131 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
132 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
133 wipememory (p
, 0, m
->size
);
138 return xmalloc (size
);
140 m
= malloc (sizeof (struct memchunk_s
)+size
);
145 np
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
147 mp
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
148 p
= (void *)((char *)mp
+(offsetof (struct memchunk_s
, data
)));
150 n
= size
> mp
->size
? mp
->size
: size
;
152 wipememory (p
, 0, mp
->size
);
155 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));