5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2007 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/SilcBool
38 * typedef unigned char SilcBool;
42 * Boolean value, and is always 8-bits. Represents value 0 or 1.
45 typedef unsigned char SilcBool;
47 /* The bool macro is deprecated. Use SilcBool instead. */
53 #define bool unsigned char
57 /****d* silcutil/SILCTypes/TRUE
65 * Boolean true value indicator.
74 /****d* silcutil/SILCTypes/FALSE
82 * Boolean false value indicator.
91 /* Our offsetof macro */
92 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
94 /* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
95 you have profiled the code first. */
97 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
98 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
100 #define silc_likely(expr) (expr)
101 #define silc_unlikely(expr) (expr)
102 #endif /* __GNUC__ >= 3 */
104 #if SILC_SIZEOF_SHORT > 2
105 #error "size of the short must be 2 bytes"
108 /****d* silcutil/SILCTypes/SilcUInt8
112 * typedef unsigned char SilcUInt8;
116 * 8-bit unsigned integer.
120 typedef unsigned char SilcUInt8;
123 /****d* silcutil/SILCTypes/SilcInt8
127 * typedef signed char SilcInt8;
131 * 8-bit signed integer.
135 typedef signed char SilcInt8;
138 /****d* silcutil/SILCTypes/SilcUInt16
142 * typedef unsigned short SilcUInt16;
146 * 16-bit unsigned integer. Guaranteed to be 16-bits.
150 typedef unsigned short SilcUInt16;
153 /****d* silcutil/SILCTypes/SilcInt16
157 * typedef signed short SilcInt16;
161 * 16-bit signed integer. Guaranteed to be 16-bits.
165 typedef signed short SilcInt16;
168 /****d* silcutil/SILCTypes/SilcUInt32
172 * typedef unsigned long SilcUInt32;
176 * 32-bit unsigned integer. Guaranteed to be 32-bits.
180 #if SILC_SIZEOF_LONG == 4
181 typedef unsigned long SilcUInt32;
182 typedef signed long SilcInt32;
184 #if SILC_SIZEOF_INT == 4
185 typedef unsigned int SilcUInt32;
186 typedef signed int SilcInt32;
188 #if SILC_SIZEOF_LONG_LONG >= 4
190 typedef unsigned long long SilcUInt32;
191 typedef signed long long SilcInt32;
198 /****d* silcutil/SILCTypes/SilcInt32
202 * typedef signed long SilcInt32;
206 * 32-bit signed integer. Guaranteed to be 32-bits.
210 /****d* silcutil/SILCTypes/SilcUInt64
214 * typedef unsigned long long SilcUInt64;
218 * 64-bit unsigned integer. Guaranteed to be 64-bits on systems that
223 #if SILC_SIZEOF_LONG >= 8
224 typedef unsigned long SilcUInt64;
225 typedef signed long SilcInt64;
227 #if SILC_SIZEOF_LONG_LONG >= 8
229 typedef unsigned long long SilcUInt64;
230 typedef signed long long SilcInt64;
232 typedef unsigned __int64 SilcUInt64;
233 typedef signed __int64 SilcInt64;
236 typedef SilcUInt32 SilcUInt64;
237 typedef SilcInt32 SilcInt64;
242 /****d* silcutil/SILCTypes/SilcInt64
246 * typedef signed long long SilcInt64;
250 * 64-bit signed integer. Guaranteed to be 64-bits on systems that
255 #if SILC_SIZEOF_VOID_P < 4
256 typedef SilcUInt32 * void *;
259 /****d* silcutil/SILCTypes/SilcSocket
267 * Platform specific socket. On POSIX compliant systems this is simply
268 * an integer, representing the socket. On other systems it is platform
269 * specific socket context. Access it only through routines that can
270 * handle SilcSocket types, unless you know what you are doing.
274 #if defined(SILC_UNIX)
275 typedef int SilcSocket;
276 #elif defined(SILC_WIN32)
277 typedef SOCKET SilcSocket;
278 #elif defined(SILC_SYMBIAN)
279 typedef void * SilcSocket;
285 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
286 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
287 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
288 | ((SilcUInt32)(SilcUInt8)(cp)[3])
290 /****d* silcutil/SILCTypes/SILC_GET16_MSB
294 * #define SILC_GET16_MSB ...
298 * Return two 8-bit bytes, most significant bytes first.
302 #define SILC_GET16_MSB(l, cp) \
304 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8) \
305 | ((SilcUInt32)(SilcUInt8)(cp)[1]); \
309 /****d* silcutil/SILCTypes/SILC_GET32_MSB
313 * #define SILC_GET32_MSB ...
317 * Return four 8-bit bytes, most significant bytes first.
321 #define SILC_GET32_MSB(l, cp) \
323 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
324 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
325 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
326 | ((SilcUInt32)(SilcUInt8)(cp)[3]); \
330 /* Same as upper but XOR the result always. Special purpose macro. */
331 #define SILC_GET32_X_MSB(l, cp) \
332 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
333 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
334 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
335 | ((SilcUInt32)(SilcUInt8)(cp)[3]);
337 /****d* silcutil/SILCTypes/SILC_GET64_MSB
341 * #define SILC_GET64_MSB ...
345 * Return eight 8-bit bytes, most significant bytes first.
349 #define SILC_GET64_MSB(l, cp) \
351 (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) | \
352 ((SilcUInt64)SILC_GET_WORD((cp) + 4))); \
356 /****d* silcutil/SILCTypes/SILC_GET16_LSB
360 * #define SILC_GET16_MSB ...
364 * Return two 8-bit bytes, least significant bytes first.
368 #if defined(SILC_I486) && defined(__GNUC__)
369 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
371 #define SILC_GET16_LSB(l, cp) \
373 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
374 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8); \
376 #endif /* SILC_I486 && __GNUC__ */
379 /****d* silcutil/SILCTypes/SILC_GET32_LSB
383 * #define SILC_GET32_LSB ...
387 * Return four 8-bit bytes, least significant bytes first.
391 #if defined(SILC_I486) && defined(__GNUC__)
392 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
394 #define SILC_GET32_LSB(l, cp) \
396 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
397 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
398 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
399 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24); \
401 #endif /* SILC_I486 && __GNUC__ */
403 /* Same as upper but XOR the result always. Special purpose macro. */
404 #if defined(SILC_I486) && defined(__GNUC__)
405 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
407 #define SILC_GET32_X_LSB(l, cp) \
408 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) \
409 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
410 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
411 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
412 #endif /* SILC_I486 && __GNUC__ */
415 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
419 * #define SILC_PUT16_MSB ...
423 * Put two 8-bit bytes, most significant bytes first.
427 #define SILC_PUT16_MSB(l, cp) \
429 (cp)[0] = (SilcUInt8)((l) >> 8); \
430 (cp)[1] = (SilcUInt8)(l); \
434 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
438 * #define SILC_PUT32_MSB ...
442 * Put four 8-bit bytes, most significant bytes first.
446 #define SILC_PUT32_MSB(l, cp) \
448 (cp)[0] = (SilcUInt8)((l) >> 24); \
449 (cp)[1] = (SilcUInt8)((l) >> 16); \
450 (cp)[2] = (SilcUInt8)((l) >> 8); \
451 (cp)[3] = (SilcUInt8)(l); \
455 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
459 * #define SILC_PUT64_MSB ...
463 * Put eight 8-bit bytes, most significant bytes first.
467 #define SILC_PUT64_MSB(l, cp) \
469 SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp)); \
470 SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4); \
474 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
478 * #define SILC_PUT16_LSB ...
482 * Put two 8-bit bytes, least significant bytes first.
486 #if defined(SILC_I486) && defined(__GNUC__)
487 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
489 #define SILC_PUT16_LSB(l, cp) \
491 (cp)[0] = (SilcUInt8)(l); \
492 (cp)[1] = (SilcUInt8)((l) >> 8); \
494 #endif /* SILC_I486 && __GNUC__ */
497 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
501 * #define SILC_PUT32_LSB ...
505 * Put four 8-bit bytes, least significant bytes first.
509 #if defined(SILC_I486) && defined(__GNUC__)
510 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
512 #define SILC_PUT32_LSB(l, cp) \
514 (cp)[0] = (SilcUInt8)(l); \
515 (cp)[1] = (SilcUInt8)((l) >> 8); \
516 (cp)[2] = (SilcUInt8)((l) >> 16); \
517 (cp)[3] = (SilcUInt8)((l) >> 24); \
519 #endif /* SILC_I486 && __GNUC__ */
522 /****d* silcutil/SILCTypes/SILC_SWAB_16
526 * #define SILC_SWAB_16 ...
530 * Swabs 16-bit unsigned integer byte order.
534 #define SILC_SWAB_16(l) \
535 ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
536 (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
539 /****d* silcutil/SILCTypes/SILC_SWAB_32
543 * #define SILC_SWAB_32 ...
547 * Swabs 32-bit unsigned integer byte order.
551 #define SILC_SWAB_32(l) \
552 ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) | \
553 (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8) | \
554 (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8) | \
555 (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
558 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
562 * #define SILC_PTR_TO_32 ...
566 * Type casts a pointer's value into a 32-bit integer. Use this to
567 * avoid compiler warnings when type casting pointers to integers
572 #if SILC_SIZEOF_VOID_P < 8
573 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
575 #define SILC_PTR_TO_32(_ptr__) \
576 ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
580 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
584 * #define SILC_PTR_TO_64 ...
588 * Type casts a pointer's value into a 64-bit integer. Use this to
589 * avoid compiler warnings when type casting pointers to integers
594 #if SILC_SIZEOF_VOID_P < 8
595 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
597 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
601 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
605 * #define SILC_32_TO_PTR ...
609 * Type casts a 32-bit integer value into a pointer. Use this to
610 * avoid compiler warnings when type casting integers to pointers of
615 #if SILC_SIZEOF_VOID_P < 8
616 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
618 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
622 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
626 * #define SILC_64_TO_PTR ...
630 * Type casts a 64-bit integer value into a pointer. Use this to
631 * avoid compiler warnings when type casting integers to pointers of
636 #if SILC_SIZEOF_VOID_P < 8
637 #define SILC_64_TO_PTR(_ival__) \
638 ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
640 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
644 #endif /* SILCTYPES_H */