master
  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 *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
 32 */
 33
 34#ifndef _SYS_LIBKERN_H_
 35#define	_SYS_LIBKERN_H_
 36
 37#include <sys/cdefs.h>
 38#include <sys/types.h>
 39#ifdef _KERNEL
 40#include <sys/systm.h>
 41#endif
 42
 43#ifndef	LIBKERN_INLINE
 44#define	LIBKERN_INLINE  static __inline
 45#define	LIBKERN_BODY
 46#endif
 47
 48/* BCD conversions. */
 49extern u_char const	bcd2bin_data[];
 50extern u_char const	bin2bcd_data[];
 51extern char const	hex2ascii_data[];
 52
 53#define	LIBKERN_LEN_BCD2BIN	154
 54#define	LIBKERN_LEN_BIN2BCD	100
 55#define	LIBKERN_LEN_HEX2ASCII	36
 56
 57static inline u_char
 58bcd2bin(int bcd)
 59{
 60
 61	KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
 62	    ("invalid bcd %d", bcd));
 63	return (bcd2bin_data[bcd]);
 64}
 65
 66static inline u_char
 67bin2bcd(int bin)
 68{
 69
 70	KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
 71	    ("invalid bin %d", bin));
 72	return (bin2bcd_data[bin]);
 73}
 74
 75static inline char
 76hex2ascii(int hex)
 77{
 78
 79	KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
 80	    ("invalid hex %d", hex));
 81	return (hex2ascii_data[hex]);
 82}
 83
 84static inline bool
 85validbcd(int bcd)
 86{
 87
 88	return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
 89}
 90static __inline int imax(int a, int b) { return (a > b ? a : b); }
 91static __inline int imin(int a, int b) { return (a < b ? a : b); }
 92static __inline long lmax(long a, long b) { return (a > b ? a : b); }
 93static __inline long lmin(long a, long b) { return (a < b ? a : b); }
 94static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
 95static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
 96static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
 97static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
 98static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
 99static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
100static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
101static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
102static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
103{
104
105	return (a > b ? a : b);
106}
107static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
108{
109
110	return (a < b ? a : b);
111}
112static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
113static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
114static __inline int abs(int a) { return (a < 0 ? -a : a); }
115static __inline long labs(long a) { return (a < 0 ? -a : a); }
116static __inline int64_t abs64(int64_t a) { return (a < 0 ? -a : a); }
117static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
118
119#ifndef RANDOM_FENESTRASX
120#define	ARC4_ENTR_NONE	0	/* Don't have entropy yet. */
121#define	ARC4_ENTR_HAVE	1	/* Have entropy. */
122#define	ARC4_ENTR_SEED	2	/* Reseeding. */
123extern int arc4rand_iniseed_state;
124#endif
125
126/* Prototypes for non-quad routines. */
127struct malloc_type;
128uint32_t arc4random(void);
129void	 arc4random_buf(void *, size_t);
130uint32_t arc4random_uniform(uint32_t);
131void	 arc4rand(void *, u_int, int);
132int	 timingsafe_bcmp(const void *, const void *, size_t);
133void	*bsearch(const void *, const void *, size_t,
134	    size_t, int (*)(const void *, const void *));
135
136/*
137 * MHTODO: remove the 'HAVE_INLINE_FOO' defines once use of these flags has
138 * been purged everywhere. For now we provide them unconditionally.
139 */
140#define	HAVE_INLINE_FFS
141#define	HAVE_INLINE_FFSL
142#define	HAVE_INLINE_FFSLL
143#define	HAVE_INLINE_FLS
144#define	HAVE_INLINE_FLSL
145#define	HAVE_INLINE_FLSLL
146
147static __inline __pure2 int
148ffs(int mask)
149{
150
151	return (__builtin_ffs((u_int)mask));
152}
153
154static __inline __pure2 int
155ffsl(long mask)
156{
157
158	return (__builtin_ffsl((u_long)mask));
159}
160
161static __inline __pure2 int
162ffsll(long long mask)
163{
164
165	return (__builtin_ffsll((unsigned long long)mask));
166}
167
168static __inline __pure2 int
169fls(int mask)
170{
171
172	return (mask == 0 ? 0 :
173	    8 * sizeof(mask) - __builtin_clz((u_int)mask));
174}
175
176static __inline __pure2 int
177flsl(long mask)
178{
179
180	return (mask == 0 ? 0 :
181	    8 * sizeof(mask) - __builtin_clzl((u_long)mask));
182}
183
184static __inline __pure2 int
185flsll(long long mask)
186{
187
188	return (mask == 0 ? 0 :
189	    8 * sizeof(mask) - __builtin_clzll((unsigned long long)mask));
190}
191
192#define	bitcount64(x)	__bitcount64((uint64_t)(x))
193#define	bitcount32(x)	__bitcount32((uint32_t)(x))
194#define	bitcount16(x)	__bitcount16((uint16_t)(x))
195#define	bitcountl(x)	__bitcountl((u_long)(x))
196#define	bitcount(x)	__bitcount((u_int)(x))
197
198int	 fnmatch(const char *, const char *, int);
199int	 locc(int, char *, u_int);
200void	*memchr(const void *s, int c, size_t n);
201void	*memcchr(const void *s, int c, size_t n);
202void	*memmem(const void *l, size_t l_len, const void *s, size_t s_len);
203void	 qsort(void *base, size_t nmemb, size_t size,
204	    int (*compar)(const void *, const void *));
205void	 qsort_r(void *base, size_t nmemb, size_t size,
206	    int (*compar)(const void *, const void *, void *), void *thunk);
207u_long	 random(void);
208int	 scanc(u_int, const u_char *, const u_char *, int);
209int	 strcasecmp(const char *, const char *);
210char	*strcasestr(const char *, const char *);
211char	*strcat(char * __restrict, const char * __restrict);
212char	*strchr(const char *, int);
213char	*strchrnul(const char *, int);
214int	 strcmp(const char *, const char *);
215char	*strcpy(char * __restrict, const char * __restrict);
216char	*strdup_flags(const char *__restrict, struct malloc_type *, int);
217size_t	 strcspn(const char *, const char *) __pure;
218char	*strdup(const char *__restrict, struct malloc_type *);
219char	*strncat(char *, const char *, size_t);
220char	*strndup(const char *__restrict, size_t, struct malloc_type *);
221size_t	 strlcat(char *, const char *, size_t);
222size_t	 strlcpy(char *, const char *, size_t);
223size_t	 strlen(const char *);
224int	 strncasecmp(const char *, const char *, size_t);
225int	 strncmp(const char *, const char *, size_t);
226char	*strncpy(char * __restrict, const char * __restrict, size_t);
227size_t	 strnlen(const char *, size_t);
228char	*strnstr(const char *, const char *, size_t);
229char	*strrchr(const char *, int);
230char	*strsep(char **, const char *delim);
231size_t	 strspn(const char *, const char *);
232char	*strstr(const char *, const char *);
233int	 strvalid(const char *, size_t);
234
235#ifdef SAN_NEEDS_INTERCEPTORS
236#ifndef SAN_INTERCEPTOR
237#define	SAN_INTERCEPTOR(func)	\
238	__CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
239#endif
240char	*SAN_INTERCEPTOR(strcpy)(char *, const char *);
241int	SAN_INTERCEPTOR(strcmp)(const char *, const char *);
242size_t	SAN_INTERCEPTOR(strlen)(const char *);
243#ifndef SAN_RUNTIME
244#define	strcpy(d, s)	SAN_INTERCEPTOR(strcpy)((d), (s))
245#define	strcmp(s1, s2)	SAN_INTERCEPTOR(strcmp)((s1), (s2))
246#define	strlen(s)	SAN_INTERCEPTOR(strlen)(s)
247#endif /* !SAN_RUNTIME */
248#else /* !SAN_NEEDS_INTERCEPTORS */
249#define strcpy(d, s)	__builtin_strcpy((d), (s))
250#define strcmp(s1, s2)	__builtin_strcmp((s1), (s2))
251#define strlen(s)	__builtin_strlen((s))
252#endif /* SAN_NEEDS_INTERCEPTORS */
253
254static __inline char *
255index(const char *p, int ch)
256{
257
258	return (strchr(p, ch));
259}
260
261static __inline char *
262rindex(const char *p, int ch)
263{
264
265	return (strrchr(p, ch));
266}
267
268static __inline int64_t
269signed_extend64(uint64_t bitmap, int lsb, int width)
270{
271
272	return ((int64_t)(bitmap << (63 - lsb - (width - 1)))) >>
273	    (63 - (width - 1));
274}
275
276static __inline int32_t
277signed_extend32(uint32_t bitmap, int lsb, int width)
278{
279
280	return ((int32_t)(bitmap << (31 - lsb - (width - 1)))) >>
281	    (31 - (width - 1));
282}
283
284/* fnmatch() return values. */
285#define	FNM_NOMATCH	1	/* Match failed. */
286
287/* fnmatch() flags. */
288#define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
289#define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */
290#define	FNM_PERIOD	0x04	/* Period must be matched by period. */
291#define	FNM_LEADING_DIR	0x08	/* Ignore /<tail> after Imatch. */
292#define	FNM_CASEFOLD	0x10	/* Case insensitive search. */
293#define	FNM_IGNORECASE	FNM_CASEFOLD
294#define	FNM_FILE_NAME	FNM_PATHNAME
295
296#endif /* !_SYS_LIBKERN_H_ */