Sun Mar 11 15:22:42 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcutil / silctypes.h
1 /*
2
3   silctypes.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Types
21  *
22  * DESCRIPTION
23  *
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
27  *    for application.
28  *
29  ***/
30
31 #ifndef SILCTYPES_H
32 #define SILCTYPES_H
33
34 /****d* silcutil/SILCTypes/SilcBool
35  *
36  * NAME
37  *
38  *    typedef unigned char SilcBool;
39  *
40  * DESCRIPTION
41  *
42  *    Boolean value, and is always 8-bits.  Represents value 0 or 1.
43  *
44  ***/
45 typedef unsigned char SilcBool;
46
47 /* The bool macro is deprecated.  Use SilcBool instead. */
48 #ifdef SILC_MACOSX
49 #define bool _Bool
50 #endif
51 #ifndef __cplusplus
52 #ifndef bool
53 #define bool unsigned char
54 #endif
55 #endif
56
57 /****d* silcutil/SILCTypes/TRUE
58  *
59  * NAME
60  *
61  *    #define TRUE ...
62  *
63  * DESCRIPTION
64  *
65  *    Boolean true value indicator.
66  *
67  * SOURCE
68  */
69 #ifndef TRUE
70 #define TRUE 1
71 #endif
72 /***/
73
74 /****d* silcutil/SILCTypes/FALSE
75  *
76  * NAME
77  *
78  *    #define FALSE ...
79  *
80  * DESCRIPTION
81  *
82  *    Boolean false value indicator.
83  *
84  * SOURCE
85  */
86 #ifndef FALSE
87 #define FALSE 0
88 #endif
89 /***/
90
91 /* Our offsetof macro */
92 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
93
94 /* silc_likely and silc_unlikely GCC branch prediction macros. Use only if
95    you have profiled the code first. */
96 #if __GNUC__ >= 3
97 #define silc_likely(expr) __builtin_expect(!!(expr), 1)
98 #define silc_unlikely(expr) __builtin_expect(!!(expr), 0)
99 #else
100 #define silc_likely(expr) (expr)
101 #define silc_unlikely(expr) (expr)
102 #endif /* __GNUC__ >= 3 */
103
104 #if SILC_SIZEOF_SHORT > 2
105 #error "size of the short must be 2 bytes"
106 #endif
107
108 /****d* silcutil/SILCTypes/SilcUInt8
109  *
110  * NAME
111  *
112  *    typedef unsigned char SilcUInt8;
113  *
114  * DESCRIPTION
115  *
116  *    8-bit unsigned integer.
117  *
118  * SOURCE
119  */
120 typedef unsigned char SilcUInt8;
121 /***/
122
123 /****d* silcutil/SILCTypes/SilcInt8
124  *
125  * NAME
126  *
127  *    typedef signed char SilcInt8;
128  *
129  * DESCRIPTION
130  *
131  *    8-bit signed integer.
132  *
133  * SOURCE
134  */
135 typedef signed char SilcInt8;
136 /***/
137
138 /****d* silcutil/SILCTypes/SilcUInt16
139  *
140  * NAME
141  *
142  *    typedef unsigned short SilcUInt16;
143  *
144  * DESCRIPTION
145  *
146  *    16-bit unsigned integer.  Guaranteed to be 16-bits.
147  *
148  * SOURCE
149  */
150 typedef unsigned short SilcUInt16;
151 /***/
152
153 /****d* silcutil/SILCTypes/SilcInt16
154  *
155  * NAME
156  *
157  *    typedef signed short SilcInt16;
158  *
159  * DESCRIPTION
160  *
161  *    16-bit signed integer.  Guaranteed to be 16-bits.
162  *
163  * SOURCE
164  */
165 typedef signed short SilcInt16;
166 /***/
167
168 /****d* silcutil/SILCTypes/SilcUInt32
169  *
170  * NAME
171  *
172  *    typedef unsigned long SilcUInt32;
173  *
174  * DESCRIPTION
175  *
176  *    32-bit unsigned integer.  Guaranteed to be 32-bits.
177  *
178  * SOURCE
179  */
180 #if SILC_SIZEOF_LONG == 4
181 typedef unsigned long SilcUInt32;
182 typedef signed long SilcInt32;
183 #else
184 #if SILC_SIZEOF_INT == 4
185 typedef unsigned int SilcUInt32;
186 typedef signed int SilcInt32;
187 #else
188 #if SILC_SIZEOF_LONG_LONG >= 4
189 #ifndef WIN32
190 typedef unsigned long long SilcUInt32;
191 typedef signed long long SilcInt32;
192 #endif
193 #endif
194 #endif
195 #endif
196 /***/
197
198 /****d* silcutil/SILCTypes/SilcInt32
199  *
200  * NAME
201  *
202  *    typedef signed long SilcInt32;
203  *
204  * DESCRIPTION
205  *
206  *    32-bit signed integer.  Guaranteed to be 32-bits.
207  *
208  ***/
209
210 /****d* silcutil/SILCTypes/SilcUInt64
211  *
212  * NAME
213  *
214  *    typedef unsigned long long SilcUInt64;
215  *
216  * DESCRIPTION
217  *
218  *    64-bit unsigned integer.  Guaranteed to be 64-bits on systems that
219  *    support it.
220  *
221  * SOURCE
222  */
223 #if SILC_SIZEOF_LONG >= 8
224 typedef unsigned long SilcUInt64;
225 typedef signed long SilcInt64;
226 #else
227 #if SILC_SIZEOF_LONG_LONG >= 8
228 #ifndef WIN32
229 typedef unsigned long long SilcUInt64;
230 typedef signed long long SilcInt64;
231 #else
232 typedef unsigned __int64 SilcUInt64;
233 typedef signed __int64 SilcInt64;
234 #endif
235 #else
236 typedef SilcUInt32 SilcUInt64;
237 typedef SilcInt32 SilcInt64;
238 #endif
239 #endif
240 /***/
241
242 /****d* silcutil/SILCTypes/SilcInt64
243  *
244  * NAME
245  *
246  *    typedef signed long long SilcInt64;
247  *
248  * DESCRIPTION
249  *
250  *    64-bit signed integer.  Guaranteed to be 64-bits on systems that
251  *    support it.
252  *
253  ***/
254
255 #if SILC_SIZEOF_VOID_P < 4
256 typedef SilcUInt32 * void *;
257 #endif
258
259 /****d* silcutil/SILCTypes/SilcSocket
260  *
261  * NAME
262  *
263  *    SilcSocket
264  *
265  * DESCRIPTION
266  *
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.
271  *
272  * SOURCE
273  */
274 #if defined(SILC_UNIX) || defined(SILC_WIN32)
275 typedef int SilcSocket;
276 #elif defined(SILC_SYMBIAN)
277 typedef void * SilcSocket;
278 #endif
279 /***/
280
281 /* Macros */
282
283 #define SILC_GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24        \
284                     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
285                     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
286                     | ((SilcUInt32)(SilcUInt8)(cp)[3])
287
288 /****d* silcutil/SILCTypes/SILC_GET16_MSB
289  *
290  * NAME
291  *
292  *    #define SILC_GET16_MSB ...
293  *
294  * DESCRIPTION
295  *
296  *    Return two 8-bit bytes, most significant bytes first.
297  *
298  * SOURCE
299  */
300 #define SILC_GET16_MSB(l, cp)                           \
301 do {                                                    \
302   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)           \
303     | ((SilcUInt32)(SilcUInt8)(cp)[1]);                 \
304 } while(0)
305 /***/
306
307 /****d* silcutil/SILCTypes/SILC_GET32_MSB
308  *
309  * NAME
310  *
311  *    #define SILC_GET32_MSB ...
312  *
313  * DESCRIPTION
314  *
315  *    Return four 8-bit bytes, most significant bytes first.
316  *
317  * SOURCE
318  */
319 #define SILC_GET32_MSB(l, cp)                           \
320 do {                                                    \
321   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24          \
322     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
323     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
324     | ((SilcUInt32)(SilcUInt8)(cp)[3]);                 \
325 } while(0)
326 /***/
327
328 /****d* silcutil/SILCTypes/SILC_GET64_MSB
329  *
330  * NAME
331  *
332  *    #define SILC_GET64_MSB ...
333  *
334  * DESCRIPTION
335  *
336  *    Return eight 8-bit bytes, most significant bytes first.
337  *
338  * SOURCE
339  */
340 #define SILC_GET64_MSB(l, cp)                                   \
341 do {                                                            \
342   (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |            \
343          ((SilcUInt64)SILC_GET_WORD((cp) + 4)));                \
344 } while(0)
345 /***/
346
347 /****d* silcutil/SILCTypes/SILC_GET16_LSB
348  *
349  * NAME
350  *
351  *    #define SILC_GET16_MSB ...
352  *
353  * DESCRIPTION
354  *
355  *    Return two 8-bit bytes, least significant bytes first.
356  *
357  * SOURCE
358  */
359 #if defined(SILC_I486) && defined(__GNUC__)
360 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
361 #else
362 #define SILC_GET16_LSB(l, cp)                           \
363 do {                                                    \
364   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
365     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);            \
366 } while(0)
367 #endif /* SILC_I486 && __GNUC__ */
368 /***/
369
370 /****d* silcutil/SILCTypes/SILC_GET32_LSB
371  *
372  * NAME
373  *
374  *    #define SILC_GET32_LSB ...
375  *
376  * DESCRIPTION
377  *
378  *    Return four 8-bit bytes, least significant bytes first.
379  *
380  * SOURCE
381  */
382 #if defined(SILC_I486) && defined(__GNUC__)
383 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
384 #else
385 #define SILC_GET32_LSB(l, cp)                           \
386 do {                                                    \
387   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
388     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
389     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
390     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);           \
391 } while(0)
392 #endif /* SILC_I486 && __GNUC__ */
393
394 /* Same as upper but XOR the result always. Special purpose macro. */
395 #if defined(SILC_I486) && defined(__GNUC__)
396 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
397 #else
398 #define SILC_GET32_X_LSB(l, cp)                         \
399   (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])               \
400     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
401     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
402     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
403 #endif /* SILC_I486 && __GNUC__ */
404 /***/
405
406 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
407  *
408  * NAME
409  *
410  *    #define SILC_PUT16_MSB ...
411  *
412  * DESCRIPTION
413  *
414  *    Put two 8-bit bytes, most significant bytes first.
415  *
416  * SOURCE
417  */
418 #define SILC_PUT16_MSB(l, cp)                   \
419 do {                                            \
420   (cp)[0] = (SilcUInt8)((l) >> 8);              \
421   (cp)[1] = (SilcUInt8)(l);                     \
422 } while(0)
423 /***/
424
425 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
426  *
427  * NAME
428  *
429  *    #define SILC_PUT32_MSB ...
430  *
431  * DESCRIPTION
432  *
433  *    Put four 8-bit bytes, most significant bytes first.
434  *
435  * SOURCE
436  */
437 #define SILC_PUT32_MSB(l, cp)                   \
438 do {                                            \
439   (cp)[0] = (SilcUInt8)((l) >> 24);             \
440   (cp)[1] = (SilcUInt8)((l) >> 16);             \
441   (cp)[2] = (SilcUInt8)((l) >> 8);              \
442   (cp)[3] = (SilcUInt8)(l);                     \
443 } while(0)
444 /***/
445
446 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
447  *
448  * NAME
449  *
450  *    #define SILC_PUT64_MSB ...
451  *
452  * DESCRIPTION
453  *
454  *    Put eight 8-bit bytes, most significant bytes first.
455  *
456  * SOURCE
457  */
458 #define SILC_PUT64_MSB(l, cp)                                   \
459 do {                                                            \
460   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
461   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
462 } while(0)
463 /***/
464
465 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
466  *
467  * NAME
468  *
469  *    #define SILC_PUT16_LSB ...
470  *
471  * DESCRIPTION
472  *
473  *    Put two 8-bit bytes, least significant bytes first.
474  *
475  * SOURCE
476  */
477 #if defined(SILC_I486) && defined(__GNUC__)
478 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
479 #else
480 #define SILC_PUT16_LSB(l, cp)                   \
481 do  {                                           \
482   (cp)[0] = (SilcUInt8)(l);                     \
483   (cp)[1] = (SilcUInt8)((l) >> 8);              \
484 } while(0)
485 #endif /* SILC_I486 && __GNUC__ */
486 /***/
487
488 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
489  *
490  * NAME
491  *
492  *    #define SILC_PUT32_LSB ...
493  *
494  * DESCRIPTION
495  *
496  *    Put four 8-bit bytes, least significant bytes first.
497  *
498  * SOURCE
499  */
500 #if defined(SILC_I486) && defined(__GNUC__)
501 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
502 #else
503 #define SILC_PUT32_LSB(l, cp)                   \
504 do {                                            \
505   (cp)[0] = (SilcUInt8)(l);                     \
506   (cp)[1] = (SilcUInt8)((l) >> 8);              \
507   (cp)[2] = (SilcUInt8)((l) >> 16);             \
508   (cp)[3] = (SilcUInt8)((l) >> 24);             \
509 } while(0)
510 #endif /* SILC_I486 && __GNUC__ */
511 /***/
512
513 /****d* silcutil/SILCTypes/SILC_SWAB_16
514  *
515  * NAME
516  *
517  *    #define SILC_SWAB_16 ...
518  *
519  * DESCRIPTION
520  *
521  *    Swabs 16-bit unsigned integer byte order.
522  *
523  * SOURCE
524  */
525 #define SILC_SWAB_16(l)                                         \
526   ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
527                (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
528 /***/
529
530 /****d* silcutil/SILCTypes/SILC_SWAB_32
531  *
532  * NAME
533  *
534  *    #define SILC_SWAB_32 ...
535  *
536  * DESCRIPTION
537  *
538  *    Swabs 32-bit unsigned integer byte order.
539  *
540  * SOURCE
541  */
542 #define SILC_SWAB_32(l)                                                 \
543   ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) |   \
544                (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8)  |   \
545                (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8)  |   \
546                (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
547 /***/
548
549 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
550  *
551  * NAME
552  *
553  *    #define SILC_PTR_TO_32 ...
554  *
555  * DESCRIPTION
556  *
557  *    Type casts a pointer's value into a 32-bit integer.  Use this to
558  *    avoid compiler warnings when type casting pointers to integers
559  *    of different size.
560  *
561  * SOURCE
562  */
563 #if SILC_SIZEOF_VOID_P < 8
564 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
565 #else
566 #define SILC_PTR_TO_32(_ptr__)                                          \
567   ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
568 #endif
569 /***/
570
571 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
572  *
573  * NAME
574  *
575  *    #define SILC_PTR_TO_64 ...
576  *
577  * DESCRIPTION
578  *
579  *    Type casts a pointer's value into a 64-bit integer.  Use this to
580  *    avoid compiler warnings when type casting pointers to integers
581  *    of different size.
582  *
583  * SOURCE
584  */
585 #if SILC_SIZEOF_VOID_P < 8
586 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
587 #else
588 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
589 #endif
590 /***/
591
592 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
593  *
594  * NAME
595  *
596  *    #define SILC_32_TO_PTR ...
597  *
598  * DESCRIPTION
599  *
600  *    Type casts a 32-bit integer value into a pointer.  Use this to
601  *    avoid compiler warnings when type casting integers to pointers of
602  *    different size.
603  *
604  * SOURCE
605  */
606 #if SILC_SIZEOF_VOID_P < 8
607 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
608 #else
609 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
610 #endif
611 /***/
612
613 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
614  *
615  * NAME
616  *
617  *    #define SILC_64_TO_PTR ...
618  *
619  * DESCRIPTION
620  *
621  *    Type casts a 64-bit integer value into a pointer.  Use this to
622  *    avoid compiler warnings when type casting integers to pointers of
623  *    different size.
624  *
625  * SOURCE
626  */
627 #if SILC_SIZEOF_VOID_P < 8
628 #define SILC_64_TO_PTR(_ival__)                                         \
629   ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
630 #else
631 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
632 #endif
633 /***/
634
635 #endif /* SILCTYPES_H */