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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Lukas Kuklinek <lkuklinek@redhat.com>
*/
#ifndef OSCAP_UTIL_H_
#define OSCAP_UTIL_H_
#include <stdbool.h>
#include <assert.h>
#include "public/oscap.h"
#include "alloc.h"
#include <stdarg.h>
#ifndef __attribute__nonnull__
#define __attribute__nonnull__(x) assert((x) != NULL)
#endif
/*
* Start a list of declarations that should not be available from outside the
* library. Must be matched with OSCAP_HIDDEN_END.
*/
#ifndef OSCAP_HIDDEN_START
#define OSCAP_HIDDEN_START _Pragma("GCC visibility push(hidden)")
#endif
/*
* Start a list of declarations that should not be available from outside the
* library started by OSCAP_HIDDEN_END.
*/
#ifndef OSCAP_HIDDEN_END
#define OSCAP_HIDDEN_END _Pragma("GCC visibility pop")
#endif
OSCAP_HIDDEN_START
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 {
int value; /* integer/enum value */
const char *string; /* string representation of the value */
};
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_strdup(const char *str);
float oscap_strtol(const char *str, char **endptr, int base);
char **oscap_split(char *str, const char *delim);
int oscap_strcmp(const char *s1, const char *s2);
bool oscap_streq(const char *s1, const char *s2);
char *oscap_trim(char *str);
char *oscap_vsprintf(const char *fmt, va_list ap);
// FIXME: This is there because of the SCE engine using this particular function
OSCAP_HIDDEN_END;
char *oscap_sprintf(const char *fmt, ...);
OSCAP_HIDDEN_START;
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);
int oscap_validate_xml(const char *xmlfile, const char *schemafile, xml_reporter reporter, void *arg);
char *oscap_find_file(const char *filename, int mode, const char *pathvar, const char *path);
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__))
OSCAP_HIDDEN_END;
#endif /* OSCAP_UTIL_H_ */