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