Loading...
Searching...
No Matches
util.h
/*
* Copyright 2009 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Lukas Kuklinek <lkuklinek@redhat.com>
*/
#ifndef OSCAP_UTIL_H_
#define OSCAP_UTIL_H_
#include "oscap_platforms.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "public/oscap.h"
#include <stdarg.h>
#include <string.h>
#include "oscap_export.h"
#include "oscap_pcre.h"
#ifndef __attribute__nonnull__
#define __attribute__nonnull__(x) assert((x) != NULL)
#endif
typedef void (*oscap_destruct_func) (void *);
typedef void* (*oscap_clone_func) (void *);
typedef void (*oscap_consumer_func) (void *, void *);
#define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
#define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
#define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
#define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
#define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
#define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
#define OSCAP_ITERATOR(n) struct n##_iterator*
#define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
#define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
#define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
#define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
#define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
#define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
#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)
#define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
#define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
#define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
#define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
#define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
{ return oscap_iterator_new((CONV(item))->MNAME); }
#define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
#define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
#define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
{ return oscap_htable_get(item->MEXP, key); }
#define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
#define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
#define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
#define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
{ if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
#define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
{ DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
#define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
{ obj->MNAME = ASSIGNER(newval); return true; }
#define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
#define OSCAP_SETTER_STRING(SNAME, MNAME) \
OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
#define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
#define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
#define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
#define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
#define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
{ oscap_list_add(obj->MNAME, item); return true; }
/* Generate iterator getter and list inserter */
#define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
/* Generate iterator getter, list inserter, and iterator manipulation functions. */
#define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
struct oscap_string_map {
const int value; /* integer/enum value */
const char *string; /* string representation of the value */
};
static inline char *oscap_strdup(const char *str) {
if (str == NULL)
return NULL;
#ifdef _MSC_VER
return _strdup(str);
#else
return strdup(str);
#endif
}
static inline void oscap_strrm(char *str, const char *substr) {
if (str == NULL)
return;
size_t sublen = strlen(substr);
char *ptr = strstr(str, substr);
while (ptr != NULL) {
memmove(ptr, ptr+sublen, strlen(ptr) - sublen + 1);
ptr = strstr(str, substr);
}
}
static inline int oscap_strcmp(const char *s1, const char *s2) {
if (s1 == NULL) s1 = "";
if (s2 == NULL) s2 = "";
return strcmp(s1, s2);
}
static inline bool oscap_streq(const char *s1, const char *s2) {
return oscap_strcmp(s1, s2) == 0;
}
static inline bool oscap_str_startswith(const char *str, const char *prefix) {
return strncmp(str, prefix, strlen(prefix)) == 0;
}
static inline bool oscap_str_endswith(const char *str, const char *suffix) {
const size_t str_len = strlen(str);
const size_t suffix_len = strlen(suffix);
if (suffix_len > str_len)
return false;
return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
}
static inline void *oscap_aligned_malloc(size_t size, size_t alignment) {
#ifdef WIN32
return _aligned_malloc(size, alignment);
#else
void *ptr = NULL;
int ret = posix_memalign(&ptr, alignment, size);
if (ret == 0)
return ptr;
return NULL;
#endif
}
static inline void oscap_aligned_free(void *memblock) {
#ifdef WIN32
_aligned_free(memblock);
#else
free(memblock);
#endif
}
char *oscap_trim(char *str);
char *oscap_vsprintf(const char *fmt, va_list ap);
char *oscap_generate_random_string(size_t len, char *charset);
char *oscap_path_join(const char *path1, const char *path2);
const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
char *oscap_rtrim(char *str, char ch);
void oscap_strtoupper(char *str);
// check pointer equality
bool oscap_ptr_cmp(void *node1, void *node2);
char *oscap_expand_ipv6(const char *input);
#ifndef OSCAP_CONCAT
# define OSCAP_CONCAT1(a,b) a ## b
# define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
#endif
#define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
#define protect_errno \
for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
char **oscap_split(char *str, const char *delim);
int oscap_strcasecmp(const char *s1, const char *s2);
int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
#ifndef OS_WINDOWS
FILE *oscap_fopen_with_prefix(const char *prefix, const char *path);
#endif
#ifdef OS_WINDOWS
char *oscap_windows_wstr_to_str(const wchar_t *wstr);
wchar_t *oscap_windows_str_to_wstr(const char *str);
char *oscap_windows_error_message(unsigned long error_code);
#endif
int oscap_open_writable(const char *filename);
bool oscap_path_startswith(const char *path, const char *prefix);
char *oscap_concat(char *str1, char *str2);
char *oscap_remove_excess_whitespace(const char *str);
#endif /* OSCAP_UTIL_H_ */
oscap_strlist_find_value
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
oscap.h
General OpenScap functions and types.
oscap_string_map
Define mapping between symbolic constant and its string representation.
Definition util.h:296