4df651037aa0a069a472d3dd224056bb71127014
[strongswan.git] / src / libstrongswan / definitions.h
1 /**
2 * @file definitions.h
3 *
4 * @brief General purpose definitions and macros.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 * Copyright (C) 1998, 1999 D. Hugh Redelmeier. (Endian stuff)
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #ifndef DEFINITIONS_H_
25 #define DEFINITIONS_H_
26
27 /* stolen from strongswan */
28 #if linux
29 # if defined(i386) && !defined(__i386__)
30 # define __i386__ 1
31 # define MYHACKFORTHIS 1
32 # endif
33 # include <endian.h>
34 # ifdef MYHACKFORTHIS
35 # undef __i386__
36 # undef MYHACKFORTHIS
37 # endif
38 #elif !(defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN) && defined(BYTE_ORDER))
39 /* we don't know how to do this, so we require the macros to be defined
40 * with compiler flags:
41 * -DBIG_ENDIAN=4321 -DLITTLE_ENDIAN=1234 -DBYTE_ORDER=BIG_ENDIAN
42 * or -DBIG_ENDIAN=4321 -DLITTLE_ENDIAN=1234 -DBYTE_ORDER=LITTLE_ENDIAN
43 * Thse match the GNU definitions
44 */
45 # include <sys/endian.h>
46 #endif
47
48 #ifndef BIG_ENDIAN
49 #error "BIG_ENDIAN must be defined"
50 #endif
51
52 #ifndef LITTLE_ENDIAN
53 #error "LITTLE_ENDIAN must be defined"
54 #endif
55
56 #ifndef BYTE_ORDER
57 #error "BYTE_ORDER must be defined"
58 #endif
59
60 /**
61 * Default length for various auxiliary text buffers
62 */
63 #define BUF_LEN 512
64
65 /**
66 * Macro compares two strings for equality
67 */
68 #define streq(x,y) (strcmp(x, y) == 0)
69
70 /**
71 * Macro compares two binary blobs for equality
72 */
73 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
74
75 /**
76 * Macro gives back larger of two values.
77 */
78 #define max(x,y) ((x) > (y) ? (x):(y))
79
80 /**
81 * Macro gives back smaller of two values.
82 */
83 #define min(x,y) ((x) < (y) ? (x):(y))
84
85 /**
86 * Debug macro to follow control flow
87 */
88 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
89
90 /**
91 * Macro to allocate a sized type.
92 *
93 * @param thing object on which a sizeof is performed
94 * @return poiner to allocated memory
95 */
96 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
97
98
99 /**
100 * Mapping entry which defines the end of a mapping_t array.
101 */
102 #define MAPPING_END (-1)
103
104 typedef struct mapping_t mapping_t;
105
106 /**
107 * @brief Mapping entry, where enum-to-string mappings are stored.
108 */
109 struct mapping_t
110 {
111 /**
112 * Enumeration value.
113 */
114 int value;
115
116 /**
117 * Mapped string.
118 */
119 char *string;
120 };
121
122
123 /**
124 * @brief Find a mapping_string in the mapping[].
125 *
126 * @param mappings mappings array
127 * @param value enum-value to get the string from
128 *
129 */
130 char *mapping_find(mapping_t *mappings, int value);
131
132 #endif /*DEFINITIONS_H_*/