1 /* $MirOS: src/kern/include/libckern.h,v 1.36 2014/03/29 10:25:53 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 2008, 2010, 2011, 2013, 2014
5  *	Thorsten Glaser <tg@mirbsd.org>
6  *
7  * Provided that these terms and disclaimer and all copyright notices
8  * are retained or reproduced in an accompanying document, permission
9  * is granted to deal in this work without restriction, including un-
10  * limited rights to use, publicly perform, distribute, sell, modify,
11  * merge, give away, or sublicence.
12  *
13  * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14  * the utmost extent permitted by applicable law, neither express nor
15  * implied; without malicious intent or gross negligence. In no event
16  * may a licensor, author or contributor be held liable for indirect,
17  * direct, other damage, loss, or other issues arising in any way out
18  * of dealing in the work, even if advised of the possibility of such
19  * damage or existence of a defect, except proven that it results out
20  * of said person's immediate fault when using the work as intended.
21  */
22 
23 #ifndef __LIBCKERN_H_
24 #define __LIBCKERN_H_
25 
26 #include <sys/types.h>
27 
28 #ifdef __MirBSD__
29 
30 #ifndef NULL
31 #ifdef __GNUG__
32 #define	NULL		__null
33 #elif defined(lint)
34 #define	NULL		0
35 #else
36 #define	NULL		((void *)((__PTRDIFF_TYPE__)0UL))
37 #endif
38 #endif
39 
40 #ifndef __IN_MKDEP
41 /* makedepend may not define the constants we are checking for */
42 #if __WCHAR_MAX__ != 65535U
43 #error GCC __WCHAR_MAX__ does not indicate UCS-2
44 #endif
45 #ifndef __STDC_ISO_10646__
46 #error This code assumes that wchar_t is UCS-2HE
47 #endif
48 #endif /* !__IN_MKDEP */
49 
50 #if !defined(_GCC_WCHAR_T) && !defined(__cplusplus)
51 #define	_GCC_WCHAR_T
52 typedef	__WCHAR_TYPE__	wchar_t;
53 #endif
54 
55 #if !defined(_GCC_WINT_T)
56 #define	_GCC_WINT_T
57 typedef	__WINT_TYPE__	wint_t;
58 #endif
59 
60 #if !defined(_GCC_MBSTATE_T)
61 #define	_GCC_MBSTATE_T
62 typedef struct {
63 	unsigned int count:2;
64 	unsigned int value:12;
65 } __attribute__((__packed__)) mbstate_t;
66 #endif
67 
68 /**
69  * An arcfour_status is hereby defined as carrying
70  * 212 octets (1696 bit) of entropic state, whereas
71  * S contains 210 octets and 3 or 4 additioinal bit,
72  * i is another 8 bit, and j adds enough to make up
73  * for the 4-5 bit of additional entropy we assume.
74  */
75 struct arcfour_status {
76 	uint8_t S[256];
77 	uint8_t i;
78 	uint8_t j;
79 };
80 
81 #undef WCHAR_MIN
82 #define WCHAR_MIN	0
83 #undef WCHAR_MAX
84 #define WCHAR_MAX	0xFFFDU
85 #undef WEOF
86 #define WEOF		0xFFFFU
87 
88 #ifndef EOF
89 #define EOF		(-1)
90 #endif
91 
92 #endif /* __MirBSD__ */
93 
94 __BEGIN_DECLS
95 void __main(void);
96 
97 void arc4random_roundhash(uint32_t *, uint8_t *, const void *, size_t)
98     __attribute__((__bounded__(__minbytes__, 1, 128)))
99     __attribute__((__bounded__(__buffer__, 3, 4)));
100 /* u_int32_t in the original API, but we pray they're the same */
101 uint32_t arc4random_uniform(uint32_t);
102 
103 /* arcfour: base cipher */
104 void arcfour_init(struct arcfour_status *);
105 void arcfour_ksa(struct arcfour_status *, const uint8_t *, size_t)
106     __attribute__((__bounded__(__buffer__, 2, 3)));
107 uint8_t arcfour_byte(struct arcfour_status *);
108 
109 int bcmp(const void *, const void *, size_t)
110     __attribute__((__bounded__(__buffer__, 1, 3)))
111     __attribute__((__bounded__(__buffer__, 2, 3)));
112 void bcopy(const void *, void *, size_t)
113     __attribute__((__bounded__(__buffer__, 1, 3)))
114     __attribute__((__bounded__(__buffer__, 2, 3)));
115 void bzero(void *, size_t)
116     __attribute__((__bounded__(__buffer__, 1, 2)));
117 void explicit_bzero(void *, size_t)
118     __attribute__((__bounded__(__buffer__, 1, 2)));
119 int timingsafe_bcmp(const void *, const void *, size_t)
120     __attribute__((__bounded__(__buffer__, 1, 3)))
121     __attribute__((__bounded__(__buffer__, 2, 3)));
122 
123 int ffs(int);
124 
125 void *memchr(const void *, int, size_t)
126     __attribute__((__bounded__(__buffer__, 1, 3)));
127 int memcmp(const void *, const void *, size_t)
128     __attribute__((__bounded__(__buffer__, 1, 3)))
129     __attribute__((__bounded__(__buffer__, 2, 3)));
130 void *memcpy(void *, const void *, size_t)
131     __attribute__((__bounded__(__buffer__, 1, 3)))
132     __attribute__((__bounded__(__buffer__, 2, 3)));
133 void memhexdump(const void *, size_t, size_t);
134 void *memmove(void *, const void *, size_t)
135     __attribute__((__bounded__(__buffer__, 1, 3)))
136     __attribute__((__bounded__(__buffer__, 2, 3)));
137 void *mempcpy(void *, const void *, size_t)
138     __attribute__((__bounded__(__buffer__, 1, 3)))
139     __attribute__((__bounded__(__buffer__, 2, 3)));
140 void *memset(void *, int, size_t)
141     __attribute__((__bounded__(__buffer__, 1, 3)));
142 
143 size_t optu16to8(char *, wchar_t, mbstate_t *)
144     __attribute__((__bounded__(__minbytes__, 1, 5)));
145 size_t optu8to16(wchar_t *, const char *, size_t, mbstate_t *)
146     __attribute__((__bounded__(__string__, 2, 3)));
147 
148 int strcasecmp(const char *, const char *);
149 char *strchr(const char *, int);
150 int strcmp(const char *, const char *);
151 int strcoll(const char *, const char *);
152 size_t strlcat(char *, const char *, size_t)
153     __attribute__((__bounded__(__string__, 1, 3)));
154 size_t strlcpy(char *, const char *, size_t)
155     __attribute__((__bounded__(__string__, 1, 3)));
156 size_t strlen(const char *);
157 int strncasecmp(const char *, const char *, size_t);
158 int strncmp(const char *, const char *, size_t);
159 char *strncpy(char *, const char *, size_t)
160     __attribute__((__bounded__(__string__, 1, 3)));
161 char *strrchr(const char *, int);
162 size_t strxfrm(char *, const char *, size_t)
163     __attribute__((__bounded__(__string__, 1, 3)));
164 
165 #ifdef _KERN_HOSTED
166 size_t wcrtomb(char *, wchar_t, mbstate_t *)
167     __attribute__((__bounded__(__minbytes__, 1, 5)));
168 #endif
169 int wcscasecmp(const wchar_t *, const wchar_t *);
170 int wcscmp(const wchar_t *, const wchar_t *);
171 int wcscoll(const wchar_t *, const wchar_t *);
172 size_t wcslcat(wchar_t *, const wchar_t *, size_t);
173 size_t wcslcpy(wchar_t *, const wchar_t *, size_t);
174 size_t wcslen(const wchar_t *);
175 int wcsncasecmp(const wchar_t *, const wchar_t *, size_t);
176 size_t wcsxfrm(wchar_t *, const wchar_t *, size_t);
177 __END_DECLS
178 
179 #ifndef iswoctet
180 #define iswoctet(wc)	(((wchar_t)(wc) & 0xFF80) == 0xEF80)
181 #endif
182 
183 #ifdef __MirBSD__
184 /* initialise/set/reset a mbstate_t to empty */
185 #define mbsreset(ps) do {				\
186 	mbstate_t *__WC_s = (ps);			\
187 	if (ps != NULL)					\
188 		ps->count = 0;				\
189 } while (0)
190 #endif /* __MirBSD__ */
191 
192 /* XXX what about other compilers? */
193 #ifdef __GNUC__
194 
195 #ifdef __MirBSD__
196 /* roll back the middle char of a mis-done 3-byte mb->wc conversion */
197 #define mbrtowc_rollback(ps) __extension__({		\
198 	const mbstate_t *__WC_s = (ps);			\
199 	int __WC_rv = EOF;				\
200 	if (__WC_s->count == 1 &&			\
201 	    __WC_s->value >= 0x20)			\
202 		__WC_rv = 0x80 |			\
203 		    (__WC_s->value & 0x3F);		\
204 	(__WC_rv);					\
205 })
206 #endif /* __MirBSD__ */
207 
208 #define imax(a,b) __extension__({			\
209 	int imax_a = (a), imax_b = (b);			\
210 	(imax_a > imax_b ? imax_a : imax_b);		\
211 })
212 #define imin(a,b) __extension__({			\
213 	int imin_a = (a), imin_b = (b);			\
214 	(imin_a < imin_b ? imin_a : imin_b);		\
215 })
216 #define lmax(a,b) __extension__({			\
217 	long lmax_a = (a), lmax_b = (b);		\
218 	(lmax_a > lmax_b ? lmax_a : lmax_b);		\
219 })
220 #define lmin(a,b) __extension__({			\
221 	long lmin_a = (a), lmin_b = (b);		\
222 	(lmin_a < lmin_b ? lmin_a : lmin_b);		\
223 })
224 #define max(a,b) __extension__({			\
225 	u_int max_a = (a), max_b = (b);			\
226 	(max_a > max_b ? max_a : max_b);		\
227 })
228 #define min(a,b) __extension__({			\
229 	u_int min_a = (a), min_b = (b);			\
230 	(min_a < min_b ? min_a : min_b);		\
231 })
232 #define ulmax(a,b) __extension__({			\
233 	u_long ulmax_a = (a), ulmax_b = (b);		\
234 	(ulmax_a > ulmax_b ? ulmax_a : ulmax_b);	\
235 })
236 #define ulmin(a,b) __extension__({			\
237 	u_long ulmin_a = (a), ulmin_b = (b);		\
238 	(ulmin_a < ulmin_b ? ulmin_a : ulmin_b);	\
239 })
240 
241 #ifndef _STDLIB_H_abs_DEFINED
242 #undef abs
243 __BEGIN_DECLS
244 int abs(int);
245 __END_DECLS
246 #define _STDLIB_H_abs_DEFINED
247 #define abs(j) __extension__({				\
248 	int abs_j = (j);				\
249 	(abs_j < 0 ? -abs_j : abs_j);			\
250 })
251 #endif
252 
253 #endif /* __GNUC__ */
254 
255 #ifndef __LIBCKERN_HAVE_ADLER32_DECL
256 #define __LIBCKERN_HAVE_ADLER32_DECL
257 __BEGIN_DECLS
258 extern u_long adler32(u_long, const uint8_t *, unsigned);
259 __END_DECLS
260 #endif
261 
262 #endif
263