util.h revision 1.38
1/* $OpenBSD: util.h,v 1.38 2023/01/29 20:30:21 gnezdo 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_show_objects(void); 63void _dl_arc4randombuf(void *, size_t); 64u_int32_t _dl_arc4random(void); 65ssize_t _dl_write(int fd, const char* buf, size_t len); 66char * _dl_dirname(const char *path); 67int _dl___realpath(const char *path, char *resolved); 68int _dl_uname(struct utsname *name); 69 70long _dl_strtol(const char *nptr, char **endptr, int base); 71 72__dead void _dl_oom(void); 73__dead void _dl_die(const char *, ...) __attribute__((format (printf, 1, 2))); 74#define _dl_diedie() _dl_thrkill(0, 9, NULL) 75__END_HIDDEN_DECLS 76 77#define _dl_round_page(x) \ 78 (((x) + ((1 << _MAX_PAGE_SHIFT) - 1)) & ~((1 << _MAX_PAGE_SHIFT) - 1)) 79 80#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 81 82/* 83 * The following functions are declared inline so they can 84 * be used before bootstrap linking has been finished. 85 */ 86static inline void * 87_dl_memset(void *dst, const int c, size_t n) 88{ 89 if (n != 0) { 90 char *d = dst; 91 92 do 93 *d++ = c; 94 while (--n != 0); 95 } 96 return (dst); 97} 98 99static inline void 100_dl_bcopy(const void *src, void *dest, int size) 101{ 102 unsigned const char *psrc = src; 103 unsigned char *pdest = dest; 104 int i; 105 106 for (i = 0; i < size; i++) 107 pdest[i] = psrc[i]; 108} 109 110static inline size_t 111_dl_strlcpy(char *dst, const char *src, size_t siz) 112{ 113 char *d = dst; 114 const char *s = src; 115 size_t n = siz; 116 117 /* Copy as many bytes as will fit */ 118 if (n != 0 && --n != 0) { 119 do { 120 if ((*d++ = *s++) == 0) 121 break; 122 } while (--n != 0); 123 } 124 125 /* Not enough room in dst, add NUL and traverse rest of src */ 126 if (n == 0) { 127 if (siz != 0) 128 *d = '\0'; /* NUL-terminate dst */ 129 while (*s++) 130 ; 131 } 132 133 return(s - src - 1); /* count does not include NUL */ 134} 135 136static inline int 137_dl_strncmp(const char *s1, const char *s2, size_t n) 138{ 139 if (n == 0) 140 return (0); 141 do { 142 if (*s1 != *s2++) 143 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 144 if (*s1++ == 0) 145 break; 146 } while (--n != 0); 147 return (0); 148} 149 150static inline int 151_dl_strcmp(const char *s1, const char *s2) 152{ 153 while (*s1 == *s2++) 154 if (*s1++ == 0) 155 return (0); 156 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 157} 158 159static inline const char * 160_dl_strchr(const char *p, const int ch) 161{ 162 for (;; ++p) { 163 if (*p == ch) 164 return((char *)p); 165 if (!*p) 166 return((char *)NULL); 167 } 168 /* NOTREACHED */ 169} 170 171static inline char * 172_dl_strrchr(const char *str, const int ch) 173{ 174 const char *p; 175 char *retval = NULL; 176 177 for (p = str; *p != '\0'; ++p) 178 if (*p == ch) 179 retval = (char *)p; 180 181 return retval; 182} 183 184static inline char * 185_dl_strstr(const char *s, const char *find) 186{ 187 char c, sc; 188 size_t len; 189 if ((c = *find++) != 0) { 190 len = _dl_strlen(find); 191 do { 192 do { 193 if ((sc = *s++) == 0) 194 return (NULL); 195 } while (sc != c); 196 } while (_dl_strncmp(s, find, len) != 0); 197 s--; 198 } 199 return ((char *)s); 200} 201 202static inline int 203_dl_isalnum(int c) 204{ 205 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); 206} 207 208#endif /*__DL_UTIL_H__*/ 209