xref: /NextBSD/sys/sys/libkern.h (revision 4557fabb34e865d7f40be64b39c9e34fa41dbb60)
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  * 4. 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	LIBKERN_INLINE
43 #define	LIBKERN_INLINE  static __inline
44 #define	LIBKERN_BODY
45 #endif
46 
47 /* BCD conversions. */
48 extern u_char const	bcd2bin_data[];
49 extern u_char const	bin2bcd_data[];
50 extern char const	hex2ascii_data[];
51 
52 #define	bcd2bin(bcd)	(bcd2bin_data[bcd])
53 #define	bin2bcd(bin)	(bin2bcd_data[bin])
54 #define	hex2ascii(hex)	(hex2ascii_data[hex])
55 
imax(int a,int b)56 static __inline int imax(int a, int b) { return (a > b ? a : b); }
imin(int a,int b)57 static __inline int imin(int a, int b) { return (a < b ? a : b); }
lmax(long a,long b)58 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
lmin(long a,long b)59 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
max(u_int a,u_int b)60 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
min(u_int a,u_int b)61 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
qmax(quad_t a,quad_t b)62 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
qmin(quad_t a,quad_t b)63 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
uqmax(u_quad_t a,u_quad_t b)64 static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
uqmin(u_quad_t a,u_quad_t b)65 static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
ulmax(u_long a,u_long b)66 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
ulmin(u_long a,u_long b)67 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
omax(off_t a,off_t b)68 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
omin(off_t a,off_t b)69 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
70 
abs(int a)71 static __inline int abs(int a) { return (a < 0 ? -a : a); }
labs(long a)72 static __inline long labs(long a) { return (a < 0 ? -a : a); }
qabs(quad_t a)73 static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
74 
75 #define	ARC4_ENTR_NONE	0	/* Don't have entropy yet. */
76 #define	ARC4_ENTR_HAVE	1	/* Have entropy. */
77 #define	ARC4_ENTR_SEED	2	/* Reseeding. */
78 extern int arc4rand_iniseed_state;
79 
80 /* Prototypes for non-quad routines. */
81 struct malloc_type;
82 uint32_t arc4random(void);
83 void	 arc4rand(void *ptr, u_int len, int reseed);
84 int	 bcmp(const void *, const void *, size_t);
85 int	 timingsafe_bcmp(const void *, const void *, size_t);
86 void	*bsearch(const void *, const void *, size_t,
87 	    size_t, int (*)(const void *, const void *));
88 #ifndef	HAVE_INLINE_FFS
89 int	 ffs(int);
90 #endif
91 #ifndef	HAVE_INLINE_FFSL
92 int	 ffsl(long);
93 #endif
94 #ifndef	HAVE_INLINE_FFSLL
95 int	 ffsll(long long);
96 #endif
97 #ifndef	HAVE_INLINE_FLS
98 int	 fls(int);
99 #endif
100 #ifndef	HAVE_INLINE_FLSL
101 int	 flsl(long);
102 #endif
103 #ifndef	HAVE_INLINE_FLSLL
104 int	 flsll(long long);
105 #endif
106 #define	bitcount64(x)	__bitcount64((uint64_t)(x))
107 #define	bitcount32(x)	__bitcount32((uint32_t)(x))
108 #define	bitcount16(x)	__bitcount16((uint16_t)(x))
109 #define	bitcountl(x)	__bitcountl((u_long)(x))
110 #define	bitcount(x)	__bitcount((u_int)(x))
111 
112 int	 fnmatch(const char *, const char *, int);
113 int	 locc(int, char *, u_int);
114 void	*memchr(const void *s, int c, size_t n);
115 void	*memcchr(const void *s, int c, size_t n);
116 int	 memcmp(const void *b1, const void *b2, size_t len);
117 void	*memmem(const void *l, size_t l_len, const void *s, size_t s_len);
118 void	 qsort(void *base, size_t nmemb, size_t size,
119 	    int (*compar)(const void *, const void *));
120 void	 qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
121 	    int (*compar)(void *, const void *, const void *));
122 u_long	 random(void);
123 int	 scanc(u_int, const u_char *, const u_char *, int);
124 void	 srandom(u_long);
125 int	 strcasecmp(const char *, const char *);
126 char	*strcat(char * __restrict, const char * __restrict);
127 char	*strchr(const char *, int);
128 int	 strcmp(const char *, const char *);
129 char	*strcpy(char * __restrict, const char * __restrict);
130 size_t	 strcspn(const char * __restrict, const char * __restrict) __pure;
131 char	*strdup(const char *__restrict, struct malloc_type *);
132 char	*strndup(const char *__restrict, size_t, struct malloc_type *);
133 size_t	 strlcat(char *, const char *, size_t);
134 size_t	 strlcpy(char *, const char *, size_t);
135 size_t	 strlen(const char *);
136 int	 strncasecmp(const char *, const char *, size_t);
137 int	 strncmp(const char *, const char *, size_t);
138 char	*strncpy(char * __restrict, const char * __restrict, size_t);
139 size_t	 strnlen(const char *, size_t);
140 char	*strrchr(const char *, int);
141 char	*strsep(char **, const char *delim);
142 size_t	 strspn(const char *, const char *);
143 char	*strstr(const char *, const char *);
144 int	 strvalid(const char *, size_t);
145 
146 extern const uint32_t crc32_tab[];
147 
148 static __inline uint32_t
crc32_raw(const void * buf,size_t size,uint32_t crc)149 crc32_raw(const void *buf, size_t size, uint32_t crc)
150 {
151 	const uint8_t *p = (const uint8_t *)buf;
152 
153 	while (size--)
154 		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
155 	return (crc);
156 }
157 
158 static __inline uint32_t
crc32(const void * buf,size_t size)159 crc32(const void *buf, size_t size)
160 {
161 	uint32_t crc;
162 
163 	crc = crc32_raw(buf, size, ~0U);
164 	return (crc ^ ~0U);
165 }
166 
167 uint32_t
168 calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
169     unsigned int length);
170 
171 
172 LIBKERN_INLINE void *memset(void *, int, size_t);
173 #ifdef LIBKERN_BODY
174 LIBKERN_INLINE void *
memset(void * b,int c,size_t len)175 memset(void *b, int c, size_t len)
176 {
177 	char *bb;
178 
179 	if (c == 0)
180 		bzero(b, len);
181 	else
182 		for (bb = (char *)b; len--; )
183 			*bb++ = c;
184 	return (b);
185 }
186 #endif
187 
188 static __inline char *
index(const char * p,int ch)189 index(const char *p, int ch)
190 {
191 
192 	return (strchr(p, ch));
193 }
194 
195 static __inline char *
rindex(const char * p,int ch)196 rindex(const char *p, int ch)
197 {
198 
199 	return (strrchr(p, ch));
200 }
201 
202 /* fnmatch() return values. */
203 #define	FNM_NOMATCH	1	/* Match failed. */
204 
205 /* fnmatch() flags. */
206 #define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
207 #define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */
208 #define	FNM_PERIOD	0x04	/* Period must be matched by period. */
209 #define	FNM_LEADING_DIR	0x08	/* Ignore /<tail> after Imatch. */
210 #define	FNM_CASEFOLD	0x10	/* Case insensitive search. */
211 #define	FNM_IGNORECASE	FNM_CASEFOLD
212 #define	FNM_FILE_NAME	FNM_PATHNAME
213 
214 #endif /* !_SYS_LIBKERN_H_ */
215