11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1992, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
3050477Speter * $FreeBSD: releng/10.3/sys/sys/libkern.h 281538 2015-04-14 20:05:26Z jhb $
311541Srgrimes */
321541Srgrimes
3313459Sbde#ifndef _SYS_LIBKERN_H_
3413459Sbde#define	_SYS_LIBKERN_H_
3513459Sbde
3615311Sbde#include <sys/cdefs.h>
371541Srgrimes#include <sys/types.h>
3865401Speter#ifdef _KERNEL
3965393Speter#include <sys/systm.h>
4065401Speter#endif
411541Srgrimes
42180514Sobrien#ifndef	LIBKERN_INLINE
43180514Sobrien#define	LIBKERN_INLINE  static __inline
44180514Sobrien#define	LIBKERN_BODY
45180514Sobrien#endif
46180514Sobrien
4713459Sbde/* BCD conversions. */
4813459Sbdeextern u_char const	bcd2bin_data[];
4913459Sbdeextern u_char const	bin2bcd_data[];
5013459Sbdeextern char const	hex2ascii_data[];
5113459Sbde
5213459Sbde#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
5313459Sbde#define	bin2bcd(bin)	(bin2bcd_data[bin])
5413459Sbde#define	hex2ascii(hex)	(hex2ascii_data[hex])
5513459Sbde
568010Sbdestatic __inline int imax(int a, int b) { return (a > b ? a : b); }
578010Sbdestatic __inline int imin(int a, int b) { return (a < b ? a : b); }
588010Sbdestatic __inline long lmax(long a, long b) { return (a > b ? a : b); }
598010Sbdestatic __inline long lmin(long a, long b) { return (a < b ? a : b); }
608010Sbdestatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
618010Sbdestatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
628010Sbdestatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
638010Sbdestatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
648010Sbdestatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
658010Sbdestatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
66163913Sandrestatic __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
67163913Sandrestatic __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
681541Srgrimes
69109447Smdoddstatic __inline int abs(int a) { return (a < 0 ? -a : a); }
70109447Smdoddstatic __inline long labs(long a) { return (a < 0 ? -a : a); }
71109447Smdoddstatic __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
72109268Smdodd
73249631Sache#define	ARC4_ENTR_NONE	0	/* Don't have entropy yet. */
74249631Sache#define	ARC4_ENTR_HAVE	1	/* Have entropy. */
75249631Sache#define	ARC4_ENTR_SEED	2	/* Reseeding. */
76249631Sacheextern int arc4rand_iniseed_state;
77249631Sache
781541Srgrimes/* Prototypes for non-quad routines. */
79111506Srwatsonstruct malloc_type;
80104900Sphkuint32_t arc4random(void);
81104900Sphkvoid	 arc4rand(void *ptr, u_int len, int reseed);
8292719Salfredint	 bcmp(const void *, const void *, size_t);
8392719Salfredvoid	*bsearch(const void *, const void *, size_t,
8492719Salfred	    size_t, int (*)(const void *, const void *));
85124480Sdes#ifndef	HAVE_INLINE_FFS
8692719Salfredint	 ffs(int);
874477Sbde#endif
88124480Sdes#ifndef	HAVE_INLINE_FFSL
89124480Sdesint	 ffsl(long);
90124480Sdes#endif
9117385Swollman#ifndef	HAVE_INLINE_FLS
9292719Salfredint	 fls(int);
9317385Swollman#endif
94124480Sdes#ifndef	HAVE_INLINE_FLSL
95124480Sdesint	 flsl(long);
96124480Sdes#endif
97253719Salfred#ifndef	HAVE_INLINE_FLSLL
98253719Salfredint	 flsll(long long);
99253719Salfred#endif
100281538Sjhb#define	bitcount64(x)	__bitcount64((uint64_t)(x))
101281538Sjhb#define	bitcount32(x)	__bitcount32((uint32_t)(x))
102281538Sjhb#define	bitcount16(x)	__bitcount16((uint16_t)(x))
103281538Sjhb#define	bitcountl(x)	__bitcountl((u_long)(x))
104281538Sjhb#define	bitcount(x)	__bitcount((u_int)(x))
105253719Salfred
106104652Sddint	 fnmatch(const char *, const char *, int);
10792719Salfredint	 locc(int, char *, u_int);
108208751Srajvoid	*memchr(const void *s, int c, size_t n);
109229198Sedvoid	*memcchr(const void *s, int c, size_t n);
110183299Sobrienint	 memcmp(const void *b1, const void *b2, size_t len);
11192719Salfredvoid	 qsort(void *base, size_t nmemb, size_t size,
11293008Sbde	    int (*compar)(const void *, const void *));
113132228Sglebiusvoid	 qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
114132228Sglebius	    int (*compar)(void *, const void *, const void *));
11592719Salfredu_long	 random(void);
11692719Salfredint	 scanc(u_int, const u_char *, const u_char *, int);
11792719Salfredvoid	 srandom(u_long);
118148861Spjdint	 strcasecmp(const char *, const char *);
119110605Shsuchar	*strcat(char * __restrict, const char * __restrict);
120229366Sedchar	*strchr(const char *, int);
12192719Salfredint	 strcmp(const char *, const char *);
122110605Shsuchar	*strcpy(char * __restrict, const char * __restrict);
123181748Skmacysize_t	 strcspn(const char * __restrict, const char * __restrict) __pure;
124111506Srwatsonchar	*strdup(const char *__restrict, struct malloc_type *);
125270892Straszchar	*strndup(const char *__restrict, size_t, struct malloc_type *);
126102863Sbrookssize_t	 strlcat(char *, const char *, size_t);
127102863Sbrookssize_t	 strlcpy(char *, const char *, size_t);
12892719Salfredsize_t	 strlen(const char *);
129148861Spjdint	 strncasecmp(const char *, const char *, size_t);
13092719Salfredint	 strncmp(const char *, const char *, size_t);
131110605Shsuchar	*strncpy(char * __restrict, const char * __restrict, size_t);
132226029Sjkimsize_t	 strnlen(const char *, size_t);
133229366Sedchar	*strrchr(const char *, int);
134104799Srwatsonchar	*strsep(char **, const char *delim);
135141665Sglebiussize_t	 strspn(const char *, const char *);
136161243Spjdchar	*strstr(const char *, const char *);
13792719Salfredint	 strvalid(const char *, size_t);
13812170Sphk
139233517Smariusextern const uint32_t crc32_tab[];
14065371Sphk
141145609Smarcelstatic __inline uint32_t
142145609Smarcelcrc32_raw(const void *buf, size_t size, uint32_t crc)
143145609Smarcel{
144160424Sphk	const uint8_t *p = (const uint8_t *)buf;
145145609Smarcel
146145609Smarcel	while (size--)
147145609Smarcel		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
148145609Smarcel	return (crc);
149145609Smarcel}
150145609Smarcel
151145609Smarcelstatic __inline uint32_t
152145609Smarcelcrc32(const void *buf, size_t size)
153145609Smarcel{
154145609Smarcel	uint32_t crc;
155145609Smarcel
156145609Smarcel	crc = crc32_raw(buf, size, ~0U);
157145609Smarcel	return (crc ^ ~0U);
158145609Smarcel}
159145609Smarcel
160188605Srrsuint32_t
161233517Smariuscalculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
162233517Smarius    unsigned int length);
163188605Srrs
164188605Srrs
165180514SobrienLIBKERN_INLINE void *memset(void *, int, size_t);
166180514Sobrien#ifdef LIBKERN_BODY
167180514SobrienLIBKERN_INLINE void *
16865371Sphkmemset(void *b, int c, size_t len)
16965371Sphk{
17065371Sphk	char *bb;
17165371Sphk
17265371Sphk	if (c == 0)
17365371Sphk		bzero(b, len);
17465371Sphk	else
17584061Sluigi		for (bb = (char *)b; len--; )
17665371Sphk			*bb++ = c;
17765371Sphk	return (b);
17865371Sphk}
179180514Sobrien#endif
18065371Sphk
181168604Swkoszekstatic __inline char *
182229366Sedindex(const char *p, int ch)
183168604Swkoszek{
184229366Sed
185229366Sed	return (strchr(p, ch));
186168604Swkoszek}
187168604Swkoszek
188168604Swkoszekstatic __inline char *
189229366Sedrindex(const char *p, int ch)
190168604Swkoszek{
191229366Sed
192229366Sed	return (strrchr(p, ch));
193168604Swkoszek}
194168604Swkoszek
195104652Sdd/* fnmatch() return values. */
196104652Sdd#define	FNM_NOMATCH	1	/* Match failed. */
197104652Sdd
198104652Sdd/* fnmatch() flags. */
199104652Sdd#define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
200104652Sdd#define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */
201104652Sdd#define	FNM_PERIOD	0x04	/* Period must be matched by period. */
202104652Sdd#define	FNM_LEADING_DIR	0x08	/* Ignore /<tail> after Imatch. */
203104652Sdd#define	FNM_CASEFOLD	0x10	/* Case insensitive search. */
204104652Sdd#define	FNM_IGNORECASE	FNM_CASEFOLD
205104652Sdd#define	FNM_FILE_NAME	FNM_PATHNAME
206104652Sdd
20713459Sbde#endif /* !_SYS_LIBKERN_H_ */
208