util.h revision 1.37
1/*	$OpenBSD: util.h,v 1.37 2022/01/08 06:49:41 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_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/*
81 *	The following functions are declared inline so they can
82 *	be used before bootstrap linking has been finished.
83 */
84static inline void *
85_dl_memset(void *dst, const int c, size_t n)
86{
87	if (n != 0) {
88		char *d = dst;
89
90		do
91			*d++ = c;
92		while (--n != 0);
93	}
94	return (dst);
95}
96
97static inline void
98_dl_bcopy(const void *src, void *dest, int size)
99{
100	unsigned const char *psrc = src;
101	unsigned char *pdest = dest;
102	int i;
103
104	for (i = 0; i < size; i++)
105		pdest[i] = psrc[i];
106}
107
108static inline size_t
109_dl_strlcpy(char *dst, const char *src, size_t siz)
110{
111	char *d = dst;
112	const char *s = src;
113	size_t n = siz;
114
115	/* Copy as many bytes as will fit */
116	if (n != 0 && --n != 0) {
117		do {
118			if ((*d++ = *s++) == 0)
119				break;
120		} while (--n != 0);
121	}
122
123	/* Not enough room in dst, add NUL and traverse rest of src */
124	if (n == 0) {
125		if (siz != 0)
126			*d = '\0';		/* NUL-terminate dst */
127		while (*s++)
128			;
129	}
130
131	return(s - src - 1);	/* count does not include NUL */
132}
133
134static inline int
135_dl_strncmp(const char *s1, const char *s2, size_t n)
136{
137	if (n == 0)
138		return (0);
139	do {
140		if (*s1 != *s2++)
141			return (*(unsigned char *)s1 - *(unsigned char *)--s2);
142		if (*s1++ == 0)
143			break;
144	} while (--n != 0);
145	return (0);
146}
147
148static inline int
149_dl_strcmp(const char *s1, const char *s2)
150{
151	while (*s1 == *s2++)
152		if (*s1++ == 0)
153			return (0);
154	return (*(unsigned char *)s1 - *(unsigned char *)--s2);
155}
156
157static inline const char *
158_dl_strchr(const char *p, const int ch)
159{
160	for (;; ++p) {
161		if (*p == ch)
162			return((char *)p);
163		if (!*p)
164			return((char *)NULL);
165	}
166	/* NOTREACHED */
167}
168
169static inline char *
170_dl_strrchr(const char *str, const int ch)
171{
172	const char *p;
173	char *retval = NULL;
174
175	for (p = str; *p != '\0'; ++p)
176		if (*p == ch)
177			retval = (char *)p;
178
179	return retval;
180}
181
182static inline char *
183_dl_strstr(const char *s, const char *find)
184{
185	char c, sc;
186	size_t len;
187	if ((c = *find++) != 0) {
188		len = _dl_strlen(find);
189		do {
190			do {
191				if ((sc = *s++) == 0)
192					return (NULL);
193			} while (sc != c);
194		} while (_dl_strncmp(s, find, len) != 0);
195		s--;
196	}
197	return ((char *)s);
198}
199
200static inline int
201_dl_isalnum(int c)
202{
203	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
204}
205
206#endif /*__DL_UTIL_H__*/
207