jcs's openbsd hax
openbsd
at jcs 210 lines 5.3 kB view raw
1/* $OpenBSD: util.h,v 1.41 2024/05/17 06:11:54 deraadt 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 <sys/signal.h> 36#include <stdarg.h> 37#include <stddef.h> /* for NULL */ 38 39#define MAXIMUM(a,b) (((a)>(b))?(a):(b)) 40 41#ifndef __boot 42# if DO_CLEAN_BOOT 43# define __boot __attribute__((section(".boot.text"))) 44# define __boot_data __attribute__((section(".boot.data"))) 45# else 46# define __boot 47# define __boot_data 48# endif 49#endif 50 51__BEGIN_HIDDEN_DECLS 52void _dl_malloc_init(void) __boot; 53void *_dl_malloc(size_t size); 54void *_dl_calloc(size_t nmemb, const size_t size); 55void *_dl_realloc(void *, size_t size); 56void *_dl_reallocarray(void *, size_t nmemb, size_t size); 57void _dl_free(void *); 58void *_dl_aligned_alloc(size_t _alignment, size_t _size); 59char *_dl_strdup(const char *); 60size_t _dl_strlen(const char *); 61size_t _dl_strlcat(char *dst, const char *src, size_t siz); 62void _dl_printf(const char *fmt, ...); 63void _dl_vprintf(const char *fmt, va_list ap); 64void _dl_dprintf(int, const char *fmt, ...); 65void _dl_arc4randombuf(void *, size_t); 66u_int32_t _dl_arc4random(void); 67ssize_t _dl_write(int fd, const char* buf, size_t len); 68char * _dl_dirname(const char *path); 69int _dl___realpath(const char *path, char *resolved); 70int _dl_uname(struct utsname *name); 71 72long _dl_strtol(const char *nptr, char **endptr, int base); 73 74__dead void _dl_oom(void); 75__dead void _dl_die(const char *, ...) __attribute__((format (printf, 1, 2))); 76#define _dl_diedie() _dl_thrkill(0, SIGKILL, NULL) 77__END_HIDDEN_DECLS 78 79#define _dl_round_page(x) \ 80 (((x) + ((1 << _MAX_PAGE_SHIFT) - 1)) & ~((1 << _MAX_PAGE_SHIFT) - 1)) 81 82#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 83 84/* 85 * The following functions are declared inline so they can 86 * be used before bootstrap linking has been finished. 87 */ 88static inline void * 89_dl_memset(void *dst, const int c, size_t n) 90{ 91 if (n != 0) { 92 char *d = dst; 93 94 do 95 *d++ = c; 96 while (--n != 0); 97 } 98 return (dst); 99} 100 101static inline void 102_dl_bcopy(const void *src, void *dest, int size) 103{ 104 unsigned const char *psrc = src; 105 unsigned char *pdest = dest; 106 int i; 107 108 for (i = 0; i < size; i++) 109 pdest[i] = psrc[i]; 110} 111 112static inline size_t 113_dl_strlcpy(char *dst, const char *src, size_t siz) 114{ 115 char *d = dst; 116 const char *s = src; 117 size_t n = siz; 118 119 /* Copy as many bytes as will fit */ 120 if (n != 0 && --n != 0) { 121 do { 122 if ((*d++ = *s++) == 0) 123 break; 124 } while (--n != 0); 125 } 126 127 /* Not enough room in dst, add NUL and traverse rest of src */ 128 if (n == 0) { 129 if (siz != 0) 130 *d = '\0'; /* NUL-terminate dst */ 131 while (*s++) 132 ; 133 } 134 135 return(s - src - 1); /* count does not include NUL */ 136} 137 138static inline int 139_dl_strncmp(const char *s1, const char *s2, size_t n) 140{ 141 if (n == 0) 142 return (0); 143 do { 144 if (*s1 != *s2++) 145 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 146 if (*s1++ == 0) 147 break; 148 } while (--n != 0); 149 return (0); 150} 151 152static inline int 153_dl_strcmp(const char *s1, const char *s2) 154{ 155 while (*s1 == *s2++) 156 if (*s1++ == 0) 157 return (0); 158 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 159} 160 161static inline const char * 162_dl_strchr(const char *p, const int ch) 163{ 164 for (;; ++p) { 165 if (*p == ch) 166 return((char *)p); 167 if (!*p) 168 return((char *)NULL); 169 } 170 /* NOTREACHED */ 171} 172 173static inline char * 174_dl_strrchr(const char *str, const int ch) 175{ 176 const char *p; 177 char *retval = NULL; 178 179 for (p = str; *p != '\0'; ++p) 180 if (*p == ch) 181 retval = (char *)p; 182 183 return retval; 184} 185 186static inline char * 187_dl_strstr(const char *s, const char *find) 188{ 189 char c, sc; 190 size_t len; 191 if ((c = *find++) != 0) { 192 len = _dl_strlen(find); 193 do { 194 do { 195 if ((sc = *s++) == 0) 196 return (NULL); 197 } while (sc != c); 198 } while (_dl_strncmp(s, find, len) != 0); 199 s--; 200 } 201 return ((char *)s); 202} 203 204static inline int 205_dl_isalnum(int c) 206{ 207 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); 208} 209 210#endif /*__DL_UTIL_H__*/