1 /* @(#)stdint.h	1.41 19/02/28 Copyright 1997-2019 J. Schilling */
2 /*
3  *	Abstraction from stdint.h
4  *
5  *	Copyright (c) 1997-2019 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef	_SCHILY_STDINT_H
22 #define	_SCHILY_STDINT_H
23 
24 #ifndef	_SCHILY_MCONFIG_H
25 #include <schily/mconfig.h>
26 #endif
27 
28 /*
29  * Let us include system defined types too.
30  */
31 #ifndef	_SCHILY_TYPES_H
32 #include <schily/types.h>
33 #endif
34 
35 /*
36  * For definitions of TYPE_MINVAL() ...
37  */
38 #ifndef	_SCHILY_TYPE_VAL_H
39 #include <schily/type_val.h>
40 #endif
41 
42 /*
43  * MSVC has size_t in stddef.h
44  */
45 #ifdef HAVE_STDDEF_H
46 #ifndef	_INCL_STDDEF_H
47 #include <stddef.h>
48 #define	_INCL_STDDEF_H
49 #endif
50 #endif
51 
52 /*
53  * CHAR_IS_UNSIGNED is needed to define int8_t
54  */
55 #ifdef	__CHAR_UNSIGNED__	/* GNU GCC define (dynamic)	*/
56 #ifndef CHAR_IS_UNSIGNED
57 #define	CHAR_IS_UNSIGNED	/* Sing Schily define (static)	*/
58 #endif
59 #endif
60 
61 /*
62  * limits.h on Solaris is broken and always #defines _CHAR_IS_SIGNED
63  * SunPro c #defines _CHAR_IS_UNSIGNED in case it was called with -xchar=u
64  *
65  * Note that this still does not fix the values for CHAR_MIN and CHAR_MAX
66  * retrived from limits.h since these constants are #defined in an #ifdef
67  * that is evaluated before we fix _CHAR_IS_SIGNED / _CHAR_IS_UNSIGNED.
68  */
69 #if	defined(CHAR_IS_UNSIGNED) || defined(_CHAR_IS_UNSIGNED)
70 #ifdef	_CHAR_IS_SIGNED
71 #define	__BAD_CHAR_SIGNED__
72 #undef	_CHAR_IS_SIGNED
73 #endif
74 #ifndef	_CHAR_IS_UNSIGNED
75 #define	_CHAR_IS_UNSIGNED
76 #endif
77 #endif
78 
79 /*
80  * This is a definition for a compiler dependant 64 bit type.
81  * There is currently a silently fallback to a long if the compiler does not
82  * support it. Check if this is the right way.
83  *
84  * Be very careful here as MSVC does not implement long long but rather __int64
85  * and once someone makes 'long long' 128 bits on a 64 bit machine, we need to
86  * check for a MSVC __int128 type.
87  */
88 #ifndef	NO_LONGLONG
89 #	if	!defined(USE_LONGLONG) && defined(HAVE_LONGLONG)
90 #		define	USE_LONGLONG
91 #	endif
92 #	if	!defined(USE_LONGLONG) && defined(HAVE___INT64)
93 #		define	USE_LONGLONG
94 #	endif
95 #endif
96 
97 #ifdef	USE_LONGLONG
98 
99 #	if	defined(HAVE___INT64)
100 
101 typedef	__int64			Llong;
102 typedef	unsigned __int64	Ullong;	/* We should avoid this */
103 typedef	unsigned __int64	ULlong;
104 
105 #define	SIZEOF_LLONG		SIZEOF___INT64
106 #define	SIZEOF_ULLONG		SIZEOF_UNSIGNED___INT64
107 
108 #	else	/* We must have HAVE_LONG_LONG */
109 
110 typedef	long long		Llong;
111 typedef	unsigned long long	Ullong;	/* We should avoid this */
112 typedef	unsigned long long	ULlong;
113 
114 #define	SIZEOF_LLONG		SIZEOF_LONG_LONG
115 #define	SIZEOF_ULLONG		SIZEOF_UNSIGNED_LONG_LONG
116 
117 #	endif	/* HAVE___INT64 / HAVE_LONG_LONG */
118 
119 #else	/* !USE_LONGLONG */
120 
121 typedef	long			Llong;
122 typedef	unsigned long		Ullong;	/* We should avoid this */
123 typedef	unsigned long		ULlong;
124 
125 #define	SIZEOF_LLONG		SIZEOF_LONG
126 #define	SIZEOF_ULLONG		SIZEOF_UNSIGNED_LONG
127 
128 #endif	/* USE_LONGLONG */
129 
130 #ifndef	LLONG_MIN
131 #define	LLONG_MIN	TYPE_MINVAL(Llong)
132 #endif
133 #ifndef	LLONG_MAX
134 #define	LLONG_MAX	TYPE_MAXVAL(Llong)
135 #endif
136 #ifndef	ULLONG_MAX
137 #define	ULLONG_MAX	TYPE_MAXVAL(Ullong)
138 #endif
139 
140 /*
141  * Start inttypes.h emulation.
142  *
143  * Thanks to Solaris 2.4 and even recent 1999 Linux versions, we
144  * cannot use the official UNIX-98 names here. Old Solaris versions
145  * define parts of the types in some exotic include files.
146  * Linux even defines incompatible types in <sys/types.h>.
147  */
148 
149 #if defined(HAVE_INTTYPES_H) || defined(HAVE_STDINT_H)
150 #if defined(HAVE_INTTYPES_H)
151 #	ifndef	_INCL_INTTYPES_H
152 #	include <inttypes.h>
153 #	define	_INCL_INTTYPES_H
154 #	endif
155 #else
156 #if defined(HAVE_STDINT_H)
157 #	ifndef	_INCL_STDINT_H
158 #	include <stdint.h>
159 #	define	_INCL_STDINT_H
160 #	endif
161 #endif
162 #endif
163 
164 #ifdef	__cplusplus
165 extern "C" {
166 #endif
167 
168 /*
169  * On VMS on VAX, these types are present but non-scalar.
170  * Thus we may not be able to use them
171  */
172 #ifdef	HAVE_LONGLONG
173 #	define	HAVE_INT64_T
174 #	define	HAVE_UINT64_T
175 #endif
176 
177 #define	Int8_t			int8_t
178 #define	Int16_t			int16_t
179 #define	Int32_t			int32_t
180 #ifdef	HAVE_LONGLONG
181 #define	Int64_t			int64_t
182 #endif
183 #define	Intmax_t		intmax_t
184 #define	UInt8_t			uint8_t
185 #define	UInt16_t		uint16_t
186 #define	UInt32_t		uint32_t
187 #ifdef	HAVE_LONGLONG
188 #define	UInt64_t		uint64_t
189 #endif
190 #define	UIntmax_t		uintmax_t
191 
192 #define	Intptr_t		intptr_t
193 #define	UIntptr_t		uintptr_t
194 
195 /*
196  * If we only have a UNIX-98 inttypes.h but no SUSv3
197  *
198  * Beware not to use int64_t / uint64_t as VMS on a VAX defines
199  * them as non-scalar (structure) based types.
200  */
201 #ifndef	HAVE_TYPE_INTMAX_T
202 #define	intmax_t	Llong
203 #endif
204 #ifndef	HAVE_TYPE_UINTMAX_T
205 #define	uintmax_t	ULlong
206 #endif
207 
208 #ifdef	__cplusplus
209 }
210 #endif
211 
212 #else	/* !HAVE_INTTYPES_H */
213 
214 #ifdef	__cplusplus
215 extern "C" {
216 #endif
217 
218 #if SIZEOF_CHAR != 1 || SIZEOF_UNSIGNED_CHAR != 1
219 /*
220  * #error will not work for all compilers (e.g. sunos4)
221  * The following line will abort compilation on all compilers
222  * if the above is true. And that's what we want.
223  */
224 error  Sizeof char is not equal 1
225 
226 #include <schily/err_char.h>	/* Avoid "unknown directive" with K&R */
227 #endif
228 
229 #if	defined(__STDC__) || defined(CHAR_IS_UNSIGNED)
230 	typedef	signed char		Int8_t;
231 #else
232 	typedef	char			Int8_t;
233 #endif
234 
235 #if SIZEOF_SHORT_INT == 2
236 	typedef	short			Int16_t;
237 #else
238 	error		No int16_t found
239 
240 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
241 #endif
242 
243 #if SIZEOF_INT == 4
244 #if defined(_MSC_VER) && SIZEOF_LONG_INT == 4
245 	typedef	long			Int32_t;
246 #else
247 	typedef	int			Int32_t;
248 #endif
249 #else
250 	error		No int32_t found
251 
252 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
253 #endif
254 
255 #if SIZEOF_LONG_INT == 8
256 	typedef		long		Int64_t;
257 #	define	HAVE_INT64_T
258 #else
259 #if SIZEOF_LONG_LONG == 8
260 	typedef		long long	Int64_t;
261 #	define	HAVE_INT64_T
262 #else
263 #if SIZEOF___INT64 == 8
264 	typedef		__int64		Int64_t;
265 #	define	HAVE_INT64_T
266 #else
267 /*
268  * Tolerate platforms without 64-Bit support.
269  */
270 /*	error		No int64_t found */
271 #endif
272 #endif
273 #endif
274 
275 #if SIZEOF_CHAR_P == SIZEOF_INT
276 	typedef		int		Intptr_t;
277 #else
278 #if SIZEOF_CHAR_P == SIZEOF_LONG_INT
279 	typedef		long		Intptr_t;
280 #else
281 #if SIZEOF_CHAR_P == SIZEOF_LLONG
282 	typedef		Llong		Intptr_t;
283 #else
284 	error		No intptr_t found
285 
286 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
287 #endif
288 #endif
289 #endif
290 
291 typedef	unsigned char		UInt8_t;
292 
293 #if SIZEOF_UNSIGNED_SHORT_INT == 2
294 	typedef	unsigned short		UInt16_t;
295 #else
296 	error		No uint16_t found
297 
298 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
299 #endif
300 
301 #if SIZEOF_UNSIGNED_INT == 4
302 #if defined(_MSC_VER) && SIZEOF_UNSIGNED_LONG_INT == 4
303 	typedef	unsigned long		UInt32_t;
304 #else
305 	typedef	unsigned int		UInt32_t;
306 #endif
307 #else
308 	error		No int32_t found
309 
310 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
311 #endif
312 
313 #if SIZEOF_UNSIGNED_LONG_INT == 8
314 	typedef	unsigned long		UInt64_t;
315 #	define	HAVE_UINT64_T
316 #else
317 #if SIZEOF_UNSIGNED_LONG_LONG == 8
318 	typedef	unsigned long long	UInt64_t;
319 #	define	HAVE_UINT64_T
320 #else
321 #if SIZEOF_UNSIGNED___INT64 == 8
322 	typedef	unsigned __int64	UInt64_t;
323 #	define	HAVE_UINT64_T
324 #else
325 /*
326  * Tolerate platforms without 64-Bit support.
327  */
328 /*	error		No uint64_t found */
329 #endif
330 #endif
331 #endif
332 
333 #define	Intmax_t	Llong
334 #define	UIntmax_t	Ullong
335 
336 #if SIZEOF_CHAR_P == SIZEOF_UNSIGNED_INT
337 	typedef		unsigned int	UIntptr_t;
338 #else
339 #if SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG_INT
340 	typedef		unsigned long	UIntptr_t;
341 #else
342 #if SIZEOF_CHAR_P == SIZEOF_ULLONG
343 	typedef		ULlong		UIntptr_t;
344 #else
345 	error		No uintptr_t found
346 
347 #include <schily/err_type.h>	/* Avoid "unknown directive" with K&R */
348 #endif
349 #endif
350 #endif
351 
352 #ifdef	_MSC_VER
353 /*
354  * All recent platforms define the POSIX/C-99 compliant types from inttypes.h
355  * except Microsoft. With these #defines, we may also use official types on a
356  * Microsoft environment.
357  *
358  * Warning: Linux-2.2 and before do not have inttypes.h and define some of the
359  * types in an incmpatible way.
360  */
361 #undef	int8_t
362 #define	int8_t			Int8_t
363 #undef	int16_t
364 #define	int16_t			Int16_t
365 #undef	int32_t
366 #define	int32_t			Int32_t
367 #undef	int64_t
368 #define	int64_t			Int64_t
369 #undef	intmax_t
370 #define	intmax_t		Intmax_t
371 #undef	uint8_t
372 #define	uint8_t			UInt8_t
373 #undef	uint16_t
374 #define	uint16_t		UInt16_t
375 #undef	uint32_t
376 #define	uint32_t		UInt32_t
377 #undef	uint64_t
378 #define	uint64_t		UInt64_t
379 #undef	uintmax_t
380 #define	uintmax_t		UIntmax_t
381 
382 #undef	intptr_t
383 #define	intptr_t		Intptr_t
384 #undef	uintptr_t
385 #define	uintptr_t		UIntptr_t
386 #endif	/* _MSC_VER */
387 
388 #ifdef	__cplusplus
389 }
390 #endif
391 
392 #endif	/* HAVE_INTTYPES_H */
393 
394 #ifndef	INT8_MIN
395 #define	INT8_MIN	TYPE_MINVAL(Int8_t)
396 #endif
397 #ifndef	INT8_MAX
398 #define	INT8_MAX	TYPE_MAXVAL(Int8_t)
399 #endif
400 #ifndef	UINT8_MAX
401 #define	UINT8_MAX	TYPE_MAXVAL(UInt8_t)
402 #endif
403 
404 #ifndef	INT16_MIN
405 #define	INT16_MIN	TYPE_MINVAL(Int16_t)
406 #endif
407 #ifndef	INT16_MAX
408 #define	INT16_MAX	TYPE_MAXVAL(Int16_t)
409 #endif
410 #ifndef	UINT16_MAX
411 #define	UINT16_MAX	TYPE_MAXVAL(UInt16_t)
412 #endif
413 
414 #ifndef	INT32_MIN
415 #define	INT32_MIN	TYPE_MINVAL(Int32_t)
416 #endif
417 #ifndef	INT32_MAX
418 #define	INT32_MAX	TYPE_MAXVAL(Int32_t)
419 #endif
420 #ifndef	UINT32_MAX
421 #define	UINT32_MAX	TYPE_MAXVAL(UInt32_t)
422 #endif
423 
424 #ifdef	HAVE_INT64_T
425 #ifndef	INT64_MIN
426 #define	INT64_MIN	TYPE_MINVAL(Int64_t)
427 #endif
428 #ifndef	INT64_MAX
429 #define	INT64_MAX	TYPE_MAXVAL(Int64_t)
430 #endif
431 #endif
432 #ifdef	HAVE_UINT64_T
433 #ifndef	UINT64_MAX
434 #define	UINT64_MAX	TYPE_MAXVAL(UInt64_t)
435 #endif
436 #endif
437 
438 #ifndef	INTMAX_MIN
439 #define	INTMAX_MIN	TYPE_MINVAL(Intmax_t)
440 #endif
441 #ifndef	INTMAX_MAX
442 #define	INTMAX_MAX	TYPE_MAXVAL(Intmax_t)
443 #endif
444 #ifndef	UINTMAX_MAX
445 #define	UINTMAX_MAX	TYPE_MAXVAL(UIntmax_t)
446 #endif
447 
448 #define	SIZE_T_MIN	TYPE_MINVAL(size_t)
449 #ifdef	SIZE_T_MAX
450 #undef	SIZE_T_MAX			/* FreeBSD has a similar #define */
451 #endif
452 #define	SIZE_T_MAX	TYPE_MAXVAL(size_t)
453 
454 #define	SSIZE_T_MIN	TYPE_MINVAL(ssize_t)
455 #define	SSIZE_T_MAX	TYPE_MAXVAL(ssize_t)
456 
457 #endif	/* _SCHILY_STDINT_H */
458