Open SCAP Library

util.h
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <lkuklinek@redhat.com>
21 */
22
23
24 #ifndef OSCAP_UTIL_H_
25 #define OSCAP_UTIL_H_
26
27 #include <stdbool.h>
28 #include <assert.h>
29 #include "public/oscap.h"
30 #include "alloc.h"
31 #include <stdarg.h>
32 #include <string.h>
33
34 #ifndef __attribute__nonnull__
35 #define __attribute__nonnull__(x) assert((x) != NULL)
36 #endif
37
38 /*
39 * Start a list of declarations that should not be available from outside the
40 * library. Must be matched with OSCAP_HIDDEN_END.
41 */
42 #ifndef OSCAP_HIDDEN_START
43 #define OSCAP_HIDDEN_START _Pragma("GCC visibility push(hidden)")
44 #endif
45
46 /*
47 * Start a list of declarations that should not be available from outside the
48 * library started by OSCAP_HIDDEN_END.
49 */
50 #ifndef OSCAP_HIDDEN_END
51 #define OSCAP_HIDDEN_END _Pragma("GCC visibility pop")
52 #endif
53
54 OSCAP_HIDDEN_START
55
59 typedef void (*oscap_destruct_func) (void *);
60
64 typedef void* (*oscap_clone_func) (void *);
65
69 typedef void (*oscap_consumer_func) (void *, void *);
70
81 #define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
82 RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
83
92 #define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
93 OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
94
103 #define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
104 OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
105
114 #define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
115 OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
116
125 #define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
126 OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
127
128 #define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
129 #define OSCAP_ITERATOR(n) struct n##_iterator*
130 #define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
131 #define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
132 #define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
133 #define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
134 #define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
135 #define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
136 #define OSCAP_ITERATOR_GEN_T(t,n) OSCAP_ITERATOR_FWD(n) OSCAP_ITERATOR_HAS_MORE(n) OSCAP_ITERATOR_RESET(n) OSCAP_ITERATOR_NEXT(t,n) OSCAP_ITERATOR_FREE(n)
137 #define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
138
139 #define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
140 void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
141 #define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
142 #define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
143
144
154 #define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
155 struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
156 { return oscap_iterator_new((CONV(item))->MNAME); }
157
166 #define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
167
173 #define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
174
184 #define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
185 RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
186 { return oscap_htable_get(item->MEXP, key); }
187
196 #define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
197
206 #define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
207
208 #define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
209
221 #define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
222 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
223 { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
224
229 #define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
230 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
231 { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
232
237 #define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
238 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
239 { obj->MNAME = ASSIGNER(newval); return true; }
240
245 #define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
246 OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
247
253 #define OSCAP_SETTER_STRING(SNAME, MNAME) \
254 OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
255
259 #define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
260 OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
261
265 #define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
266 OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
267 OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
268
272 #define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
273 OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
274
279 #define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
280 OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
281 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
282
292 #define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
293 bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
294 { oscap_list_add(obj->MNAME, item); return true; }
295
296 /* Generate iterator getter and list inserter */
297 #define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
298 OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
299 /* Generate iterator getter, list inserter, and iterator manipulation functions. */
300 #define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
301 OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
302
310 struct oscap_string_map {
311 const int value; /* integer/enum value */
312 const char *string; /* string representation of the value */
313 };
314
321 int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
322
329 const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
330
335 static inline char *oscap_strdup(const char *str) {
336 if (str == NULL)
337 return NULL;
338
339 #ifdef _MSC_VER
340 return _strdup(str);
341 #else
342 return strdup(str);
343 #endif
344 }
345
354 char **oscap_split(char *str, const char *delim);
355
356
358 static inline int oscap_strcmp(const char *s1, const char *s2) {
359 if (s1 == NULL) s1 = "";
360 if (s2 == NULL) s2 = "";
361 return strcmp(s1, s2);
362 }
363
365 static inline bool oscap_streq(const char *s1, const char *s2) {
366 return oscap_strcmp(s1, s2) == 0;
367 }
368
370 static inline bool oscap_str_startswith(const char *str, const char *prefix) {
371 return strncmp(str, prefix, strlen(prefix)) == 0;
372 }
373
375 static inline bool oscap_str_endswith(const char *str, const char *suffix) {
376 const size_t str_len = strlen(str);
377 const size_t suffix_len = strlen(suffix);
378 if (suffix_len > str_len)
379 return false;
380 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
381 }
383 char *oscap_trim(char *str);
385 char *oscap_vsprintf(const char *fmt, va_list ap);
386
387 // FIXME: This is there because of the SCE engine using this particular function
388 OSCAP_HIDDEN_END;
389
391 char *oscap_sprintf(const char *fmt, ...);
392
402 char *oscap_path_join(const char *path1, const char *path2);
403
404 OSCAP_HIDDEN_START;
405
407 const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
409 char *oscap_rtrim(char *str, char ch);
411 void oscap_strtoupper(char *str);
412
413 // check pointer equality
414 bool oscap_ptr_cmp(void *node1, void *node2);
415
427 char *oscap_expand_ipv6(const char *input);
428
429 #ifndef OSCAP_CONCAT
430 # define OSCAP_CONCAT1(a,b) a ## b
431 # define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
432 #endif
433
440 #define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
441
442 #define protect_errno \
443 for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
444
445 OSCAP_HIDDEN_END;
446
447 #endif /* OSCAP_UTIL_H_ */
oscap.h
General OpenScap functions and types.
oscap_string_map
Define mapping between symbolic constant and its string representation.
Definition: util.h:310
alloc.h
OpenScap allocation helpers.
oscap_string_map::oscap_string_to_enum
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
Convert a string to an enumeration constant.
Definition: util.c:40
oscap_string_map::oscap_enum_to_string
const char * oscap_enum_to_string(const struct oscap_string_map *map, int val)
Convert an enumeration constant to its corresponding string representation.
Definition: util.c:49