1 | /*- |
---|
2 | * Copyright (c) 1992, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 3. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)libkern.h 8.1 (Berkeley) 6/10/93 |
---|
30 | * $FreeBSD$ |
---|
31 | */ |
---|
32 | |
---|
33 | #ifndef _SYS_LIBKERN_H_ |
---|
34 | #define _SYS_LIBKERN_H_ |
---|
35 | |
---|
36 | #include <sys/cdefs.h> |
---|
37 | #include <sys/types.h> |
---|
38 | #ifdef _KERNEL |
---|
39 | #include <sys/systm.h> |
---|
40 | #endif |
---|
41 | |
---|
42 | #ifndef __rtems__ |
---|
43 | #ifndef LIBKERN_INLINE |
---|
44 | #define LIBKERN_INLINE static __inline |
---|
45 | #define LIBKERN_BODY |
---|
46 | #endif |
---|
47 | #else /* __rtems__ */ |
---|
48 | #define LIBKERN_INLINE |
---|
49 | #endif /* __rtems__ */ |
---|
50 | |
---|
51 | /* BCD conversions. */ |
---|
52 | extern u_char const bcd2bin_data[]; |
---|
53 | extern u_char const bin2bcd_data[]; |
---|
54 | extern char const hex2ascii_data[]; |
---|
55 | |
---|
56 | #define LIBKERN_LEN_BCD2BIN 154 |
---|
57 | #define LIBKERN_LEN_BIN2BCD 100 |
---|
58 | #define LIBKERN_LEN_HEX2ASCII 36 |
---|
59 | |
---|
60 | static inline u_char |
---|
61 | bcd2bin(int bcd) |
---|
62 | { |
---|
63 | |
---|
64 | KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN, |
---|
65 | ("invalid bcd %d", bcd)); |
---|
66 | return (bcd2bin_data[bcd]); |
---|
67 | } |
---|
68 | |
---|
69 | static inline u_char |
---|
70 | bin2bcd(int bin) |
---|
71 | { |
---|
72 | |
---|
73 | KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD, |
---|
74 | ("invalid bin %d", bin)); |
---|
75 | return (bin2bcd_data[bin]); |
---|
76 | } |
---|
77 | |
---|
78 | static inline char |
---|
79 | hex2ascii(int hex) |
---|
80 | { |
---|
81 | |
---|
82 | KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII, |
---|
83 | ("invalid hex %d", hex)); |
---|
84 | return (hex2ascii_data[hex]); |
---|
85 | } |
---|
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 | |
---|
112 | #ifndef __rtems__ |
---|
113 | static __inline int abs(int a) { return (a < 0 ? -a : a); } |
---|
114 | static __inline long labs(long a) { return (a < 0 ? -a : a); } |
---|
115 | #endif /* __rtems__ */ |
---|
116 | static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); } |
---|
117 | |
---|
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 | |
---|
123 | /* Prototypes for non-quad routines. */ |
---|
124 | struct malloc_type; |
---|
125 | uint32_t arc4random(void); |
---|
126 | #ifndef __rtems__ |
---|
127 | void arc4rand(void *ptr, u_int len, int reseed); |
---|
128 | #else /* __rtems__ */ |
---|
129 | void arc4random_buf(void *, size_t); |
---|
130 | |
---|
131 | static inline void |
---|
132 | arc4rand(void *ptr, u_int len, int reseed) |
---|
133 | { |
---|
134 | |
---|
135 | (void)reseed; |
---|
136 | arc4random_buf(ptr, len); |
---|
137 | } |
---|
138 | #endif /* __rtems__ */ |
---|
139 | #ifndef __rtems__ |
---|
140 | int bcmp(const void *, const void *, size_t); |
---|
141 | #else /* __rtems__ */ |
---|
142 | #define bcmp(m1, m2, n) memcmp(m1, m2, n) |
---|
143 | #endif /* __rtems__ */ |
---|
144 | int timingsafe_bcmp(const void *, const void *, size_t); |
---|
145 | void *bsearch(const void *, const void *, size_t, |
---|
146 | size_t, int (*)(const void *, const void *)); |
---|
147 | #ifndef __rtems__ |
---|
148 | #ifndef HAVE_INLINE_FFS |
---|
149 | int ffs(int); |
---|
150 | #endif |
---|
151 | #ifndef HAVE_INLINE_FFSL |
---|
152 | int ffsl(long); |
---|
153 | #endif |
---|
154 | #ifndef HAVE_INLINE_FFSLL |
---|
155 | int ffsll(long long); |
---|
156 | #endif |
---|
157 | #ifndef HAVE_INLINE_FLS |
---|
158 | int fls(int); |
---|
159 | #endif |
---|
160 | #ifndef HAVE_INLINE_FLSL |
---|
161 | int flsl(long); |
---|
162 | #endif |
---|
163 | #ifndef HAVE_INLINE_FLSLL |
---|
164 | int flsll(long long); |
---|
165 | #endif |
---|
166 | #else /* __rtems__ */ |
---|
167 | static inline int |
---|
168 | builtin_fls(int x) |
---|
169 | { |
---|
170 | |
---|
171 | return (x != 0 ? sizeof(x) * 8 - __builtin_clz((unsigned int)x) : 0); |
---|
172 | } |
---|
173 | |
---|
174 | static inline int |
---|
175 | builtin_flsl(long x) |
---|
176 | { |
---|
177 | |
---|
178 | return (x != 0 ? sizeof(x) * 8 - __builtin_clzl((unsigned long)x) : 0); |
---|
179 | } |
---|
180 | |
---|
181 | static inline int |
---|
182 | builtin_flsll(long long x) |
---|
183 | { |
---|
184 | |
---|
185 | return (x != 0 ? sizeof(x) * 8 - __builtin_clzll((unsigned long long)x) : 0); |
---|
186 | } |
---|
187 | |
---|
188 | #define ffs(_x) __builtin_ffs((unsigned int)(_x)) |
---|
189 | #define ffsl(_x) __builtin_ffsl((unsigned long)(_x)) |
---|
190 | #define ffsll(_x) __builtin_ffsll((unsigned long long)(_x)) |
---|
191 | #define fls(_x) builtin_fls(_x) |
---|
192 | #define flsl(_x) builtin_flsl(_x) |
---|
193 | #define flsll(_x) builtin_flsll(_x) |
---|
194 | #endif /* __rtems__ */ |
---|
195 | #define bitcount64(x) __bitcount64((uint64_t)(x)) |
---|
196 | #define bitcount32(x) __bitcount32((uint32_t)(x)) |
---|
197 | #define bitcount16(x) __bitcount16((uint16_t)(x)) |
---|
198 | #define bitcountl(x) __bitcountl((u_long)(x)) |
---|
199 | #define bitcount(x) __bitcount((u_int)(x)) |
---|
200 | |
---|
201 | int fnmatch(const char *, const char *, int); |
---|
202 | int locc(int, char *, u_int); |
---|
203 | void *memchr(const void *s, int c, size_t n); |
---|
204 | void *memcchr(const void *s, int c, size_t n); |
---|
205 | int memcmp(const void *b1, const void *b2, size_t len); |
---|
206 | void *memmem(const void *l, size_t l_len, const void *s, size_t s_len); |
---|
207 | void qsort(void *base, size_t nmemb, size_t size, |
---|
208 | int (*compar)(const void *, const void *)); |
---|
209 | void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, |
---|
210 | int (*compar)(void *, const void *, const void *)); |
---|
211 | #ifndef __rtems__ |
---|
212 | u_long random(void); |
---|
213 | #else /* __rtems__ */ |
---|
214 | #include <stdlib.h> |
---|
215 | u_long _bsd_random(void); |
---|
216 | #define random() _bsd_random() |
---|
217 | #endif /* __rtems__ */ |
---|
218 | int scanc(u_int, const u_char *, const u_char *, int); |
---|
219 | #ifndef __rtems__ |
---|
220 | void srandom(u_long); |
---|
221 | #else /* __rtems__ */ |
---|
222 | void _bsd_srandom(u_long); |
---|
223 | #define srandom(_x) _bsd_srandom(_x) |
---|
224 | #endif /* __rtems__ */ |
---|
225 | int strcasecmp(const char *, const char *); |
---|
226 | char *strcat(char * __restrict, const char * __restrict); |
---|
227 | char *strchr(const char *, int); |
---|
228 | int strcmp(const char *, const char *); |
---|
229 | char *strcpy(char * __restrict, const char * __restrict); |
---|
230 | size_t strcspn(const char * __restrict, const char * __restrict) __pure; |
---|
231 | #ifdef __rtems__ |
---|
232 | #include <string.h> |
---|
233 | #define strdup _bsd_strdup |
---|
234 | #define strndup _bsd_strndup |
---|
235 | #endif /* __rtems__ */ |
---|
236 | char *strdup(const char *__restrict, struct malloc_type *); |
---|
237 | char *strncat(char *, const char *, size_t); |
---|
238 | char *strndup(const char *__restrict, size_t, struct malloc_type *); |
---|
239 | size_t strlcat(char *, const char *, size_t); |
---|
240 | size_t strlcpy(char *, const char *, size_t); |
---|
241 | size_t strlen(const char *); |
---|
242 | int strncasecmp(const char *, const char *, size_t); |
---|
243 | int strncmp(const char *, const char *, size_t); |
---|
244 | char *strncpy(char * __restrict, const char * __restrict, size_t); |
---|
245 | size_t strnlen(const char *, size_t); |
---|
246 | char *strrchr(const char *, int); |
---|
247 | char *strsep(char **, const char *delim); |
---|
248 | size_t strspn(const char *, const char *); |
---|
249 | char *strstr(const char *, const char *); |
---|
250 | int strvalid(const char *, size_t); |
---|
251 | |
---|
252 | extern const uint32_t crc32_tab[]; |
---|
253 | |
---|
254 | static __inline uint32_t |
---|
255 | crc32_raw(const void *buf, size_t size, uint32_t crc) |
---|
256 | { |
---|
257 | const uint8_t *p = (const uint8_t *)buf; |
---|
258 | |
---|
259 | while (size--) |
---|
260 | crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); |
---|
261 | return (crc); |
---|
262 | } |
---|
263 | |
---|
264 | static __inline uint32_t |
---|
265 | crc32(const void *buf, size_t size) |
---|
266 | { |
---|
267 | uint32_t crc; |
---|
268 | |
---|
269 | crc = crc32_raw(buf, size, ~0U); |
---|
270 | return (crc ^ ~0U); |
---|
271 | } |
---|
272 | |
---|
273 | uint32_t |
---|
274 | calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, |
---|
275 | unsigned int length); |
---|
276 | #ifdef _KERNEL |
---|
277 | #if defined(__amd64__) || defined(__i386__) |
---|
278 | uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned); |
---|
279 | #endif |
---|
280 | #endif |
---|
281 | |
---|
282 | |
---|
283 | LIBKERN_INLINE void *memset(void *, int, size_t); |
---|
284 | #ifdef LIBKERN_BODY |
---|
285 | LIBKERN_INLINE void * |
---|
286 | memset(void *b, int c, size_t len) |
---|
287 | { |
---|
288 | char *bb; |
---|
289 | |
---|
290 | if (c == 0) |
---|
291 | bzero(b, len); |
---|
292 | else |
---|
293 | for (bb = (char *)b; len--; ) |
---|
294 | *bb++ = c; |
---|
295 | return (b); |
---|
296 | } |
---|
297 | #endif |
---|
298 | |
---|
299 | #ifndef __rtems__ |
---|
300 | static __inline char * |
---|
301 | index(const char *p, int ch) |
---|
302 | { |
---|
303 | |
---|
304 | return (strchr(p, ch)); |
---|
305 | } |
---|
306 | |
---|
307 | static __inline char * |
---|
308 | rindex(const char *p, int ch) |
---|
309 | { |
---|
310 | |
---|
311 | return (strrchr(p, ch)); |
---|
312 | } |
---|
313 | #endif /* __rtems__ */ |
---|
314 | |
---|
315 | /* fnmatch() return values. */ |
---|
316 | #define FNM_NOMATCH 1 /* Match failed. */ |
---|
317 | |
---|
318 | /* fnmatch() flags. */ |
---|
319 | #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ |
---|
320 | #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ |
---|
321 | #define FNM_PERIOD 0x04 /* Period must be matched by period. */ |
---|
322 | #define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */ |
---|
323 | #define FNM_CASEFOLD 0x10 /* Case insensitive search. */ |
---|
324 | #define FNM_IGNORECASE FNM_CASEFOLD |
---|
325 | #define FNM_FILE_NAME FNM_PATHNAME |
---|
326 | |
---|
327 | #endif /* !_SYS_LIBKERN_H_ */ |
---|