util.h revision 1.39
1/* $OpenBSD: util.h,v 1.39 2023/08/15 06:26:34 guenther Exp $ */ 2 3/* 4 * Copyright (c) 1998 Todd C. Miller <millert@openbsd.org> 5 * All rights reserved. 6 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * 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 */ 30 31#ifndef __DL_UTIL_H__ 32#define __DL_UTIL_H__ 33 34#include <sys/utsname.h> 35#include <stdarg.h> 36#include <stddef.h> /* for NULL */ 37 38#ifndef __boot 39# if DO_CLEAN_BOOT 40# define __boot __attribute__((section(".boot.text"))) 41# define __boot_data __attribute__((section(".boot.data"))) 42# else 43# define __boot 44# define __boot_data 45# endif 46#endif 47 48__BEGIN_HIDDEN_DECLS 49void _dl_malloc_init(void) __boot; 50void *_dl_malloc(size_t size); 51void *_dl_calloc(size_t nmemb, const size_t size); 52void *_dl_realloc(void *, size_t size); 53void *_dl_reallocarray(void *, size_t nmemb, size_t size); 54void _dl_free(void *); 55void *_dl_aligned_alloc(size_t _alignment, size_t _size); 56char *_dl_strdup(const char *); 57size_t _dl_strlen(const char *); 58size_t _dl_strlcat(char *dst, const char *src, size_t siz); 59void _dl_printf(const char *fmt, ...); 60void _dl_vprintf(const char *fmt, va_list ap); 61void _dl_dprintf(int, const char *fmt, ...); 62void _dl_arc4randombuf(void *, size_t); 63u_int32_t _dl_arc4random(void); 64ssize_t _dl_write(int fd, const char* buf, size_t len); 65char * _dl_dirname(const char *path); 66int _dl___realpath(const char *path, char *resolved); 67int _dl_uname(struct utsname *name); 68 69long _dl_strtol(const char *nptr, char **endptr, int base); 70 71__dead void _dl_oom(void); 72__dead void _dl_die(const char *, ...) __attribute__((format (printf, 1, 2))); 73#define _dl_diedie() _dl_thrkill(0, 9, NULL) 74__END_HIDDEN_DECLS 75 76#define _dl_round_page(x) \ 77 (((x) + ((1 << _MAX_PAGE_SHIFT) - 1)) & ~((1 << _MAX_PAGE_SHIFT) - 1)) 78 79#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 80 81/* 82 * The following functions are declared inline so they can 83 * be used before bootstrap linking has been finished. 84 */ 85static inline void * 86_dl_memset(void *dst, const int c, size_t n) 87{ 88 if (n != 0) { 89 char *d = dst; 90 91 do 92 *d++ = c; 93 while (--n != 0); 94 } 95 return (dst); 96} 97 98static inline void 99_dl_bcopy(const void *src, void *dest, int size) 100{ 101 unsigned const char *psrc = src; 102 unsigned char *pdest = dest; 103 int i; 104 105 for (i = 0; i < size; i++) 106 pdest[i] = psrc[i]; 107} 108 109static inline size_t 110_dl_strlcpy(char *dst, const char *src, size_t siz) 111{ 112 char *d = dst; 113 const char *s = src; 114 size_t n = siz; 115 116 /* Copy as many bytes as will fit */ 117 if (n != 0 && --n != 0) { 118 do { 119 if ((*d++ = *s++) == 0) 120 break; 121 } while (--n != 0); 122 } 123 124 /* Not enough room in dst, add NUL and traverse rest of src */ 125 if (n == 0) { 126 if (siz != 0) 127 *d = '\0'; /* NUL-terminate dst */ 128 while (*s++) 129 ; 130 } 131 132 return(s - src - 1); /* count does not include NUL */ 133} 134 135static inline int 136_dl_strncmp(const char *s1, const char *s2, size_t n) 137{ 138 if (n == 0) 139 return (0); 140 do { 141 if (*s1 != *s2++) 142 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 143 if (*s1++ == 0) 144 break; 145 } while (--n != 0); 146 return (0); 147} 148 149static inline int 150_dl_strcmp(const char *s1, const char *s2) 151{ 152 while (*s1 == *s2++) 153 if (*s1++ == 0) 154 return (0); 155 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 156} 157 158static inline const char * 159_dl_strchr(const char *p, const int ch) 160{ 161 for (;; ++p) { 162 if (*p == ch) 163 return((char *)p); 164 if (!*p) 165 return((char *)NULL); 166 } 167 /* NOTREACHED */ 168} 169 170static inline char * 171_dl_strrchr(const char *str, const int ch) 172{ 173 const char *p; 174 char *retval = NULL; 175 176 for (p = str; *p != '\0'; ++p) 177 if (*p == ch) 178 retval = (char *)p; 179 180 return retval; 181} 182 183static inline char * 184_dl_strstr(const char *s, const char *find) 185{ 186 char c, sc; 187 size_t len; 188 if ((c = *find++) != 0) { 189 len = _dl_strlen(find); 190 do { 191 do { 192 if ((sc = *s++) == 0) 193 return (NULL); 194 } while (sc != c); 195 } while (_dl_strncmp(s, find, len) != 0); 196 s--; 197 } 198 return ((char *)s); 199} 200 201static inline int 202_dl_isalnum(int c) 203{ 204 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); 205} 206 207#endif /*__DL_UTIL_H__*/ 208