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