b22c7e771d83b1de276eb8edf1aab09ff59aab90
[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)
275 typedef int SilcSocket;
276 #elif defined(SILC_WIN32)
277 typedef SOCKET SilcSocket;
278 #elif defined(SILC_SYMBIAN)
279 typedef void * SilcSocket;
280 #endif
281 /***/
282
283 /* Macros */
284
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])
289
290 /****d* silcutil/SILCTypes/SILC_GET16_MSB
291  *
292  * NAME
293  *
294  *    #define SILC_GET16_MSB ...
295  *
296  * DESCRIPTION
297  *
298  *    Return two 8-bit bytes, most significant bytes first.
299  *
300  * SOURCE
301  */
302 #define SILC_GET16_MSB(l, cp)                           \
303 do {                                                    \
304   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)           \
305     | ((SilcUInt32)(SilcUInt8)(cp)[1]);                 \
306 } while(0)
307 /***/
308
309 /****d* silcutil/SILCTypes/SILC_GET32_MSB
310  *
311  * NAME
312  *
313  *    #define SILC_GET32_MSB ...
314  *
315  * DESCRIPTION
316  *
317  *    Return four 8-bit bytes, most significant bytes first.
318  *
319  * SOURCE
320  */
321 #define SILC_GET32_MSB(l, cp)                           \
322 do {                                                    \
323   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24          \
324     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)            \
325     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)             \
326     | ((SilcUInt32)(SilcUInt8)(cp)[3]);                 \
327 } while(0)
328 /***/
329
330 /****d* silcutil/SILCTypes/SILC_GET64_MSB
331  *
332  * NAME
333  *
334  *    #define SILC_GET64_MSB ...
335  *
336  * DESCRIPTION
337  *
338  *    Return eight 8-bit bytes, most significant bytes first.
339  *
340  * SOURCE
341  */
342 #define SILC_GET64_MSB(l, cp)                                   \
343 do {                                                            \
344   (l) = ((((SilcUInt64)SILC_GET_WORD((cp))) << 32) |            \
345          ((SilcUInt64)SILC_GET_WORD((cp) + 4)));                \
346 } while(0)
347 /***/
348
349 /****d* silcutil/SILCTypes/SILC_GET16_LSB
350  *
351  * NAME
352  *
353  *    #define SILC_GET16_MSB ...
354  *
355  * DESCRIPTION
356  *
357  *    Return two 8-bit bytes, least significant bytes first.
358  *
359  * SOURCE
360  */
361 #if defined(SILC_I486) && defined(__GNUC__)
362 #define SILC_GET16_LSB(l, cp) (l) = (*(SilcUInt16 *)(cp))
363 #else
364 #define SILC_GET16_LSB(l, cp)                           \
365 do {                                                    \
366   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
367     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);            \
368 } while(0)
369 #endif /* SILC_I486 && __GNUC__ */
370 /***/
371
372 /****d* silcutil/SILCTypes/SILC_GET32_LSB
373  *
374  * NAME
375  *
376  *    #define SILC_GET32_LSB ...
377  *
378  * DESCRIPTION
379  *
380  *    Return four 8-bit bytes, least significant bytes first.
381  *
382  * SOURCE
383  */
384 #if defined(SILC_I486) && defined(__GNUC__)
385 #define SILC_GET32_LSB(l, cp) (l) = (*(SilcUInt32 *)(cp))
386 #else
387 #define SILC_GET32_LSB(l, cp)                           \
388 do {                                                    \
389   (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])                \
390     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
391     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
392     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);           \
393 } while(0)
394 #endif /* SILC_I486 && __GNUC__ */
395
396 /* Same as upper but XOR the result always. Special purpose macro. */
397 #if defined(SILC_I486) && defined(__GNUC__)
398 #define SILC_GET32_X_LSB(l, cp) (l) ^= (*(SilcUInt32 *)(cp))
399 #else
400 #define SILC_GET32_X_LSB(l, cp)                         \
401   (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])               \
402     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)             \
403     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)            \
404     | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
405 #endif /* SILC_I486 && __GNUC__ */
406 /***/
407
408 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
409  *
410  * NAME
411  *
412  *    #define SILC_PUT16_MSB ...
413  *
414  * DESCRIPTION
415  *
416  *    Put two 8-bit bytes, most significant bytes first.
417  *
418  * SOURCE
419  */
420 #define SILC_PUT16_MSB(l, cp)                   \
421 do {                                            \
422   (cp)[0] = (SilcUInt8)((l) >> 8);              \
423   (cp)[1] = (SilcUInt8)(l);                     \
424 } while(0)
425 /***/
426
427 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
428  *
429  * NAME
430  *
431  *    #define SILC_PUT32_MSB ...
432  *
433  * DESCRIPTION
434  *
435  *    Put four 8-bit bytes, most significant bytes first.
436  *
437  * SOURCE
438  */
439 #define SILC_PUT32_MSB(l, cp)                   \
440 do {                                            \
441   (cp)[0] = (SilcUInt8)((l) >> 24);             \
442   (cp)[1] = (SilcUInt8)((l) >> 16);             \
443   (cp)[2] = (SilcUInt8)((l) >> 8);              \
444   (cp)[3] = (SilcUInt8)(l);                     \
445 } while(0)
446 /***/
447
448 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
449  *
450  * NAME
451  *
452  *    #define SILC_PUT64_MSB ...
453  *
454  * DESCRIPTION
455  *
456  *    Put eight 8-bit bytes, most significant bytes first.
457  *
458  * SOURCE
459  */
460 #define SILC_PUT64_MSB(l, cp)                                   \
461 do {                                                            \
462   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
463   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
464 } while(0)
465 /***/
466
467 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
468  *
469  * NAME
470  *
471  *    #define SILC_PUT16_LSB ...
472  *
473  * DESCRIPTION
474  *
475  *    Put two 8-bit bytes, least significant bytes first.
476  *
477  * SOURCE
478  */
479 #if defined(SILC_I486) && defined(__GNUC__)
480 #define SILC_PUT16_LSB(l, cp) (*(SilcUInt16 *)(cp)) = (l)
481 #else
482 #define SILC_PUT16_LSB(l, cp)                   \
483 do  {                                           \
484   (cp)[0] = (SilcUInt8)(l);                     \
485   (cp)[1] = (SilcUInt8)((l) >> 8);              \
486 } while(0)
487 #endif /* SILC_I486 && __GNUC__ */
488 /***/
489
490 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
491  *
492  * NAME
493  *
494  *    #define SILC_PUT32_LSB ...
495  *
496  * DESCRIPTION
497  *
498  *    Put four 8-bit bytes, least significant bytes first.
499  *
500  * SOURCE
501  */
502 #if defined(SILC_I486) && defined(__GNUC__)
503 #define SILC_PUT32_LSB(l, cp) (*(SilcUInt32 *)(cp)) = (l)
504 #else
505 #define SILC_PUT32_LSB(l, cp)                   \
506 do {                                            \
507   (cp)[0] = (SilcUInt8)(l);                     \
508   (cp)[1] = (SilcUInt8)((l) >> 8);              \
509   (cp)[2] = (SilcUInt8)((l) >> 16);             \
510   (cp)[3] = (SilcUInt8)((l) >> 24);             \
511 } while(0)
512 #endif /* SILC_I486 && __GNUC__ */
513 /***/
514
515 /****d* silcutil/SILCTypes/SILC_SWAB_16
516  *
517  * NAME
518  *
519  *    #define SILC_SWAB_16 ...
520  *
521  * DESCRIPTION
522  *
523  *    Swabs 16-bit unsigned integer byte order.
524  *
525  * SOURCE
526  */
527 #define SILC_SWAB_16(l)                                         \
528   ((SilcUInt16)(((SilcUInt16)(l) & (SilcUInt16)0x00FFU) << 8) | \
529                (((SilcUInt16)(l) & (SilcUInt16)0xFF00U) >> 8))
530 /***/
531
532 /****d* silcutil/SILCTypes/SILC_SWAB_32
533  *
534  * NAME
535  *
536  *    #define SILC_SWAB_32 ...
537  *
538  * DESCRIPTION
539  *
540  *    Swabs 32-bit unsigned integer byte order.
541  *
542  * SOURCE
543  */
544 #define SILC_SWAB_32(l)                                                 \
545   ((SilcUInt32)(((SilcUInt32)(l) & (SilcUInt32)0x000000FFUL) << 24) |   \
546                (((SilcUInt32)(l) & (SilcUInt32)0x0000FF00UL) << 8)  |   \
547                (((SilcUInt32)(l) & (SilcUInt32)0x00FF0000UL) >> 8)  |   \
548                (((SilcUInt32)(l) & (SilcUInt32)0xFF000000UL) >> 24))
549 /***/
550
551 /****d* silcutil/SILCTypes/SILC_PTR_TO_32
552  *
553  * NAME
554  *
555  *    #define SILC_PTR_TO_32 ...
556  *
557  * DESCRIPTION
558  *
559  *    Type casts a pointer's value into a 32-bit integer.  Use this to
560  *    avoid compiler warnings when type casting pointers to integers
561  *    of different size.
562  *
563  * SOURCE
564  */
565 #if SILC_SIZEOF_VOID_P < 8
566 #define SILC_PTR_TO_32(_ptr__) ((SilcUInt32)(_ptr__))
567 #else
568 #define SILC_PTR_TO_32(_ptr__)                                          \
569   ((SilcUInt32)((SilcUInt64)(_ptr__) & (SilcUInt32)0xFFFFFFFFUL))
570 #endif
571 /***/
572
573 /****d* silcutil/SILCTypes/SILC_PTR_TO_64
574  *
575  * NAME
576  *
577  *    #define SILC_PTR_TO_64 ...
578  *
579  * DESCRIPTION
580  *
581  *    Type casts a pointer's value into a 64-bit integer.  Use this to
582  *    avoid compiler warnings when type casting pointers to integers
583  *    of different size.
584  *
585  * SOURCE
586  */
587 #if SILC_SIZEOF_VOID_P < 8
588 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt32)(_ptr__)))
589 #else
590 #define SILC_PTR_TO_64(_ptr__) ((SilcUInt64)((SilcUInt64)(_ptr__)))
591 #endif
592 /***/
593
594 /****d* silcutil/SILCTypes/SILC_32_TO_PTR
595  *
596  * NAME
597  *
598  *    #define SILC_32_TO_PTR ...
599  *
600  * DESCRIPTION
601  *
602  *    Type casts a 32-bit integer value into a pointer.  Use this to
603  *    avoid compiler warnings when type casting integers to pointers of
604  *    different size.
605  *
606  * SOURCE
607  */
608 #if SILC_SIZEOF_VOID_P < 8
609 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt32)(_ival__)))
610 #else
611 #define SILC_32_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
612 #endif
613 /***/
614
615 /****d* silcutil/SILCTypes/SILC_64_TO_PTR
616  *
617  * NAME
618  *
619  *    #define SILC_64_TO_PTR ...
620  *
621  * DESCRIPTION
622  *
623  *    Type casts a 64-bit integer value into a pointer.  Use this to
624  *    avoid compiler warnings when type casting integers to pointers of
625  *    different size.
626  *
627  * SOURCE
628  */
629 #if SILC_SIZEOF_VOID_P < 8
630 #define SILC_64_TO_PTR(_ival__)                                         \
631   ((void *)((SilcUInt32)((SilcUInt64)(_ival__) & (SilcUInt32)0xFFFFFFFFUL)))
632 #else
633 #define SILC_64_TO_PTR(_ival__) ((void *)((SilcUInt64)(_ival__)))
634 #endif
635 /***/
636
637 typedef char __check_size1[sizeof(SilcInt8)   ==        1 ? 1 : -1];
638 typedef char __check_size2[sizeof(SilcUInt8)  ==        1 ? 1 : -1];
639 typedef char __check_size3[sizeof(SilcInt16)  ==        2 ? 1 : -1];
640 typedef char __check_size4[sizeof(SilcUInt16) ==        2 ? 1 : -1];
641 typedef char __check_size5[sizeof(SilcInt32)  ==        4 ? 1 : -1];
642 typedef char __check_size6[sizeof(SilcUInt32) ==        4 ? 1 : -1];
643
644 #endif /* SILCTYPES_H */