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