Open SCAP Library: util.h Source File

Open SCAP Library

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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
33 #ifndef __attribute__nonnull__
34 #define __attribute__nonnull__(x) assert((x) != NULL)
35 #endif
36
37 /*
38 * Start a list of declarations that should not be available from outside the
39 * library. Must be matched with OSCAP_HIDDEN_END.
40 */
41 #ifndef OSCAP_HIDDEN_START
42 #define OSCAP_HIDDEN_START _Pragma("GCC visibility push(hidden)")
43 #endif
44
45 /*
46 * Start a list of declarations that should not be available from outside the
47 * library started by OSCAP_HIDDEN_END.
48 */
49 #ifndef OSCAP_HIDDEN_END
50 #define OSCAP_HIDDEN_END _Pragma("GCC visibility pop")
51 #endif
52
53 OSCAP_HIDDEN_START
54
58 typedef void (*oscap_destruct_func) (void *);
59
63 typedef void* (*oscap_clone_func) (void *);
64
68 typedef void (*oscap_consumer_func) (void *, void *);
69
80 #define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
81 RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
82
91 #define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
92 OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
93
102 #define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
103 OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
104
113 #define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
114 OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
115
124 #define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
125 OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
126
127 #define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
128 #define OSCAP_ITERATOR(n) struct n##_iterator*
129 #define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
130 #define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
131 #define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
132 #define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
133 #define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
134 #define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
135 #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)
136 #define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
137
138 #define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
139 void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
140 #define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
141 #define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
142
143
153 #define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
154 struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
155 { return oscap_iterator_new((CONV(item))->MNAME); }
156
165 #define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
166
172 #define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
173
183 #define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
184 RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
185 { return oscap_htable_get(item->MEXP, key); }
186
195 #define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
196
205 #define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
206
207 #define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
208
220 #define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
221 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
222 { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
223
228 #define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
229 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
230 { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
231
236 #define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
237 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
238 { obj->MNAME = ASSIGNER(newval); return true; }
239
244 #define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
245 OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
246
252 #define OSCAP_SETTER_STRING(SNAME, MNAME) \
253 OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
254
258 #define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
259 OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
260
264 #define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
265 OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
266 OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
267
271 #define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
272 OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
273
278 #define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
279 OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
280 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
281
291 #define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
292 bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
293 { oscap_list_add(obj->MNAME, item); return true; }
294
295 /* Generate iterator getter and list inserter */
296 #define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
297 OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
298 /* Generate iterator getter, list inserter, and iterator manipulation functions. */
299 #define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
300 OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
301
309 struct oscap_string_map {
310 int value; /* integer/enum value */
311 const char *string; /* string representation of the value */
312 };
313
320 int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
321
328 const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
329
334 char *oscap_strdup(const char *str);
335
342 float oscap_strtol(const char *str, char **endptr, int base);
343
352 char **oscap_split(char *str, const char *delim);
353
354
356 int oscap_strcmp(const char *s1, const char *s2);
358 bool oscap_streq(const char *s1, const char *s2);
359 bool oscap_str_startswith(const char *str, const char *with);
360 bool oscap_str_endswith(const char *str, const char *with);
362 char *oscap_trim(char *str);
364 char *oscap_vsprintf(const char *fmt, va_list ap);
365
366 // FIXME: This is there because of the SCE engine using this particular function
367 OSCAP_HIDDEN_END;
368
370 char *oscap_sprintf(const char *fmt, ...);
371
372 OSCAP_HIDDEN_START;
373
375 const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
377 char *oscap_rtrim(char *str, char ch);
379 void oscap_strtoupper(char *str);
380
381 // check pointer equality
382 bool oscap_ptr_cmp(void *node1, void *node2);
383
391 char *oscap_find_file(const char *filename, int mode, const char *pathvar, const char *path);
392
404 char *oscap_expand_ipv6(const char *input);
405
406 #ifndef OSCAP_CONCAT
407 # define OSCAP_CONCAT1(a,b) a ## b
408 # define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
409 #endif
410
411 #define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
412
413 #define protect_errno \
414 for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
415
416 OSCAP_HIDDEN_END;
417
418 #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:309
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:39
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:48