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