cast fixes.
[silc.git] / lib / silcutil / silctypes.h
1 /*
2
3   silctypes.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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 #ifndef __cplusplus
83 #ifndef bool
84 #define bool unsigned char
85 #endif
86 #endif
87 /***/
88
89 #define silc_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
90
91 #if SILC_SIZEOF_SHORT > 2
92 #error "size of the short must be 2 bytes"
93 #endif
94
95 /****d* silcutil/SILCTypes/SilcUInt8
96  *
97  * NAME
98  *
99  *    typedef unsigned char SilcUInt8;
100  *
101  * DESCRIPTION
102  *
103  *    8-bit unsigned integer.
104  *
105  * SOURCE
106  */
107 typedef unsigned char SilcUInt8;
108 /***/
109
110 /****d* silcutil/SILCTypes/SilcInt8
111  *
112  * NAME
113  *
114  *    typedef signed char SilcInt8;
115  *
116  * DESCRIPTION
117  *
118  *    8-bit signed integer.
119  *
120  * SOURCE
121  */
122 typedef signed char SilcInt8;
123 /***/
124
125 /****d* silcutil/SILCTypes/SilcUInt16
126  *
127  * NAME
128  *
129  *    typedef unsigned short SilcUInt16;
130  *
131  * DESCRIPTION
132  *
133  *    16-bit unsigned integer.  Guaranteed to be 16-bits.
134  *
135  * SOURCE
136  */
137 typedef unsigned short SilcUInt16;
138 /***/
139
140 /****d* silcutil/SILCTypes/SilcInt16
141  *
142  * NAME
143  *
144  *    typedef signed short SilcInt16;
145  *
146  * DESCRIPTION
147  *
148  *    16-bit signed integer.  Guaranteed to be 16-bits.
149  *
150  * SOURCE
151  */
152 typedef signed short SilcInt16;
153 /***/
154
155 /****d* silcutil/SILCTypes/SilcUInt32
156  *
157  * NAME
158  *
159  *    typedef unsigned long SilcUInt32;
160  *
161  * DESCRIPTION
162  *
163  *    32-bit unsigned integer.  Guaranteed to be 32-bits.
164  *
165  * SOURCE
166  */
167 #if SILC_SIZEOF_LONG == 4
168 typedef unsigned long SilcUInt32;
169 typedef signed long SilcInt32;
170 #else
171 #if SILC_SIZEOF_INT == 4
172 typedef unsigned int SilcUInt32;
173 typedef signed int SilcInt32;
174 #else
175 #if SILC_SIZEOF_LONG_LONG >= 4
176 #ifndef WIN32
177 typedef unsigned long long SilcUInt32;
178 typedef signed long long SilcInt32;
179 #endif
180 #endif
181 #endif
182 #endif
183 /***/
184
185 /****d* silcutil/SILCTypes/SilcInt32
186  *
187  * NAME
188  *
189  *    typedef signed long SilcInt32;
190  *
191  * DESCRIPTION
192  *
193  *    32-bit signed integer.  Guaranteed to be 32-bits.
194  *
195  ***/
196
197 /****d* silcutil/SILCTypes/SilcUInt64
198  *
199  * NAME
200  *
201  *    typedef unsigned long long SilcUInt64;
202  *
203  * DESCRIPTION
204  *
205  *    64-bit unsigned integer.  Guaranteed to be 64-bits on systems that
206  *    support it.
207  *
208  * SOURCE
209  */
210 #if SILC_SIZEOF_LONG >= 8
211 typedef unsigned long SilcUInt64;
212 typedef signed long SilcInt64;
213 #else
214 #if SILC_SIZEOF_LONG_LONG >= 8
215 #ifndef WIN32
216 typedef unsigned long long SilcUInt64;
217 typedef signed long long SilcInt64;
218 #else
219 typedef SilcUInt32 SilcUInt64; /* XXX Use Windows's own 64 bit types */
220 typedef SilcInt32 SilcInt64;
221 #endif
222 #else
223 typedef SilcUInt32 SilcUInt64;
224 typedef SilcInt32 SilcInt64;
225 #endif
226 #endif
227 /***/
228
229 /****d* silcutil/SILCTypes/SilcInt64
230  *
231  * NAME
232  *
233  *    typedef signed long long SilcInt64;
234  *
235  * DESCRIPTION
236  *
237  *    64-bit signed integer.  Guaranteed to be 64-bits on systems that
238  *    support it.
239  *
240  ***/
241
242 #if SILC_SIZEOF_VOID_P < 4
243 typedef SilcUInt32 * void *;
244 #endif
245
246 /* Macros */
247
248 #define GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24     \
249                     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
250                     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
251                     | ((SilcUInt32)(SilcUInt8)(cp)[3])
252
253 /****d* silcutil/SILCTypes/SILC_GET16_MSB
254  *
255  * NAME
256  *
257  *    #define SILC_GET16_MSB ...
258  *
259  * DESCRIPTION
260  *
261  *    Return two 8-bit bytes, most significant bytes first.
262  *
263  * SOURCE
264  */
265 #define SILC_GET16_MSB(l, cp)                           \
266 do {                                                    \
267         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)     \
268             | ((SilcUInt32)(SilcUInt8)(cp)[1]);         \
269 } while(0)
270 /***/
271
272 /****d* silcutil/SILCTypes/SILC_GET32_MSB
273  *
274  * NAME
275  *
276  *    #define SILC_GET32_MSB ...
277  *
278  * DESCRIPTION
279  *
280  *    Return four 8-bit bytes, most significant bytes first.
281  *
282  * SOURCE
283  */
284 #define SILC_GET32_MSB(l, cp)                           \
285 do {                                                    \
286         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
287             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
288             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
289             | ((SilcUInt32)(SilcUInt8)(cp)[3]);         \
290 } while(0)
291 /***/
292
293 /****d* silcutil/SILCTypes/SILC_GET64_MSB
294  *
295  * NAME
296  *
297  *    #define SILC_GET64_MSB ...
298  *
299  * DESCRIPTION
300  *
301  *    Return eight 8-bit bytes, most significant bytes first.
302  *
303  * SOURCE
304  */
305 #define SILC_GET64_MSB(l, cp)                           \
306 do {                                                    \
307        (l) = ((((SilcUInt64)GET_WORD((cp))) << 32) |    \
308               ((SilcUInt64)GET_WORD((cp) + 4)));        \
309 } while(0)
310 /***/
311
312 /****d* silcutil/SILCTypes/SILC_GET16_LSB
313  *
314  * NAME
315  *
316  *    #define SILC_GET16_MSB ...
317  *
318  * DESCRIPTION
319  *
320  *    Return two 8-bit bytes, least significant bytes first.
321  *
322  * SOURCE
323  */
324 #define SILC_GET16_LSB(l, cp)                           \
325 do {                                                    \
326         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
327             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8);    \
328 } while(0)
329 /***/
330
331 /****d* silcutil/SILCTypes/SILC_GET32_LSB
332  *
333  * NAME
334  *
335  *    #define SILC_GET32_LSB ...
336  *
337  * DESCRIPTION
338  *
339  *    Return four 8-bit bytes, least significant bytes first.
340  *
341  * SOURCE
342  */
343 #define SILC_GET32_LSB(l, cp)                           \
344 do {                                                    \
345         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
346             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
347             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
348             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24);   \
349 } while(0)
350
351 /* Same as upper but XOR the result always. Special purpose macro. */
352 #define SILC_GET32_X_LSB(l, cp)                         \
353         (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])         \
354             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
355             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
356             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
357 /***/
358
359 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
360  *
361  * NAME
362  *
363  *    #define SILC_PUT16_MSB ...
364  *
365  * DESCRIPTION
366  *
367  *    Put two 8-bit bytes, most significant bytes first.
368  *
369  * SOURCE
370  */
371 #define SILC_PUT16_MSB(l, cp)                   \
372 do {                                            \
373         (cp)[0] = (SilcUInt8)((l) >> 8);        \
374         (cp)[1] = (SilcUInt8)(l);               \
375 } while(0)
376 /***/
377
378 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
379  *
380  * NAME
381  *
382  *    #define SILC_PUT32_MSB ...
383  *
384  * DESCRIPTION
385  *
386  *    Put four 8-bit bytes, most significant bytes first.
387  *
388  * SOURCE
389  */
390 #define SILC_PUT32_MSB(l, cp)                   \
391 do {                                            \
392         (cp)[0] = (SilcUInt8)((l) >> 24);       \
393         (cp)[1] = (SilcUInt8)((l) >> 16);       \
394         (cp)[2] = (SilcUInt8)((l) >> 8);        \
395         (cp)[3] = (SilcUInt8)(l);               \
396 } while(0)
397 /***/
398
399 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
400  *
401  * NAME
402  *
403  *    #define SILC_PUT64_MSB ...
404  *
405  * DESCRIPTION
406  *
407  *    Put eight 8-bit bytes, most significant bytes first.
408  *
409  * SOURCE
410  */
411 #define SILC_PUT64_MSB(l, cp)                                   \
412 do {                                                            \
413   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
414   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
415 } while(0)
416 /***/
417
418 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
419  *
420  * NAME
421  *
422  *    #define SILC_PUT16_LSB ...
423  *
424  * DESCRIPTION
425  *
426  *    Put two 8-bit bytes, least significant bytes first.
427  *
428  * SOURCE
429  */
430 #define SILC_PUT16_LSB(l, cp)                   \
431 do  {                                           \
432         (cp)[0] = (SilcUInt8)(l);               \
433         (cp)[1] = (SilcUInt8)((l) >> 8);        \
434 } while(0)
435 /***/
436
437 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
438  *
439  * NAME
440  *
441  *    #define SILC_PUT32_LSB ...
442  *
443  * DESCRIPTION
444  *
445  *    Put four 8-bit bytes, least significant bytes first.
446  *
447  * SOURCE
448  */
449 #define SILC_PUT32_LSB(l, cp)                   \
450 do {                                            \
451         (cp)[0] = (SilcUInt8)(l);               \
452         (cp)[1] = (SilcUInt8)((l) >> 8);        \
453         (cp)[2] = (SilcUInt8)((l) >> 16);       \
454         (cp)[3] = (SilcUInt8)((l) >> 24);       \
455 } while(0)
456 /***/
457
458 #endif /* SILCTYPES_H */