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