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