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