Added cross-reference support for document generator.
[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 #ifndef TRUE
35 #define TRUE 1
36 #endif
37 #ifndef FALSE
38 #define FALSE 0
39 #endif
40
41 /* Define offsetof */
42 #ifndef offsetof
43 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
44 #endif
45
46 #if SILC_SIZEOF_SHORT > 2
47 #error "size of the short must be 2 bytes"
48 #endif
49
50 /****d* silcutil/SILCTypes/SilcUInt8
51  *
52  * NAME
53  *
54  *    typedef unsigned char SilcUInt8;
55  *
56  * DESCRIPTION
57  *
58  *    8-bit unsigned integer.
59  *
60  * SOURCE
61  */
62 typedef unsigned char SilcUInt8;
63 /***/
64
65 /****d* silcutil/SILCTypes/SilcInt8
66  *
67  * NAME
68  *
69  *    typedef signed char SilcInt8;
70  *
71  * DESCRIPTION
72  *
73  *    8-bit signed integer.
74  *
75  * SOURCE
76  */
77 typedef signed char SilcInt8;
78 /***/
79
80 /****d* silcutil/SILCTypes/SilcUInt16
81  *
82  * NAME
83  *
84  *    typedef unsigned short SilcUInt16;
85  *
86  * DESCRIPTION
87  *
88  *    16-bit unsigned integer.  Guaranteed to be 16-bits.
89  *
90  * SOURCE
91  */
92 typedef unsigned short SilcUInt16;
93 /***/
94
95 /****d* silcutil/SILCTypes/SilcInt16
96  *
97  * NAME
98  *
99  *    typedef signed short SilcInt16;
100  *
101  * DESCRIPTION
102  *
103  *    16-bit signed integer.  Guaranteed to be 16-bits.
104  *
105  * SOURCE
106  */
107 typedef signed short SilcInt16;
108 /***/
109
110 /****d* silcutil/SILCTypes/SilcUInt32
111  *
112  * NAME
113  *
114  *    typedef unsigned long SilcUInt32;
115  *
116  * DESCRIPTION
117  *
118  *    32-bit unsigned integer.  Guaranteed to be 32-bits.
119  *
120  * SOURCE
121  */
122 #if SILC_SIZEOF_LONG == 4
123 typedef unsigned long SilcUInt32;
124 typedef signed long SilcInt32;
125 #else
126 #if SILC_SIZEOF_INT == 4
127 typedef unsigned int SilcUInt32;
128 typedef signed int SilcInt32;
129 #else
130 #if SILC_SIZEOF_LONG_LONG >= 4
131 #ifndef WIN32
132 typedef unsigned long long SilcUInt32;
133 typedef signed long long SilcInt32;
134 #endif
135 #endif
136 #endif
137 #endif
138 /***/
139
140 /****d* silcutil/SILCTypes/SilcInt32
141  *
142  * NAME
143  *
144  *    typedef signed long SilcInt32;
145  *
146  * DESCRIPTION
147  *
148  *    32-bit signed integer.  Guaranteed to be 32-bits.
149  *
150  ***/
151
152 /****d* silcutil/SILCTypes/SilcUInt64
153  *
154  * NAME
155  *
156  *    typedef unsigned long long SilcUInt64;
157  *
158  * DESCRIPTION
159  *
160  *    64-bit unsigned integer.  Guaranteed to be 64-bits on systems that
161  *    support it.
162  *
163  * SOURCE
164  */
165 #if SILC_SIZEOF_LONG >= 8
166 typedef unsigned long SilcUInt64;
167 typedef signed long SilcInt64;
168 #else
169 #if SILC_SIZEOF_LONG_LONG >= 8
170 #ifndef WIN32
171 typedef unsigned long long SilcUInt64;
172 typedef signed long long SilcInt64;
173 #else
174 typedef SilcUInt32 SilcUInt64; /* XXX Use Windows's own 64 bit types */
175 typedef SilcInt32 SilcInt64;
176 #endif
177 #else
178 typedef SilcUInt32 SilcUInt64;
179 typedef SilcInt32 SilcInt64;
180 #endif
181 #endif
182 /***/
183
184 /****d* silcutil/SILCTypes/SilcInt64
185  *
186  * NAME
187  *
188  *    typedef signed long long SilcInt64;
189  *
190  * DESCRIPTION
191  *
192  *    64-bit signed integer.  Guaranteed to be 64-bits on systems that
193  *    support it.
194  *
195  ***/
196
197 #if SILC_SIZEOF_VOID_P < 4
198 typedef SilcUInt32 * void *;
199 #endif
200
201 #ifndef __cplusplus
202 #ifndef bool
203 #define bool unsigned char
204 #endif
205 #endif
206
207 #define GET_WORD(cp) ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24     \
208                     | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
209                     | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
210                     | ((SilcUInt32)(SilcUInt8)(cp)[3])
211
212 /****d* silcutil/SILCTypes/SILC_GET16_MSB
213  *
214  * NAME
215  *
216  *    #define SILC_GET16_MSB ...
217  *
218  * DESCRIPTION
219  *
220  *    Return two 8-bit bytes, most significant bytes first.
221  *
222  * SOURCE
223  */
224 #define SILC_GET16_MSB(l, cp)                           \
225         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0] << 8)     \
226             | ((SilcUInt32)(SilcUInt8)(cp)[1])
227 /***/
228
229 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
230  *
231  * NAME
232  *
233  *    #define SILC_PUT16_MSB ...
234  *
235  * DESCRIPTION
236  *
237  *    Put two 8-bit bytes, most significant bytes first.
238  *
239  * SOURCE
240  */
241 #define SILC_PUT16_MSB(l, cp)                   \
242         (cp)[0] = l >> 8;                       \
243         (cp)[1] = l;
244 /***/
245
246 /****d* silcutil/SILCTypes/SILC_GET16_LSB
247  *
248  * NAME
249  *
250  *    #define SILC_GET16_MSB ...
251  *
252  * DESCRIPTION
253  *
254  *    Return two 8-bit bytes, least significant bytes first.
255  *
256  * SOURCE
257  */
258 #define SILC_GET16_LSB(l, cp)                           \
259         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
260             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)
261 /***/
262
263 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
264  *
265  * NAME
266  *
267  *    #define SILC_PUT16_LSB ...
268  *
269  * DESCRIPTION
270  *
271  *    Put two 8-bit bytes, least significant bytes first.
272  *
273  * SOURCE
274  */
275 #define SILC_PUT16_LSB(l, cp)                   \
276         (cp)[0] = l;                            \
277         (cp)[1] = l >> 8;
278 /***/
279
280 /****d* silcutil/SILCTypes/SILC_GET32_MSB
281  *
282  * NAME
283  *
284  *    #define SILC_GET32_MSB ...
285  *
286  * DESCRIPTION
287  *
288  *    Return four 8-bit bytes, most significant bytes first.
289  *
290  * SOURCE
291  */
292 #define SILC_GET32_MSB(l, cp)                           \
293         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
294             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
295             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
296             | ((SilcUInt32)(SilcUInt8)(cp)[3])
297 /***/
298
299 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
300  *
301  * NAME
302  *
303  *    #define SILC_PUT32_MSB ...
304  *
305  * DESCRIPTION
306  *
307  *    Put four 8-bit bytes, most significant bytes first.
308  *
309  * SOURCE
310  */
311 #define SILC_PUT32_MSB(l, cp)                   \
312         (cp)[0] = l >> 24;                      \
313         (cp)[1] = l >> 16;                      \
314         (cp)[2] = l >> 8;                       \
315         (cp)[3] = l;
316 /***/
317
318 /****d* silcutil/SILCTypes/SILC_GET32_LSB
319  *
320  * NAME
321  *
322  *    #define SILC_GET32_LSB ...
323  *
324  * DESCRIPTION
325  *
326  *    Return four 8-bit bytes, least significant bytes first.
327  *
328  * SOURCE
329  */
330 #define SILC_GET32_LSB(l, cp)                           \
331         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
332             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
333             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
334             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
335
336 /* Same as upper but XOR the result always. Special purpose macro. */
337 #define SILC_GET32_X_LSB(l, cp)                         \
338         (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])         \
339             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
340             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
341             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
342 /***/
343
344 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
345  *
346  * NAME
347  *
348  *    #define SILC_PUT32_LSB ...
349  *
350  * DESCRIPTION
351  *
352  *    Put four 8-bit bytes, least significant bytes first.
353  *
354  * SOURCE
355  */
356 #define SILC_PUT32_LSB(l, cp)                   \
357         (cp)[0] = l;                            \
358         (cp)[1] = l >> 8;                       \
359         (cp)[2] = l >> 16;                      \
360         (cp)[3] = l >> 24;
361 /***/
362
363 /****d* silcutil/SILCTypes/SILC_GET64_MSB
364  *
365  * NAME
366  *
367  *    #define SILC_GET64_MSB ...
368  *
369  * DESCRIPTION
370  *
371  *    Return eight 8-bit bytes, most significant bytes first.
372  *
373  * SOURCE
374  */
375 #define SILC_GET64_MSB(l, cp)                           \
376        (l) = ((((SilcUInt64)GET_WORD((cp))) << 32) |    \
377               ((SilcUInt64)GET_WORD((cp) + 4)))
378 /***/
379
380 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
381  *
382  * NAME
383  *
384  *    #define SILC_PUT64_MSB ...
385  *
386  * DESCRIPTION
387  *
388  *    Put eight 8-bit bytes, most significant bytes first.
389  *
390  * SOURCE
391  */
392 #define SILC_PUT64_MSB(l, cp)                                   \
393 do {                                                            \
394   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
395   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
396 } while(0)
397 /***/
398
399 #endif /* SILCTYPES_H */