libkern.h revision 319404
1330449Seadler/*- 2330449Seadler * Copyright (c) 1992, 1993 3330449Seadler * The Regents of the University of California. All rights reserved. 465226Sgreen * 565226Sgreen * Redistribution and use in source and binary forms, with or without 665226Sgreen * modification, are permitted provided that the following conditions 765226Sgreen * are met: 865226Sgreen * 1. Redistributions of source code must retain the above copyright 965226Sgreen * notice, this list of conditions and the following disclaimer. 1065226Sgreen * 2. Redistributions in binary form must reproduce the above copyright 1165226Sgreen * notice, this list of conditions and the following disclaimer in the 1265226Sgreen * documentation and/or other materials provided with the distribution. 1365226Sgreen * 4. Neither the name of the University nor the names of its contributors 1465226Sgreen * may be used to endorse or promote products derived from this software 1565226Sgreen * without specific prior written permission. 1665226Sgreen * 1765226Sgreen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1865226Sgreen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1965226Sgreen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2065226Sgreen * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2165226Sgreen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2265226Sgreen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2365226Sgreen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2465226Sgreen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2565226Sgreen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2665226Sgreen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2765226Sgreen * SUCH DAMAGE. 2865226Sgreen * 2987678Smarkm * @(#)libkern.h 8.1 (Berkeley) 6/10/93 3087678Smarkm * $FreeBSD: stable/11/sys/sys/libkern.h 319404 2017-06-01 09:00:38Z tuexen $ 3187678Smarkm */ 3287678Smarkm 33320721Sngie#ifndef _SYS_LIBKERN_H_ 3465226Sgreen#define _SYS_LIBKERN_H_ 3565226Sgreen 3665226Sgreen#include <sys/cdefs.h> 3765226Sgreen#include <sys/types.h> 3865226Sgreen#ifdef _KERNEL 3965226Sgreen#include <sys/systm.h> 4065226Sgreen#endif 4165226Sgreen 4265226Sgreen#ifndef LIBKERN_INLINE 43318610Sngie#define LIBKERN_INLINE static __inline 44227245Sed#define LIBKERN_BODY 45227245Sed#endif 4665226Sgreen 4765226Sgreen/* BCD conversions. */ 4865226Sgreenextern u_char const bcd2bin_data[]; 4965226Sgreenextern u_char const bin2bcd_data[]; 5065226Sgreenextern char const hex2ascii_data[]; 5165226Sgreen 5265226Sgreen#define bcd2bin(bcd) (bcd2bin_data[bcd]) 5365226Sgreen#define bin2bcd(bin) (bin2bcd_data[bin]) 5465226Sgreen#define hex2ascii(hex) (hex2ascii_data[hex]) 5565226Sgreen 5665226Sgreenstatic __inline int imax(int a, int b) { return (a > b ? a : b); } 5765226Sgreenstatic __inline int imin(int a, int b) { return (a < b ? a : b); } 5865226Sgreenstatic __inline long lmax(long a, long b) { return (a > b ? a : b); } 5965226Sgreenstatic __inline long lmin(long a, long b) { return (a < b ? a : b); } 6065226Sgreenstatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); } 6165226Sgreenstatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); } 6265226Sgreenstatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); } 6365226Sgreenstatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); } 6465226Sgreenstatic __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); } 6565226Sgreenstatic __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); } 6665226Sgreenstatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); } 6765226Sgreenstatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); } 6865226Sgreenstatic __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b) 6965226Sgreen{ 7065226Sgreen 7165226Sgreen return (a > b ? a : b); 7265226Sgreen} 73141656Srustatic __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b) 74141656Sru{ 75141656Sru 7665226Sgreen return (a < b ? a : b); 7765226Sgreen} 7865226Sgreenstatic __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); } 7965226Sgreenstatic __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); } 8065226Sgreen 8165226Sgreenstatic __inline int abs(int a) { return (a < 0 ? -a : a); } 8265226Sgreenstatic __inline long labs(long a) { return (a < 0 ? -a : a); } 8365226Sgreenstatic __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); } 8465226Sgreen 85318610Sngie#define ARC4_ENTR_NONE 0 /* Don't have entropy yet. */ 8691945Swosch#define ARC4_ENTR_HAVE 1 /* Have entropy. */ 8765226Sgreen#define ARC4_ENTR_SEED 2 /* Reseeding. */ 8865226Sgreenextern int arc4rand_iniseed_state; 8965226Sgreen 9065226Sgreen/* Prototypes for non-quad routines. */ 9165226Sgreenstruct malloc_type; 9265226Sgreenuint32_t arc4random(void); 9365226Sgreenvoid arc4rand(void *ptr, u_int len, int reseed); 9465226Sgreenint bcmp(const void *, const void *, size_t); 9565226Sgreenint timingsafe_bcmp(const void *, const void *, size_t); 9665226Sgreenvoid *bsearch(const void *, const void *, size_t, 9765226Sgreen size_t, int (*)(const void *, const void *)); 9865226Sgreen#ifndef HAVE_INLINE_FFS 9965226Sgreenint ffs(int); 10065226Sgreen#endif 101141656Sru#ifndef HAVE_INLINE_FFSL 102141656Sruint ffsl(long); 10365226Sgreen#endif 10465226Sgreen#ifndef HAVE_INLINE_FFSLL 10565226Sgreenint ffsll(long long); 10665226Sgreen#endif 10765226Sgreen#ifndef HAVE_INLINE_FLS 10865226Sgreenint fls(int); 10965226Sgreen#endif 11065226Sgreen#ifndef HAVE_INLINE_FLSL 11165226Sgreenint flsl(long); 11265226Sgreen#endif 11365226Sgreen#ifndef HAVE_INLINE_FLSLL 11465226Sgreenint flsll(long long); 11565226Sgreen#endif 11665226Sgreen#define bitcount64(x) __bitcount64((uint64_t)(x)) 11765226Sgreen#define bitcount32(x) __bitcount32((uint32_t)(x)) 11865226Sgreen#define bitcount16(x) __bitcount16((uint16_t)(x)) 11965226Sgreen#define bitcountl(x) __bitcountl((u_long)(x)) 12065226Sgreen#define bitcount(x) __bitcount((u_int)(x)) 12165226Sgreen 12265226Sgreenint fnmatch(const char *, const char *, int); 12365226Sgreenint locc(int, char *, u_int); 12465226Sgreenvoid *memchr(const void *s, int c, size_t n); 12565226Sgreenvoid *memcchr(const void *s, int c, size_t n); 12665226Sgreenint memcmp(const void *b1, const void *b2, size_t len); 12799118Stjrvoid *memmem(const void *l, size_t l_len, const void *s, size_t s_len); 12865226Sgreenvoid qsort(void *base, size_t nmemb, size_t size, 12965226Sgreen int (*compar)(const void *, const void *)); 13099120Stjrvoid qsort_r(void *base, size_t nmemb, size_t size, void *thunk, 13165226Sgreen int (*compar)(void *, const void *, const void *)); 13265226Sgreenu_long random(void); 13365226Sgreenint scanc(u_int, const u_char *, const u_char *, int); 13499118Stjrvoid srandom(u_long); 13599118Stjrint strcasecmp(const char *, const char *); 13665226Sgreenchar *strcat(char * __restrict, const char * __restrict); 13787678Smarkmchar *strchr(const char *, int); 13865226Sgreenint strcmp(const char *, const char *); 13965226Sgreenchar *strcpy(char * __restrict, const char * __restrict); 14065226Sgreensize_t strcspn(const char * __restrict, const char * __restrict) __pure; 14165226Sgreenchar *strdup(const char *__restrict, struct malloc_type *); 14265226Sgreenchar *strncat(char *, const char *, size_t); 14365226Sgreenchar *strndup(const char *__restrict, size_t, struct malloc_type *); 14465226Sgreensize_t strlcat(char *, const char *, size_t); 14565226Sgreensize_t strlcpy(char *, const char *, size_t); 14665226Sgreensize_t strlen(const char *); 14765226Sgreenint strncasecmp(const char *, const char *, size_t); 148int strncmp(const char *, const char *, size_t); 149char *strncpy(char * __restrict, const char * __restrict, size_t); 150size_t strnlen(const char *, size_t); 151char *strrchr(const char *, int); 152char *strsep(char **, const char *delim); 153size_t strspn(const char *, const char *); 154char *strstr(const char *, const char *); 155int strvalid(const char *, size_t); 156 157extern const uint32_t crc32_tab[]; 158 159static __inline uint32_t 160crc32_raw(const void *buf, size_t size, uint32_t crc) 161{ 162 const uint8_t *p = (const uint8_t *)buf; 163 164 while (size--) 165 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 166 return (crc); 167} 168 169static __inline uint32_t 170crc32(const void *buf, size_t size) 171{ 172 uint32_t crc; 173 174 crc = crc32_raw(buf, size, ~0U); 175 return (crc ^ ~0U); 176} 177 178uint32_t 179calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, 180 unsigned int length); 181#ifdef _KERNEL 182#if defined(__amd64__) || defined(__i386__) 183uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned); 184#endif 185#if defined(__aarch64__) 186uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned int); 187#endif 188#endif 189 190 191LIBKERN_INLINE void *memset(void *, int, size_t); 192#ifdef LIBKERN_BODY 193LIBKERN_INLINE void * 194memset(void *b, int c, size_t len) 195{ 196 char *bb; 197 198 if (c == 0) 199 bzero(b, len); 200 else 201 for (bb = (char *)b; len--; ) 202 *bb++ = c; 203 return (b); 204} 205#endif 206 207static __inline char * 208index(const char *p, int ch) 209{ 210 211 return (strchr(p, ch)); 212} 213 214static __inline char * 215rindex(const char *p, int ch) 216{ 217 218 return (strrchr(p, ch)); 219} 220 221/* fnmatch() return values. */ 222#define FNM_NOMATCH 1 /* Match failed. */ 223 224/* fnmatch() flags. */ 225#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ 226#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ 227#define FNM_PERIOD 0x04 /* Period must be matched by period. */ 228#define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */ 229#define FNM_CASEFOLD 0x10 /* Case insensitive search. */ 230#define FNM_IGNORECASE FNM_CASEFOLD 231#define FNM_FILE_NAME FNM_PATHNAME 232 233#endif /* !_SYS_LIBKERN_H_ */ 234