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