5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2004 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/bool
76 * Boolean value, and is 8-bits. Represents value 0 or 1. In
77 * C++ code this type is defined by the C++, and this definition is
88 #define bool unsigned char
93 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
95 #if SILC_SIZEOF_SHORT > 2
96 #error "size of the short must be 2 bytes"
99 /****d* silcutil/SILCTypes/SilcUInt8
103 * typedef unsigned char SilcUInt8;
107 * 8-bit unsigned integer.
111 typedef unsigned char SilcUInt8;
114 /****d* silcutil/SILCTypes/SilcInt8
118 * typedef signed char SilcInt8;
122 * 8-bit signed integer.
126 typedef signed char SilcInt8;
129 /****d* silcutil/SILCTypes/SilcUInt16
133 * typedef unsigned short SilcUInt16;
137 * 16-bit unsigned integer. Guaranteed to be 16-bits.
141 typedef unsigned short SilcUInt16;
144 /****d* silcutil/SILCTypes/SilcInt16
148 * typedef signed short SilcInt16;
152 * 16-bit signed integer. Guaranteed to be 16-bits.
156 typedef signed short SilcInt16;
159 /****d* silcutil/SILCTypes/SilcUInt32
163 * typedef unsigned long SilcUInt32;
167 * 32-bit unsigned integer. Guaranteed to be 32-bits.
171 #if SILC_SIZEOF_LONG == 4
172 typedef unsigned long SilcUInt32;
173 typedef signed long SilcInt32;
175 #if SILC_SIZEOF_INT == 4
176 typedef unsigned int SilcUInt32;
177 typedef signed int SilcInt32;
179 #if SILC_SIZEOF_LONG_LONG >= 4
181 typedef unsigned long long SilcUInt32;
182 typedef signed long long SilcInt32;
189 /****d* silcutil/SILCTypes/SilcInt32
193 * typedef signed long SilcInt32;
197 * 32-bit signed integer. Guaranteed to be 32-bits.
201 /****d* silcutil/SILCTypes/SilcUInt64
205 * typedef unsigned long long SilcUInt64;
209 * 64-bit unsigned integer. Guaranteed to be 64-bits on systems that
214 #if SILC_SIZEOF_LONG >= 8
215 typedef unsigned long SilcUInt64;
216 typedef signed long SilcInt64;
218 #if SILC_SIZEOF_LONG_LONG >= 8
220 typedef unsigned long long SilcUInt64;
221 typedef signed long long SilcInt64;
223 typedef unsigned __int64 SilcUInt64;
224 typedef signed __int64 SilcInt64;
227 typedef SilcUInt32 SilcUInt64;
228 typedef SilcInt32 SilcInt64;
233 /****d* silcutil/SILCTypes/SilcInt64
237 * typedef signed long long SilcInt64;
241 * 64-bit signed integer. Guaranteed to be 64-bits on systems that
246 #if SILC_SIZEOF_VOID_P < 4
247 typedef SilcUInt32 * void *;
252 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
253 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
254 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
255 | ((SilcUInt32)(SilcUInt8)(cp)[3])
257 /****d* silcutil/SILCTypes/SILC_GET16_MSB
261 * #define SILC_GET16_MSB ...
265 * Return two 8-bit bytes, most significant bytes first.
269 #define SILC_GET16_MSB(l, cp) \
271 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8) \
272 | ((SilcUInt32)(SilcUInt8)(cp)[1]); \
276 /****d* silcutil/SILCTypes/SILC_GET32_MSB
280 * #define SILC_GET32_MSB ...
284 * Return four 8-bit bytes, most significant bytes first.
288 #define SILC_GET32_MSB(l, cp) \
290 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24 \
291 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16) \
292 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8) \
293 | ((SilcUInt32)(SilcUInt8)(cp)[3]); \
297 /****d* silcutil/SILCTypes/SILC_GET64_MSB
301 * #define SILC_GET64_MSB ...
305 * Return eight 8-bit bytes, most significant bytes first.
309 #define SILC_GET64_MSB(l, cp) \
311 (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) | \
312 ((SilcUInt64)SILC_GET_WORD((cp) + 4))); \
316 /****d* silcutil/SILCTypes/SILC_GET16_LSB
320 * #define SILC_GET16_MSB ...
324 * Return two 8-bit bytes, least significant bytes first.
328 #define SILC_GET16_LSB(l, cp) \
330 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
331 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8); \
335 /****d* silcutil/SILCTypes/SILC_GET32_LSB
339 * #define SILC_GET32_LSB ...
343 * Return four 8-bit bytes, least significant bytes first.
347 #define SILC_GET32_LSB(l, cp) \
349 (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) \
350 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
351 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
352 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24); \
355 /* Same as upper but XOR the result always. Special purpose macro. */
356 #define SILC_GET32_X_LSB(l, cp) \
357 (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0]) \
358 | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8) \
359 | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16) \
360 | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
363 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
367 * #define SILC_PUT16_MSB ...
371 * Put two 8-bit bytes, most significant bytes first.
375 #define SILC_PUT16_MSB(l, cp) \
377 (cp)[0] = (SilcUInt8)((l) >> 8); \
378 (cp)[1] = (SilcUInt8)(l); \
382 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
386 * #define SILC_PUT32_MSB ...
390 * Put four 8-bit bytes, most significant bytes first.
394 #define SILC_PUT32_MSB(l, cp) \
396 (cp)[0] = (SilcUInt8)((l) >> 24); \
397 (cp)[1] = (SilcUInt8)((l) >> 16); \
398 (cp)[2] = (SilcUInt8)((l) >> 8); \
399 (cp)[3] = (SilcUInt8)(l); \
403 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
407 * #define SILC_PUT64_MSB ...
411 * Put eight 8-bit bytes, most significant bytes first.
415 #define SILC_PUT64_MSB(l, cp) \
417 SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp)); \
418 SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4); \
422 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
426 * #define SILC_PUT16_LSB ...
430 * Put two 8-bit bytes, least significant bytes first.
434 #define SILC_PUT16_LSB(l, cp) \
436 (cp)[0] = (SilcUInt8)(l); \
437 (cp)[1] = (SilcUInt8)((l) >> 8); \
441 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
445 * #define SILC_PUT32_LSB ...
449 * Put four 8-bit bytes, least significant bytes first.
453 #define SILC_PUT32_LSB(l, cp) \
455 (cp)[0] = (SilcUInt8)(l); \
456 (cp)[1] = (SilcUInt8)((l) >> 8); \
457 (cp)[2] = (SilcUInt8)((l) >> 16); \
458 (cp)[3] = (SilcUInt8)((l) >> 24); \
462 /****d* silcutil/SILCTypes/SILC_SWAB_16
466 * #define SILC_SWAB_16 ...
470 * Swabs 16-bit unsigned integer byte order.
474 #define SILC_SWAB_16(l) \
475 ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
476 (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
479 /****d* silcutil/SILCTypes/SILC_SWAB_32
483 * #define SILC_SWAB_32 ...
487 * Swabs 32-bit unsigned integer byte order.
491 #define SILC_SWAB_32(l) \
492 ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) | \
493 (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8) | \
494 (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8) | \
495 (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
498 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
502 * #define SILC_PTR_TO_32 ...
506 * Type casts a pointer's value into a 32-bit integer. Use this to
507 * avoid compiler warnings when type casting pointers to integers
512 #if SILC_SIZEOF_VOID_P < 8
513 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
515 #define SILC_PTR_TO_32(_ptr__) \
516 ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
520 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
524 * #define SILC_PTR_TO_64 ...
528 * Type casts a pointer's value into a 64-bit integer. Use this to
529 * avoid compiler warnings when type casting pointers to integers
534 #if SILC_SIZEOF_VOID_P < 8
535 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
537 #define SILC_PTR_TO_64(_ptr__) \
538 ((SilcUInt64)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
542 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
546 * #define SILC_PTR_TO_32 ...
550 * Type casts a 32-bit integer value into a pointer. Use this to
551 * avoid compiler warnings when type casting integers to pointers of
556 #if SILC_SIZEOF_VOID_P < 8
557 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
559 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
563 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
567 * #define SILC_PTR_TO_64 ...
571 * Type casts a 64-bit integer value into a pointer. Use this to
572 * avoid compiler warnings when type casting integers to pointers of
577 #if SILC_SIZEOF_VOID_P < 8
578 #define SILC_64_TO_PTR(_ival__) \
579 ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
581 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
585 #endif /* SILCTYPES_H */