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