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