xref: /freebsd/sys/sys/libkern.h (revision 19261079)
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  * $FreeBSD$
33  */
34 
35 #ifndef _SYS_LIBKERN_H_
36 #define	_SYS_LIBKERN_H_
37 
38 #include <sys/cdefs.h>
39 #include <sys/types.h>
40 #ifdef _KERNEL
41 #include <sys/systm.h>
42 #endif
43 
44 #ifndef	LIBKERN_INLINE
45 #define	LIBKERN_INLINE  static __inline
46 #define	LIBKERN_BODY
47 #endif
48 
49 /* BCD conversions. */
50 extern u_char const	bcd2bin_data[];
51 extern u_char const	bin2bcd_data[];
52 extern char const	hex2ascii_data[];
53 
54 #define	LIBKERN_LEN_BCD2BIN	154
55 #define	LIBKERN_LEN_BIN2BCD	100
56 #define	LIBKERN_LEN_HEX2ASCII	36
57 
58 static inline u_char
59 bcd2bin(int bcd)
60 {
61 
62 	KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
63 	    ("invalid bcd %d", bcd));
64 	return (bcd2bin_data[bcd]);
65 }
66 
67 static inline u_char
68 bin2bcd(int bin)
69 {
70 
71 	KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
72 	    ("invalid bin %d", bin));
73 	return (bin2bcd_data[bin]);
74 }
75 
76 static inline char
77 hex2ascii(int hex)
78 {
79 
80 	KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
81 	    ("invalid hex %d", hex));
82 	return (hex2ascii_data[hex]);
83 }
84 
85 static inline bool
86 validbcd(int bcd)
87 {
88 
89 	return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
90 }
91 static __inline int imax(int a, int b) { return (a > b ? a : b); }
92 static __inline int imin(int a, int b) { return (a < b ? a : b); }
93 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
94 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
95 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
96 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
97 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
98 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
99 static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
100 static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
101 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
102 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
103 static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
104 {
105 
106 	return (a > b ? a : b);
107 }
108 static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
109 {
110 
111 	return (a < b ? a : b);
112 }
113 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
114 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
115 static __inline int abs(int a) { return (a < 0 ? -a : a); }
116 static __inline long labs(long a) { return (a < 0 ? -a : a); }
117 static __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. */
123 extern int arc4rand_iniseed_state;
124 #endif
125 
126 /* Prototypes for non-quad routines. */
127 struct malloc_type;
128 uint32_t arc4random(void);
129 void	 arc4random_buf(void *, size_t);
130 uint32_t arc4random_uniform(uint32_t);
131 void	 arc4rand(void *, u_int, int);
132 int	 timingsafe_bcmp(const void *, const void *, size_t);
133 void	*bsearch(const void *, const void *, size_t,
134 	    size_t, int (*)(const void *, const void *));
135 #ifndef	HAVE_INLINE_FFS
136 int	 ffs(int);
137 #endif
138 #ifndef	HAVE_INLINE_FFSL
139 int	 ffsl(long);
140 #endif
141 #ifndef	HAVE_INLINE_FFSLL
142 int	 ffsll(long long);
143 #endif
144 #ifndef	HAVE_INLINE_FLS
145 int	 fls(int);
146 #endif
147 #ifndef	HAVE_INLINE_FLSL
148 int	 flsl(long);
149 #endif
150 #ifndef	HAVE_INLINE_FLSLL
151 int	 flsll(long long);
152 #endif
153 #define	bitcount64(x)	__bitcount64((uint64_t)(x))
154 #define	bitcount32(x)	__bitcount32((uint32_t)(x))
155 #define	bitcount16(x)	__bitcount16((uint16_t)(x))
156 #define	bitcountl(x)	__bitcountl((u_long)(x))
157 #define	bitcount(x)	__bitcount((u_int)(x))
158 
159 int	 fnmatch(const char *, const char *, int);
160 int	 locc(int, char *, u_int);
161 void	*memchr(const void *s, int c, size_t n);
162 void	*memcchr(const void *s, int c, size_t n);
163 void	*memmem(const void *l, size_t l_len, const void *s, size_t s_len);
164 void	 qsort(void *base, size_t nmemb, size_t size,
165 	    int (*compar)(const void *, const void *));
166 void	 qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
167 	    int (*compar)(void *, const void *, const void *));
168 u_long	 random(void);
169 int	 scanc(u_int, const u_char *, const u_char *, int);
170 int	 strcasecmp(const char *, const char *);
171 char	*strcasestr(const char *, const char *);
172 char	*strcat(char * __restrict, const char * __restrict);
173 char	*strchr(const char *, int);
174 char	*strchrnul(const char *, int);
175 int	 strcmp(const char *, const char *);
176 char	*strcpy(char * __restrict, const char * __restrict);
177 char	*strdup_flags(const char *__restrict, struct malloc_type *, int);
178 size_t	 strcspn(const char *, const char *) __pure;
179 char	*strdup(const char *__restrict, struct malloc_type *);
180 char	*strncat(char *, const char *, size_t);
181 char	*strndup(const char *__restrict, size_t, struct malloc_type *);
182 size_t	 strlcat(char *, const char *, size_t);
183 size_t	 strlcpy(char *, const char *, size_t);
184 size_t	 strlen(const char *);
185 int	 strncasecmp(const char *, const char *, size_t);
186 int	 strncmp(const char *, const char *, size_t);
187 char	*strncpy(char * __restrict, const char * __restrict, size_t);
188 size_t	 strnlen(const char *, size_t);
189 char	*strnstr(const char *, const char *, size_t);
190 char	*strrchr(const char *, int);
191 char	*strsep(char **, const char *delim);
192 size_t	 strspn(const char *, const char *);
193 char	*strstr(const char *, const char *);
194 int	 strvalid(const char *, size_t);
195 
196 #ifdef SAN_NEEDS_INTERCEPTORS
197 #ifndef SAN_INTERCEPTOR
198 #define	SAN_INTERCEPTOR(func)	\
199 	__CONCAT(SAN_INTERCEPTOR_PREFIX, __CONCAT(_, func))
200 #endif
201 char	*SAN_INTERCEPTOR(strcpy)(char *, const char *);
202 int	SAN_INTERCEPTOR(strcmp)(const char *, const char *);
203 size_t	SAN_INTERCEPTOR(strlen)(const char *);
204 #ifndef SAN_RUNTIME
205 #define	strcpy(d, s)	SAN_INTERCEPTOR(strcpy)((d), (s))
206 #define	strcmp(s1, s2)	SAN_INTERCEPTOR(strcmp)((s1), (s2))
207 #define	strlen(s)	SAN_INTERCEPTOR(strlen)(s)
208 #endif /* !SAN_RUNTIME */
209 #else /* !SAN_NEEDS_INTERCEPTORS */
210 #define strcpy(d, s)	__builtin_strcpy((d), (s))
211 #define strcmp(s1, s2)	__builtin_strcmp((s1), (s2))
212 #define strlen(s)	__builtin_strlen((s))
213 #endif /* SAN_NEEDS_INTERCEPTORS */
214 
215 static __inline char *
216 index(const char *p, int ch)
217 {
218 
219 	return (strchr(p, ch));
220 }
221 
222 static __inline char *
223 rindex(const char *p, int ch)
224 {
225 
226 	return (strrchr(p, ch));
227 }
228 
229 /* fnmatch() return values. */
230 #define	FNM_NOMATCH	1	/* Match failed. */
231 
232 /* fnmatch() flags. */
233 #define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
234 #define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */
235 #define	FNM_PERIOD	0x04	/* Period must be matched by period. */
236 #define	FNM_LEADING_DIR	0x08	/* Ignore /<tail> after Imatch. */
237 #define	FNM_CASEFOLD	0x10	/* Case insensitive search. */
238 #define	FNM_IGNORECASE	FNM_CASEFOLD
239 #define	FNM_FILE_NAME	FNM_PATHNAME
240 
241 #endif /* !_SYS_LIBKERN_H_ */
242