Changed order of macros for documentation.
[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_GET32_MSB
230  *
231  * NAME
232  *
233  *    #define SILC_GET32_MSB ...
234  *
235  * DESCRIPTION
236  *
237  *    Return four 8-bit bytes, most significant bytes first.
238  *
239  * SOURCE
240  */
241 #define SILC_GET32_MSB(l, cp)                           \
242         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0]) << 24    \
243             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 16)    \
244             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 8)     \
245             | ((SilcUInt32)(SilcUInt8)(cp)[3])
246 /***/
247
248 /****d* silcutil/SILCTypes/SILC_GET64_MSB
249  *
250  * NAME
251  *
252  *    #define SILC_GET64_MSB ...
253  *
254  * DESCRIPTION
255  *
256  *    Return eight 8-bit bytes, most significant bytes first.
257  *
258  * SOURCE
259  */
260 #define SILC_GET64_MSB(l, cp)                           \
261        (l) = ((((SilcUInt64)GET_WORD((cp))) << 32) |    \
262               ((SilcUInt64)GET_WORD((cp) + 4)))
263 /***/
264
265 /****d* silcutil/SILCTypes/SILC_GET16_LSB
266  *
267  * NAME
268  *
269  *    #define SILC_GET16_MSB ...
270  *
271  * DESCRIPTION
272  *
273  *    Return two 8-bit bytes, least significant bytes first.
274  *
275  * SOURCE
276  */
277 #define SILC_GET16_LSB(l, cp)                           \
278         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
279             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)
280 /***/
281
282 /****d* silcutil/SILCTypes/SILC_GET32_LSB
283  *
284  * NAME
285  *
286  *    #define SILC_GET32_LSB ...
287  *
288  * DESCRIPTION
289  *
290  *    Return four 8-bit bytes, least significant bytes first.
291  *
292  * SOURCE
293  */
294 #define SILC_GET32_LSB(l, cp)                           \
295         (l) = ((SilcUInt32)(SilcUInt8)(cp)[0])          \
296             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
297             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
298             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
299
300 /* Same as upper but XOR the result always. Special purpose macro. */
301 #define SILC_GET32_X_LSB(l, cp)                         \
302         (l) ^= ((SilcUInt32)(SilcUInt8)(cp)[0])         \
303             | ((SilcUInt32)(SilcUInt8)(cp)[1] << 8)     \
304             | ((SilcUInt32)(SilcUInt8)(cp)[2] << 16)    \
305             | ((SilcUInt32)(SilcUInt8)(cp)[3] << 24)
306 /***/
307
308 /****d* silcutil/SILCTypes/SILC_PUT16_MSB
309  *
310  * NAME
311  *
312  *    #define SILC_PUT16_MSB ...
313  *
314  * DESCRIPTION
315  *
316  *    Put two 8-bit bytes, most significant bytes first.
317  *
318  * SOURCE
319  */
320 #define SILC_PUT16_MSB(l, cp)                   \
321         (cp)[0] = l >> 8;                       \
322         (cp)[1] = l;
323 /***/
324
325 /****d* silcutil/SILCTypes/SILC_PUT32_MSB
326  *
327  * NAME
328  *
329  *    #define SILC_PUT32_MSB ...
330  *
331  * DESCRIPTION
332  *
333  *    Put four 8-bit bytes, most significant bytes first.
334  *
335  * SOURCE
336  */
337 #define SILC_PUT32_MSB(l, cp)                   \
338         (cp)[0] = l >> 24;                      \
339         (cp)[1] = l >> 16;                      \
340         (cp)[2] = l >> 8;                       \
341         (cp)[3] = l;
342 /***/
343
344 /****d* silcutil/SILCTypes/SILC_PUT64_MSB
345  *
346  * NAME
347  *
348  *    #define SILC_PUT64_MSB ...
349  *
350  * DESCRIPTION
351  *
352  *    Put eight 8-bit bytes, most significant bytes first.
353  *
354  * SOURCE
355  */
356 #define SILC_PUT64_MSB(l, cp)                                   \
357 do {                                                            \
358   SILC_PUT32_MSB((SilcUInt32)((SilcUInt64)(l) >> 32), (cp));    \
359   SILC_PUT32_MSB((SilcUInt32)(l), (cp) + 4);                    \
360 } while(0)
361 /***/
362
363 /****d* silcutil/SILCTypes/SILC_PUT16_LSB
364  *
365  * NAME
366  *
367  *    #define SILC_PUT16_LSB ...
368  *
369  * DESCRIPTION
370  *
371  *    Put two 8-bit bytes, least significant bytes first.
372  *
373  * SOURCE
374  */
375 #define SILC_PUT16_LSB(l, cp)                   \
376         (cp)[0] = l;                            \
377         (cp)[1] = l >> 8;
378 /***/
379
380 /****d* silcutil/SILCTypes/SILC_PUT32_LSB
381  *
382  * NAME
383  *
384  *    #define SILC_PUT32_LSB ...
385  *
386  * DESCRIPTION
387  *
388  *    Put four 8-bit bytes, least significant bytes first.
389  *
390  * SOURCE
391  */
392 #define SILC_PUT32_LSB(l, cp)                   \
393         (cp)[0] = l;                            \
394         (cp)[1] = l >> 8;                       \
395         (cp)[2] = l >> 16;                      \
396         (cp)[3] = l >> 24;
397 /***/
398
399 #endif /* SILCTYPES_H */