Open SCAP Library
Loading...
Searching...
No Matches
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "oscap_platforms.h"
28#include <stdlib.h>
29#include <stdio.h>
30#include <stdbool.h>
31#include <assert.h>
32#include "public/oscap.h"
33#include <stdarg.h>
34#include <string.h>
35#include <pcre.h>
36#include "oscap_export.h"
37
38#ifndef __attribute__nonnull__
39#define __attribute__nonnull__(x) assert((x) != NULL)
40#endif
41
45typedef void (*oscap_destruct_func) (void *);
46
50typedef void* (*oscap_clone_func) (void *);
51
55typedef void (*oscap_consumer_func) (void *, void *);
56
67#define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
68 RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
69
78#define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
79 OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
80
89#define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
90 OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
91
100#define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
101 OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
102
111#define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
112 OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
113
114#define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
115#define OSCAP_ITERATOR(n) struct n##_iterator*
116#define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
117#define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
118#define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
119#define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
120#define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
121#define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
122#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)
123#define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
124
125#define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
126 void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
127#define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
128#define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
129
130
140#define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
141 struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
142 { return oscap_iterator_new((CONV(item))->MNAME); }
143
152#define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
153
159#define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
160
170#define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
171 RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
172 { return oscap_htable_get(item->MEXP, key); }
173
182#define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
183
192#define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
193
194#define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
195
207#define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
208 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
209 { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
210
215#define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
216 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
217 { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
218
223#define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
224 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
225 { obj->MNAME = ASSIGNER(newval); return true; }
226
231#define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
232 OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
233
239#define OSCAP_SETTER_STRING(SNAME, MNAME) \
240 OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
241
245#define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
246 OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
247
251#define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
252 OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
253 OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
254
258#define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
259 OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
260
265#define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
266 OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
267 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
268
278#define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
279 bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
280 { oscap_list_add(obj->MNAME, item); return true; }
281
282/* Generate iterator getter and list inserter */
283#define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
284 OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
285/* Generate iterator getter, list inserter, and iterator manipulation functions. */
286#define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
287 OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
288
297 const int value; /* integer/enum value */
298 const char *string; /* string representation of the value */
299};
300
305static inline char *oscap_strdup(const char *str) {
306 if (str == NULL)
307 return NULL;
308
309#ifdef _MSC_VER
310 return _strdup(str);
311#else
312 return strdup(str);
313#endif
314}
315
317static inline int oscap_strcmp(const char *s1, const char *s2) {
318 if (s1 == NULL) s1 = "";
319 if (s2 == NULL) s2 = "";
320 return strcmp(s1, s2);
321}
322
324static inline bool oscap_streq(const char *s1, const char *s2) {
325 return oscap_strcmp(s1, s2) == 0;
326}
327
329static inline bool oscap_str_startswith(const char *str, const char *prefix) {
330 return strncmp(str, prefix, strlen(prefix)) == 0;
331}
332
334static inline bool oscap_str_endswith(const char *str, const char *suffix) {
335 const size_t str_len = strlen(str);
336 const size_t suffix_len = strlen(suffix);
337 if (suffix_len > str_len)
338 return false;
339 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
340}
341
343static inline void *oscap_aligned_malloc(size_t size, size_t alignment) {
344#ifdef WIN32
345 return _aligned_malloc(size, alignment);
346#else
347 void *ptr = NULL;
348 posix_memalign(&ptr, alignment, size);
349 return ptr;
350#endif
351}
352
354static inline void oscap_aligned_free(void *memblock) {
355#ifdef WIN32
356 _aligned_free(memblock);
357#else
358 free(memblock);
359#endif
360}
361
363char *oscap_trim(char *str);
365char *oscap_vsprintf(const char *fmt, va_list ap);
366
376char *oscap_path_join(const char *path1, const char *path2);
377
379const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
381char *oscap_rtrim(char *str, char ch);
383void oscap_strtoupper(char *str);
384
385// check pointer equality
386bool oscap_ptr_cmp(void *node1, void *node2);
387
399char *oscap_expand_ipv6(const char *input);
400
401#ifndef OSCAP_CONCAT
402# define OSCAP_CONCAT1(a,b) a ## b
403# define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
404#endif
405
412#define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
413
414#define protect_errno \
415 for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
416
423int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
424
431const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
432
441char **oscap_split(char *str, const char *delim);
442
451int oscap_strcasecmp(const char *s1, const char *s2);
452
462int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
463
470char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
471
483int oscap_get_substrings(char *str, int *ofs, pcre *re, int want_substrs, char ***substrings);
484
485
486#ifndef OS_WINDOWS
495FILE *oscap_fopen_with_prefix(const char *prefix, const char *path);
496#endif
497
498#ifdef OS_WINDOWS
506char *oscap_windows_wstr_to_str(const wchar_t *wstr);
507
515wchar_t *oscap_windows_str_to_wstr(const char *str);
516
524char *oscap_windows_error_message(unsigned long error_code);
525#endif
526
537int oscap_open_writable(const char *filename);
538
539#endif /* OSCAP_UTIL_H_ */
const char * oscap_strlist_find_value(char **const kvalues, const char *key)
In a list of key-value pairs (odd indicies are keys, even values), find a value for given key.
Definition: common/elements.c:210
General OpenScap functions and types.
Define mapping between symbolic constant and its string representation.
Definition: util.h:296