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