1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef _SYS_LIBKERN_H_
33 #define _SYS_LIBKERN_H_
34
35 #include <sys/types.h>
36 #ifdef _KERNEL
37 #include <sys/systm.h>
38 #endif
39 #include <sys/kassert.h>
40
41 #ifndef LIBKERN_INLINE
42 #define LIBKERN_INLINE static __inline
43 #define LIBKERN_BODY
44 #endif
45
46 /* BCD conversions. */
47 extern u_char const bcd2bin_data[];
48 extern u_char const bin2bcd_data[];
49 extern char const hex2ascii_data[];
50
51 #define LIBKERN_LEN_BCD2BIN 154
52 #define LIBKERN_LEN_BIN2BCD 100
53 #define LIBKERN_LEN_HEX2ASCII 36
54
55 static inline u_char
bcd2bin(int bcd)56 bcd2bin(int bcd)
57 {
58
59 KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
60 ("invalid bcd %d", bcd));
61 return (bcd2bin_data[bcd]);
62 }
63
64 static inline u_char
bin2bcd(int bin)65 bin2bcd(int bin)
66 {
67
68 KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
69 ("invalid bin %d", bin));
70 return (bin2bcd_data[bin]);
71 }
72
73 static inline char
hex2ascii(int hex)74 hex2ascii(int hex)
75 {
76
77 KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
78 ("invalid hex %d", hex));
79 return (hex2ascii_data[hex]);
80 }
81
82 static inline bool
validbcd(int bcd)83 validbcd(int bcd)
84 {
85
86 return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
87 }
imax(int a,int b)88 static __inline int imax(int a, int b) { return (a > b ? a : b); }
imin(int a,int b)89 static __inline int imin(int a, int b) { return (a < b ? a : b); }
lmax(long a,long b)90 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
lmin(long a,long b)91 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
max(u_int a,u_int b)92 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
min(u_int a,u_int b)93 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
qmax(quad_t a,quad_t b)94 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
qmin(quad_t a,quad_t b)95 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
uqmax(u_quad_t a,u_quad_t b)96 static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
uqmin(u_quad_t a,u_quad_t b)97 static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
ulmax(u_long a,u_long b)98 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
ulmin(u_long a,u_long b)99 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
ummax(__uintmax_t a,__uintmax_t b)100 static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
101 {
102
103 return (a > b ? a : b);
104 }
ummin(__uintmax_t a,__uintmax_t b)105 static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
106 {
107
108 return (a < b ? a : b);
109 }
omax(off_t a,off_t b)110 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
omin(off_t a,off_t b)111 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
abs(int a)112 static __inline int abs(int a) { return (a < 0 ? -a : a); }
labs(long a)113 static __inline long labs(long a) { return (a < 0 ? -a : a); }
abs64(int64_t a)114 static __inline int64_t abs64(int64_t a) { return (a < 0 ? -a : a); }
qabs(quad_t a)115 static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
116
117 #ifndef RANDOM_FENESTRASX
118 #define ARC4_ENTR_NONE 0 /* Don't have entropy yet. */
119 #define ARC4_ENTR_HAVE 1 /* Have entropy. */
120 #define ARC4_ENTR_SEED 2 /* Reseeding. */
121 extern int arc4rand_iniseed_state;
122 #endif
123
124 /* Prototypes for non-quad routines. */
125 struct malloc_type;
126 uint32_t arc4random(void);
127 void arc4random_buf(void *, size_t);
128 uint32_t arc4random_uniform(uint32_t);
129 void arc4rand(void *, u_int, int);
130 int timingsafe_bcmp(const void *, const void *, size_t);
131 void *bsearch(const void *, const void *, size_t,
132 size_t, int (*)(const void *, const void *));
133
134 /*
135 * MHTODO: remove the 'HAVE_INLINE_FOO' defines once use of these flags has
136 * been purged everywhere. For now we provide them unconditionally.
137 */
138 #define HAVE_INLINE_FFS
139 #define HAVE_INLINE_FFSL
140 #define HAVE_INLINE_FFSLL
141 #define HAVE_INLINE_FLS
142 #define HAVE_INLINE_FLSL
143 #define HAVE_INLINE_FLSLL
144
145 static __inline __pure2 int
ffs(int mask)146 ffs(int mask)
147 {
148
149 return (__builtin_ffs((u_int)mask));
150 }
151
152 static __inline __pure2 int
ffsl(long mask)153 ffsl(long mask)
154 {
155
156 return (__builtin_ffsl((u_long)mask));
157 }
158
159 static __inline __pure2 int
ffsll(long long mask)160 ffsll(long long mask)
161 {
162
163 return (__builtin_ffsll((unsigned long long)mask));
164 }
165
166 static __inline __pure2 int
fls(int mask)167 fls(int mask)
168 {
169
170 return (mask == 0 ? 0 :
171 8 * sizeof(mask) - __builtin_clz((u_int)mask));
172 }
173
174 static __inline __pure2 int
flsl(long mask)175 flsl(long mask)
176 {
177
178 return (mask == 0 ? 0 :
179 8 * sizeof(mask) - __builtin_clzl((u_long)mask));
180 }
181
182 static __inline __pure2 int
flsll(long long mask)183 flsll(long long mask)
184 {
185
186 return (mask == 0 ? 0 :
187 8 * sizeof(mask) - __builtin_clzll((unsigned long long)mask));
188 }
189
190 static __inline __pure2 int
ilog2_int(int n)191 ilog2_int(int n)
192 {
193
194 KASSERT(n != 0, ("ilog argument must be nonzero"));
195 return (8 * sizeof(n) - 1 - __builtin_clz((u_int)n));
196 }
197
198 static __inline __pure2 int
ilog2_long(long n)199 ilog2_long(long n)
200 {
201
202 KASSERT(n != 0, ("ilog argument must be nonzero"));
203 return (8 * sizeof(n) - 1 - __builtin_clzl((u_long)n));
204 }
205
206 static __inline __pure2 int
ilog2_long_long(long long n)207 ilog2_long_long(long long n)
208 {
209
210 KASSERT(n != 0, ("ilog argument must be nonzero"));
211 return (8 * sizeof(n) - 1 -
212 __builtin_clzll((unsigned long long)n));
213 }
214
215 #define ilog2_var(n) \
216 _Generic((n), \
217 default: ilog2_int, \
218 long: ilog2_long, \
219 unsigned long: ilog2_long, \
220 long long: ilog2_long_long, \
221 unsigned long long: ilog2_long_long \
222 )(n)
223
224 #define ilog2(n) \
225 ( \
226 __builtin_constant_p(n) ? ( \
227 (n) < 1 ? -1 : \
228 (n) & (1ULL << 63) ? 63 : \
229 (n) & (1ULL << 62) ? 62 : \
230 (n) & (1ULL << 61) ? 61 : \
231 (n) & (1ULL << 60) ? 60 : \
232 (n) & (1ULL << 59) ? 59 : \
233 (n) & (1ULL << 58) ? 58 : \
234 (n) & (1ULL << 57) ? 57 : \
235 (n) & (1ULL << 56) ? 56 : \
236 (n) & (1ULL << 55) ? 55 : \
237 (n) & (1ULL << 54) ? 54 : \
238 (n) & (1ULL << 53) ? 53 : \
239 (n) & (1ULL << 52) ? 52 : \
240 (n) & (1ULL << 51) ? 51 : \
241 (n) & (1ULL << 50) ? 50 : \
242 (n) & (1ULL << 49) ? 49 : \
243 (n) & (1ULL << 48) ? 48 : \
244 (n) & (1ULL << 47) ? 47 : \
245 (n) & (1ULL << 46) ? 46 : \
246 (n) & (1ULL << 45) ? 45 : \
247 (n) & (1ULL << 44) ? 44 : \
248 (n) & (1ULL << 43) ? 43 : \
249 (n) & (1ULL << 42) ? 42 : \
250 (n) & (1ULL << 41) ? 41 : \
251 (n) & (1ULL << 40) ? 40 : \
252 (n) & (1ULL << 39) ? 39 : \
253 (n) & (1ULL << 38) ? 38 : \
254 (n) & (1ULL << 37) ? 37 : \
255 (n) & (1ULL << 36) ? 36 : \
256 (n) & (1ULL << 35) ? 35 : \
257 (n) & (1ULL << 34) ? 34 : \
258 (n) & (1ULL << 33) ? 33 : \
259 (n) & (1ULL << 32) ? 32 : \
260 (n) & (1ULL << 31) ? 31 : \
261 (n) & (1ULL << 30) ? 30 : \
262 (n) & (1ULL << 29) ? 29 : \
263 (n) & (1ULL << 28) ? 28 : \
264 (n) & (1ULL << 27) ? 27 : \
265 (n) & (1ULL << 26) ? 26 : \
266 (n) & (1ULL << 25) ? 25 : \
267 (n) & (1ULL << 24) ? 24 : \
268 (n) & (1ULL << 23) ? 23 : \
269 (n) & (1ULL << 22) ? 22 : \
270 (n) & (1ULL << 21) ? 21 : \
271 (n) & (1ULL << 20) ? 20 : \
272 (n) & (1ULL << 19) ? 19 : \
273 (n) & (1ULL << 18) ? 18 : \
274 (n) & (1ULL << 17) ? 17 : \
275 (n) & (1ULL << 16) ? 16 : \
276 (n) & (1ULL << 15) ? 15 : \
277 (n) & (1ULL << 14) ? 14 : \
278 (n) & (1ULL << 13) ? 13 : \
279 (n) & (1ULL << 12) ? 12 : \
280 (n) & (1ULL << 11) ? 11 : \
281 (n) & (1ULL << 10) ? 10 : \
282 (n) & (1ULL << 9) ? 9 : \
283 (n) & (1ULL << 8) ? 8 : \
284 (n) & (1ULL << 7) ? 7 : \
285 (n) & (1ULL << 6) ? 6 : \
286 (n) & (1ULL << 5) ? 5 : \
287 (n) & (1ULL << 4) ? 4 : \
288 (n) & (1ULL << 3) ? 3 : \
289 (n) & (1ULL << 2) ? 2 : \
290 (n) & (1ULL << 1) ? 1 : \
291 (n) & (1ULL << 0) ? 0 : \
292 -1) : \
293 ilog2_var(n) \
294 )
295
296 #define bitcount64(x) __bitcount64((uint64_t)(x))
297 #define bitcount32(x) __bitcount32((uint32_t)(x))
298 #define bitcount16(x) __bitcount16((uint16_t)(x))
299 #define bitcountl(x) __bitcountl((u_long)(x))
300 #define bitcount(x) __bitcount((u_int)(x))
301
302 int fnmatch(const char *, const char *, int);
303 int locc(int, char *, u_int);
304 void *memchr(const void *s, int c, size_t n);
305 void *memcchr(const void *s, int c, size_t n);
306 void *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
307 void qsort(void *base, size_t nmemb, size_t size,
308 int (*compar)(const void *, const void *));
309 void qsort_r(void *base, size_t nmemb, size_t size,
310 int (*compar)(const void *, const void *, void *), void *thunk);
311 u_long random(void);
312 int scanc(u_int, const u_char *, const u_char *, int);
313 int strcasecmp(const char *, const char *);
314 char *strcasestr(const char *, const char *);
315 char *strcat(char * __restrict, const char * __restrict);
316 char *strchr(const char *, int);
317 char *strchrnul(const char *, int);
318 int strcmp(const char *, const char *);
319 char *strcpy(char * __restrict, const char * __restrict);
320 char *strdup_flags(const char *__restrict, struct malloc_type *, int);
321 size_t strcspn(const char *, const char *) __pure;
322 char *strdup(const char *__restrict, struct malloc_type *);
323 char *strncat(char *, const char *, size_t);
324 char *strndup(const char *__restrict, size_t, struct malloc_type *);
325 size_t strlcat(char *, const char *, size_t);
326 size_t strlcpy(char *, const char *, size_t);
327 size_t strlen(const char *);
328 int strncasecmp(const char *, const char *, size_t);
329 int strncmp(const char *, const char *, size_t);
330 char *strncpy(char * __restrict, const char * __restrict, size_t);
331 size_t strnlen(const char *, size_t);
332 char *strnstr(const char *, const char *, size_t);
333 char *strrchr(const char *, int);
334 char *strsep(char **, const char *delim);
335 size_t strspn(const char *, const char *);
336 char *strstr(const char *, const char *);
337 int strvalid(const char *, size_t);
338
339 #ifdef SAN_NEEDS_INTERCEPTORS
340 #ifndef SAN_INTERCEPTOR
341 #define SAN_INTERCEPTOR(func) \
342 __CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
343 #endif
344 char *SAN_INTERCEPTOR(strcpy)(char *, const char *);
345 int SAN_INTERCEPTOR(strcmp)(const char *, const char *);
346 size_t SAN_INTERCEPTOR(strlen)(const char *);
347 #ifndef SAN_RUNTIME
348 #define strcpy(d, s) SAN_INTERCEPTOR(strcpy)((d), (s))
349 #define strcmp(s1, s2) SAN_INTERCEPTOR(strcmp)((s1), (s2))
350 #define strlen(s) SAN_INTERCEPTOR(strlen)(s)
351 #endif /* !SAN_RUNTIME */
352 #else /* !SAN_NEEDS_INTERCEPTORS */
353 #define strcpy(d, s) __builtin_strcpy((d), (s))
354 #define strcmp(s1, s2) __builtin_strcmp((s1), (s2))
355 #define strlen(s) __builtin_strlen((s))
356 #endif /* SAN_NEEDS_INTERCEPTORS */
357
358 static __inline char *
index(const char * p,int ch)359 index(const char *p, int ch)
360 {
361
362 return (strchr(p, ch));
363 }
364
365 static __inline char *
rindex(const char * p,int ch)366 rindex(const char *p, int ch)
367 {
368
369 return (strrchr(p, ch));
370 }
371
372 static __inline int64_t
signed_extend64(uint64_t bitmap,int lsb,int width)373 signed_extend64(uint64_t bitmap, int lsb, int width)
374 {
375
376 return ((int64_t)(bitmap << (63 - lsb - (width - 1)))) >>
377 (63 - (width - 1));
378 }
379
380 static __inline int32_t
signed_extend32(uint32_t bitmap,int lsb,int width)381 signed_extend32(uint32_t bitmap, int lsb, int width)
382 {
383
384 return ((int32_t)(bitmap << (31 - lsb - (width - 1)))) >>
385 (31 - (width - 1));
386 }
387
388 /* fnmatch() return values. */
389 #define FNM_NOMATCH 1 /* Match failed. */
390
391 /* fnmatch() flags. */
392 #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
393 #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
394 #define FNM_PERIOD 0x04 /* Period must be matched by period. */
395 #define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */
396 #define FNM_CASEFOLD 0x10 /* Case insensitive search. */
397 #define FNM_IGNORECASE FNM_CASEFOLD
398 #define FNM_FILE_NAME FNM_PATHNAME
399
400 #endif /* !_SYS_LIBKERN_H_ */
401