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 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/>.
31 xrealloc_gpgrt (void *p
, size_t n
)
36 return xrealloc (p
, n
);
45 /* Borrowed from g10code:
46 * https://lists.gnupg.org/pipermail/gnupg-devel/2018-November/034060.html
49 wipememory (void *ptr
, int c
, size_t len
)
51 #if defined(HAVE_W32_SYSTEM) && defined(SecureZeroMemory)
53 SecureZeroMemory (ptr
, len
);
54 #elif defined(HAVE_EXPLICIT_BZERO)
56 explicit_bzero (ptr
, len
);
57 #elif defined(HAVE_MEMSET_S)
58 memset_s (ptr
, len
, c
, len
);
60 /* Prevent compiler from optimizing away the call to memset by accessing
61 memset through volatile pointer. */
62 static void *(*volatile memset_ptr
)(void *, int, size_t) = (void *)memset
;
63 memset_ptr (ptr
, c
, len
);
72 } __attribute ((packed
));
83 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
84 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
85 wipememory (p
, 0, m
->size
);
97 m
= malloc (sizeof (struct memchunk_s
)+size
);
102 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
106 xcalloc (size_t nmemb
, size_t size
)
109 struct memchunk_s
*m
;
111 m
= malloc (sizeof (struct memchunk_s
)+(nmemb
*size
));
115 m
->size
= nmemb
*size
;
116 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
117 memset (p
, 0, m
->size
);
122 xrealloc (void *ptr
, size_t size
)
125 struct memchunk_s
*m
, *mp
;
130 m
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
131 p
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
132 wipememory (p
, 0, m
->size
);
137 return xmalloc (size
);
139 m
= malloc (sizeof (struct memchunk_s
)+size
);
144 np
= (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));
146 mp
= (struct memchunk_s
*)((char *)ptr
-(offsetof (struct memchunk_s
, data
)));
147 p
= (void *)((char *)mp
+(offsetof (struct memchunk_s
, data
)));
149 n
= size
> mp
->size
? mp
->size
: size
;
151 wipememory (p
, 0, mp
->size
);
154 return (void *)((char *)m
+(offsetof (struct memchunk_s
, data
)));