updates
[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         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)     \
267             | ((SilcUInt32)(SilcUInt8)(cp)[1])
268 /***/
269
270 /****d* silcutil/SILCTypes/SILC_GET32_MSB
271  *
272  * NAME
273  *
274  *    #define SILC_GET32_MSB ...
275  *
276  * DESCRIPTION
277  *
278  *    Return four 8-bit bytes, most significant bytes first.
279  *
280  * SOURCE
281  */
282 #define SILC_GET32_MSB(l, cp)                           \
283         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
284             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
285             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
286             | ((SilcUInt32)(SilcUInt8)(cp)[3])
287 /***/
288
289 /****d* silcutil/SILCTypes/SILC_GET64_MSB
290  *
291  * NAME
292  *
293  *    #define SILC_GET64_MSB ...
294  *
295  * DESCRIPTION
296  *
297  *    Return eight 8-bit bytes, most significant bytes first.
298  *
299  * SOURCE
300  */
301 #define SILC_GET64_MSB(l, cp)                           \
302        (l) = ((((SilcUInt64)GET_WORD((cp))) << 32) |    \
303               ((SilcUInt64)GET_WORD((cp) + 4)))
304 /***/
305
306 /****d* silcutil/SILCTypes/SILC_GET16_LSB
307  *
308  * NAME
309  *
310  *    #define SILC_GET16_MSB ...
311  *
312  * DESCRIPTION
313  *
314  *    Return two 8-bit bytes, least significant bytes first.
315  *
316  * SOURCE
317  */
318 #define SILC_GET16_LSB(l, cp)                           \
319         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
320             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)
321 /***/
322
323 /****d* silcutil/SILCTypes/SILC_GET32_LSB
324  *
325  * NAME
326  *
327  *    #define SILC_GET32_LSB ...
328  *
329  * DESCRIPTION
330  *
331  *    Return four 8-bit bytes, least significant bytes first.
332  *
333  * SOURCE
334  */
335 #define SILC_GET32_LSB(l, cp)                           \
336         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
337             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
338             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
339             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
340
341 /* Same as upper but XOR the result always. Special purpose macro. */
342 #define SILC_GET32_X_LSB(l, cp)                         \
343         (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])         \
344             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
345             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
346             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
347 /***/
348
349 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
350  *
351  * NAME
352  *
353  *    #define SILC_PUT16_MSB ...
354  *
355  * DESCRIPTION
356  *
357  *    Put two 8-bit bytes, most significant bytes first.
358  *
359  * SOURCE
360  */
361 #define SILC_PUT16_MSB(l, cp)                   \
362         (cp)[0] = l >> 8;                       \
363         (cp)[1] = l;
364 /***/
365
366 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
367  *
368  * NAME
369  *
370  *    #define SILC_PUT32_MSB ...
371  *
372  * DESCRIPTION
373  *
374  *    Put four 8-bit bytes, most significant bytes first.
375  *
376  * SOURCE
377  */
378 #define SILC_PUT32_MSB(l, cp)                   \
379         (cp)[0] = l >> 24;                      \
380         (cp)[1] = l >> 16;                      \
381         (cp)[2] = l >> 8;                       \
382         (cp)[3] = l;
383 /***/
384
385 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
386  *
387  * NAME
388  *
389  *    #define SILC_PUT64_MSB ...
390  *
391  * DESCRIPTION
392  *
393  *    Put eight 8-bit bytes, most significant bytes first.
394  *
395  * SOURCE
396  */
397 #define SILC_PUT64_MSB(l, cp)                                   \
398 do {                                                            \
399   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
400   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
401 } while(0)
402 /***/
403
404 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
405  *
406  * NAME
407  *
408  *    #define SILC_PUT16_LSB ...
409  *
410  * DESCRIPTION
411  *
412  *    Put two 8-bit bytes, least significant bytes first.
413  *
414  * SOURCE
415  */
416 #define SILC_PUT16_LSB(l, cp)                   \
417         (cp)[0] = l;                            \
418         (cp)[1] = l >> 8;
419 /***/
420
421 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
422  *
423  * NAME
424  *
425  *    #define SILC_PUT32_LSB ...
426  *
427  * DESCRIPTION
428  *
429  *    Put four 8-bit bytes, least significant bytes first.
430  *
431  * SOURCE
432  */
433 #define SILC_PUT32_LSB(l, cp)                   \
434         (cp)[0] = l;                            \
435         (cp)[1] = l >> 8;                       \
436         (cp)[2] = l >> 16;                      \
437         (cp)[3] = l >> 24;
438 /***/
439
440 #endif /* SILCTYPES_H */