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