(no commit message)
[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-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 * Copyright (C) 1998, 1999 D. Hugh Redelmeier. (Endian stuff)
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 #ifndef DEFINITIONS_H_
26 #define DEFINITIONS_H_
27
28 #include <stddef.h>
29
30 #define BITS_PER_BYTE 8
31 #define RSA_MIN_OCTETS (1024 / BITS_PER_BYTE)
32 #define RSA_MIN_OCTETS_UGH "RSA modulus too small for security: less than 1024 bits"
33 #define RSA_MAX_OCTETS (8192 / BITS_PER_BYTE)
34 #define RSA_MAX_OCTETS_UGH "RSA modulus too large: more than 8192 bits"
35
36 /**
37 * Default length for various auxiliary text buffers
38 */
39 #define BUF_LEN 512
40
41 /**
42 * Macro compares two strings for equality
43 */
44 #define streq(x,y) (strcmp(x, y) == 0)
45
46 /**
47 * Macro compares two binary blobs for equality
48 */
49 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
50
51 /**
52 * Macro gives back larger of two values.
53 */
54 #define max(x,y) ((x) > (y) ? (x):(y))
55
56 /**
57 * Macro gives back smaller of two values.
58 */
59 #define min(x,y) ((x) < (y) ? (x):(y))
60
61 /**
62 * Call destructor of a object if object != NULL
63 */
64 #define DESTROY_IF(obj) if (obj) obj->destroy(obj)
65
66 /**
67 * Debug macro to follow control flow
68 */
69 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
70
71 /**
72 * Macro to allocate a sized type.
73 */
74 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
75
76 /**
77 * Assign a function as a class method
78 */
79 #define ASSIGN(method, function) (method = (typeof(method))function)
80
81
82 /**
83 * Mapping entry which defines the end of a mapping_t array.
84 */
85 #define MAPPING_END (-1)
86
87 typedef struct mapping_t mapping_t;
88
89 /**
90 * @brief Mapping entry, where enum-to-string mappings are stored.
91 */
92 struct mapping_t
93 {
94 /**
95 * Enumeration value.
96 */
97 int value;
98
99 /**
100 * Mapped string.
101 */
102 char *string;
103 };
104
105 /**
106 * @brief Find a mapping_string in the mapping[].
107 *
108 * @param mappings mappings array
109 * @param value enum-value to get the string from
110 *
111 */
112 char *mapping_find(mapping_t *mappings, int value);
113
114 /**
115 * @brief Describes an enumeration
116 * enum_name() returns the name of an enum value, or NULL if invalid.
117 */
118 typedef const struct enum_names enum_names;
119
120 struct enum_names {
121 unsigned long en_first; /* first value in range */
122 unsigned long en_last; /* last value in range (inclusive) */
123 const char *const *en_names;
124 enum_names *en_next_range; /* descriptor of next range */
125 };
126
127 /**
128 * @brief Returns the name of an enum value, or NULL if invalid
129 */
130 const char *enum_name(enum_names *ed, unsigned long val);
131
132 #endif /*DEFINITIONS_H_*/