5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/SILC Types
24 * This header includes the most basic types used in the SILC source
25 * tree, such as arithmetic types and their manipulation macros. This
26 * file is included in the silcincludes.h and is automatically available
34 /****d* silcutil/SILCTypes/TRUE
42 * Boolean true value indicator.
51 /****d* silcutil/SILCTypes/FALSE
59 * Boolean false value indicator.
68 /****d* silcutil/SILCTypes/SilcBool
72 * typedef unigned char SilcBool;
76 * Boolean value, and is always 8-bits. Represents value 0 or 1.
79 typedef unsigned char SilcBool;
81 /****d* silcutil/SILCTypes/bool
85 * #define SilcBool ...
89 * Boolean value, and is 8-bits. Represents value 0 or 1. In
90 * C++ code this type is defined by the C++, and this definition is
95 * This macro is deprecated. Use SilcBool instead.
105 #define bool unsigned char
110 /* Our offsetof macro */
111 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
113 /* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
114 you have profiled the code first. */
116 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
117 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
119 #define silc_likely(expr) (expr)
120 #define silc_unlikely(expr) (expr)
121 #endif /* __GNUC__ >= 3 */
123 #if SILC_SIZEOF_SHORT > 2
124 #error "size of the short must be 2 bytes"
127 /****d* silcutil/SILCTypes/SilcUInt8
131 * typedef unsigned char SilcUInt8;
135 * 8-bit unsigned integer.
139 typedef unsigned char SilcUInt8;
142 /****d* silcutil/SILCTypes/SilcInt8
146 * typedef signed char SilcInt8;
150 * 8-bit signed integer.
154 typedef signed char SilcInt8;
157 /****d* silcutil/SILCTypes/SilcUInt16
161 * typedef unsigned short SilcUInt16;
165 * 16-bit unsigned integer. Guaranteed to be 16-bits.
169 typedef unsigned short SilcUInt16;
172 /****d* silcutil/SILCTypes/SilcInt16
176 * typedef signed short SilcInt16;
180 * 16-bit signed integer. Guaranteed to be 16-bits.
184 typedef signed short SilcInt16;
187 /****d* silcutil/SILCTypes/SilcUInt32
191 * typedef unsigned long SilcUInt32;
195 * 32-bit unsigned integer. Guaranteed to be 32-bits.
199 #if SILC_SIZEOF_LONG == 4
200 typedef unsigned long SilcUInt32;
201 typedef signed long SilcInt32;
203 #if SILC_SIZEOF_INT == 4
204 typedef unsigned int SilcUInt32;
205 typedef signed int SilcInt32;
207 #if SILC_SIZEOF_LONG_LONG >= 4
209 typedef unsigned long long SilcUInt32;
210 typedef signed long long SilcInt32;
217 /****d* silcutil/SILCTypes/SilcInt32
221 * typedef signed long SilcInt32;
225 * 32-bit signed integer. Guaranteed to be 32-bits.
229 /****d* silcutil/SILCTypes/SilcUInt64
233 * typedef unsigned long long SilcUInt64;
237 * 64-bit unsigned integer. Guaranteed to be 64-bits on systems that
242 #if SILC_SIZEOF_LONG >= 8
243 typedef unsigned long SilcUInt64;
244 typedef signed long SilcInt64;
246 #if SILC_SIZEOF_LONG_LONG >= 8
248 typedef unsigned long long SilcUInt64;
249 typedef signed long long SilcInt64;
251 typedef unsigned __int64 SilcUInt64;
252 typedef signed __int64 SilcInt64;
255 typedef SilcUInt32 SilcUInt64;
256 typedef SilcInt32 SilcInt64;
261 /****d* silcutil/SILCTypes/SilcInt64
265 * typedef signed long long SilcInt64;
269 * 64-bit signed integer. Guaranteed to be 64-bits on systems that
274 #if SILC_SIZEOF_VOID_P < 4
275 typedef SilcUInt32 * void *;
278 /****d* silcutil/SILCTypes/SilcSocket
286 * Platform specific socket. On POSIX compliant systems this is simply
287 * an integer, representing the socket. On other systems it is platform
288 * specific socket context. Access it only through routines that can
289 * handle SilcSocket types, unless you know what you are doing.
293 #if defined(SILC_UNIX) || defined(SILC_WIN32)
294 typedef int SilcSocket;
295 #elif defined(SILC_SYMBIAN)
296 typedef void * SilcSocket;
302 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
303 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
304 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
305 | ((SilcUInt32)(SilcUInt8)(cp)[3])
307 /****d* silcutil/SILCTypes/SILC_GET16_MSB
311 * #define SILC_GET16_MSB ...
315 * Return two 8-bit bytes, most significant bytes first.
319 #define SILC_GET16_MSB(l, cp) \
321 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8) \
322 | ((SilcUInt32)(SilcUInt8)(cp)[1]); \
326 /****d* silcutil/SILCTypes/SILC_GET32_MSB
330 * #define SILC_GET32_MSB ...
334 * Return four 8-bit bytes, most significant bytes first.
338 #define SILC_GET32_MSB(l, cp) \
340 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
341 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
342 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
343 | ((SilcUInt32)(SilcUInt8)(cp)[3]); \
347 /****d* silcutil/SILCTypes/SILC_GET64_MSB
351 * #define SILC_GET64_MSB ...
355 * Return eight 8-bit bytes, most significant bytes first.
359 #define SILC_GET64_MSB(l, cp) \
361 (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) | \
362 ((SilcUInt64)SILC_GET_WORD((cp) + 4))); \
366 /****d* silcutil/SILCTypes/SILC_GET16_LSB
370 * #define SILC_GET16_MSB ...
374 * Return two 8-bit bytes, least significant bytes first.
378 #if defined(SILC_I486) && defined(__GNUC__)
379 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
381 #define SILC_GET16_LSB(l, cp) \
383 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
384 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8); \
386 #endif /* SILC_I486 && __GNUC__ */
389 /****d* silcutil/SILCTypes/SILC_GET32_LSB
393 * #define SILC_GET32_LSB ...
397 * Return four 8-bit bytes, least significant bytes first.
401 #if defined(SILC_I486) && defined(__GNUC__)
402 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
404 #define SILC_GET32_LSB(l, cp) \
406 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
407 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
408 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
409 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24); \
411 #endif /* SILC_I486 && __GNUC__ */
413 /* Same as upper but XOR the result always. Special purpose macro. */
414 #if defined(SILC_I486) && defined(__GNUC__)
415 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
417 #define SILC_GET32_X_LSB(l, cp) \
418 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) \
419 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
420 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
421 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
422 #endif /* SILC_I486 && __GNUC__ */
425 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
429 * #define SILC_PUT16_MSB ...
433 * Put two 8-bit bytes, most significant bytes first.
437 #define SILC_PUT16_MSB(l, cp) \
439 (cp)[0] = (SilcUInt8)((l) >> 8); \
440 (cp)[1] = (SilcUInt8)(l); \
444 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
448 * #define SILC_PUT32_MSB ...
452 * Put four 8-bit bytes, most significant bytes first.
456 #define SILC_PUT32_MSB(l, cp) \
458 (cp)[0] = (SilcUInt8)((l) >> 24); \
459 (cp)[1] = (SilcUInt8)((l) >> 16); \
460 (cp)[2] = (SilcUInt8)((l) >> 8); \
461 (cp)[3] = (SilcUInt8)(l); \
465 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
469 * #define SILC_PUT64_MSB ...
473 * Put eight 8-bit bytes, most significant bytes first.
477 #define SILC_PUT64_MSB(l, cp) \
479 SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp)); \
480 SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4); \
484 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
488 * #define SILC_PUT16_LSB ...
492 * Put two 8-bit bytes, least significant bytes first.
496 #if defined(SILC_I486) && defined(__GNUC__)
497 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
499 #define SILC_PUT16_LSB(l, cp) \
501 (cp)[0] = (SilcUInt8)(l); \
502 (cp)[1] = (SilcUInt8)((l) >> 8); \
504 #endif /* SILC_I486 && __GNUC__ */
507 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
511 * #define SILC_PUT32_LSB ...
515 * Put four 8-bit bytes, least significant bytes first.
519 #if defined(SILC_I486) && defined(__GNUC__)
520 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
522 #define SILC_PUT32_LSB(l, cp) \
524 (cp)[0] = (SilcUInt8)(l); \
525 (cp)[1] = (SilcUInt8)((l) >> 8); \
526 (cp)[2] = (SilcUInt8)((l) >> 16); \
527 (cp)[3] = (SilcUInt8)((l) >> 24); \
529 #endif /* SILC_I486 && __GNUC__ */
532 /****d* silcutil/SILCTypes/SILC_SWAB_16
536 * #define SILC_SWAB_16 ...
540 * Swabs 16-bit unsigned integer byte order.
544 #define SILC_SWAB_16(l) \
545 ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
546 (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
549 /****d* silcutil/SILCTypes/SILC_SWAB_32
553 * #define SILC_SWAB_32 ...
557 * Swabs 32-bit unsigned integer byte order.
561 #define SILC_SWAB_32(l) \
562 ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) | \
563 (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8) | \
564 (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8) | \
565 (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
568 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
572 * #define SILC_PTR_TO_32 ...
576 * Type casts a pointer's value into a 32-bit integer. Use this to
577 * avoid compiler warnings when type casting pointers to integers
582 #if SILC_SIZEOF_VOID_P < 8
583 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
585 #define SILC_PTR_TO_32(_ptr__) \
586 ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
590 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
594 * #define SILC_PTR_TO_64 ...
598 * Type casts a pointer's value into a 64-bit integer. Use this to
599 * avoid compiler warnings when type casting pointers to integers
604 #if SILC_SIZEOF_VOID_P < 8
605 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
607 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
611 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
615 * #define SILC_32_TO_PTR ...
619 * Type casts a 32-bit integer value into a pointer. Use this to
620 * avoid compiler warnings when type casting integers to pointers of
625 #if SILC_SIZEOF_VOID_P < 8
626 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
628 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
632 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
636 * #define SILC_64_TO_PTR ...
640 * Type casts a 64-bit integer value into a pointer. Use this to
641 * avoid compiler warnings when type casting integers to pointers of
646 #if SILC_SIZEOF_VOID_P < 8
647 #define SILC_64_TO_PTR(_ival__) \
648 ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
650 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
654 #endif /* SILCTYPES_H */