27#include "oscap_platforms.h"
35#include "oscap_export.h"
36#include "oscap_pcre.h"
38#ifndef __attribute__nonnull__
39#define __attribute__nonnull__(x) assert((x) != NULL)
45typedef void (*oscap_destruct_func) (
void *);
50typedef void* (*oscap_clone_func) (
void *);
55typedef void (*oscap_consumer_func) (
void *,
void *);
67#define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
68 RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
78#define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
79 OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
89#define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
90 OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
100#define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
101 OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
111#define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
112 OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
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)
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)
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); }
152#define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
159#define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
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); }
182#define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
192#define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
194#define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
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; }
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; }
223#define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
224 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
225 { obj->MNAME = ASSIGNER(newval); return true; }
231#define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
232 OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
239#define OSCAP_SETTER_STRING(SNAME, MNAME) \
240 OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
245#define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
246 OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
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, )
258#define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
259 OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
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; }
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; }
283#define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
284 OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
286#define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
287 OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
305static inline char *oscap_strdup(
const char *str) {
322static inline void oscap_strrm(
char *str,
const char *substr) {
326 size_t sublen = strlen(substr);
327 char *ptr = strstr(str, substr);
328 while (ptr != NULL) {
329 memmove(ptr, ptr+sublen, strlen(ptr) - sublen + 1);
330 ptr = strstr(str, substr);
335static inline int oscap_strcmp(
const char *s1,
const char *s2) {
336 if (s1 == NULL) s1 =
"";
337 if (s2 == NULL) s2 =
"";
338 return strcmp(s1, s2);
342static inline bool oscap_streq(
const char *s1,
const char *s2) {
343 return oscap_strcmp(s1, s2) == 0;
347static inline bool oscap_str_startswith(
const char *str,
const char *prefix) {
348 return strncmp(str, prefix, strlen(prefix)) == 0;
352static inline bool oscap_str_endswith(
const char *str,
const char *suffix) {
353 const size_t str_len = strlen(str);
354 const size_t suffix_len = strlen(suffix);
355 if (suffix_len > str_len)
357 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
361static inline void *oscap_aligned_malloc(
size_t size,
size_t alignment) {
363 return _aligned_malloc(size, alignment);
366 int ret = posix_memalign(&ptr, alignment, size);
374static inline void oscap_aligned_free(
void *memblock) {
376 _aligned_free(memblock);
383char *oscap_trim(
char *str);
385char *oscap_vsprintf(
const char *fmt, va_list ap);
397char *oscap_generate_random_string(
size_t len,
char *charset);
408char *oscap_path_join(
const char *path1,
const char *path2);
413char *oscap_rtrim(
char *str,
char ch);
415void oscap_strtoupper(
char *str);
418bool oscap_ptr_cmp(
void *node1,
void *node2);
431char *oscap_expand_ipv6(
const char *input);
434# define OSCAP_CONCAT1(a,b) a ## b
435# define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
444#define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
446#define protect_errno \
447 for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
455int oscap_string_to_enum(
const struct oscap_string_map *map,
const char *str);
463const char *oscap_enum_to_string(
const struct oscap_string_map *map,
int val);
473char **oscap_split(
char *str,
const char *delim);
483int oscap_strcasecmp(
const char *s1,
const char *s2);
494int oscap_strncasecmp(
const char *s1,
const char *s2,
size_t n);
502char *oscap_strerror_r(
int errnum,
char *buf,
size_t buflen);
513FILE *oscap_fopen_with_prefix(
const char *prefix,
const char *path);
524char *oscap_windows_wstr_to_str(
const wchar_t *wstr);
533wchar_t *oscap_windows_str_to_wstr(
const char *str);
542char *oscap_windows_error_message(
unsigned long error_code);
555int oscap_open_writable(
const char *filename);
563bool oscap_path_startswith(
const char *path,
const char *prefix);
572char *oscap_concat(
char *str1,
char *str2);
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