Open SCAP Library
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 <stdbool.h>
30 #include <assert.h>
31 #include "public/oscap.h"
32 #include <stdarg.h>
33 #include <string.h>
34 #include <pcre.h>
35 #include "oscap_export.h"
36 
37 #ifndef __attribute__nonnull__
38 #define __attribute__nonnull__(x) assert((x) != NULL)
39 #endif
40 
44 typedef void (*oscap_destruct_func) (void *);
45 
49 typedef void* (*oscap_clone_func) (void *);
50 
54 typedef void (*oscap_consumer_func) (void *, void *);
55 
66 #define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
67  RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
68 
77 #define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
78  OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
79 
88 #define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
89  OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
90 
99 #define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
100  OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
101 
110 #define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
111  OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
112 
113 #define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
114 #define OSCAP_ITERATOR(n) struct n##_iterator*
115 #define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
116 #define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
117 #define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
118 #define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
119 #define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
120 #define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
121 #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)
122 #define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
123 
124 #define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
125  void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
126 #define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
127 #define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
128 
129 
139 #define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
140  struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
141  { return oscap_iterator_new((CONV(item))->MNAME); }
142 
151 #define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
152 
158 #define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
159 
169 #define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
170  RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
171  { return oscap_htable_get(item->MEXP, key); }
172 
181 #define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
182 
191 #define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
192 
193 #define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
194 
206 #define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
207  OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
208  { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
209 
214 #define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
215  OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
216  { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
217 
222 #define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
223  OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
224  { obj->MNAME = ASSIGNER(newval); return true; }
225 
230 #define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
231  OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
232 
238 #define OSCAP_SETTER_STRING(SNAME, MNAME) \
239  OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
240 
244 #define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
245  OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
246 
250 #define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
251  OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
252  OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
253 
257 #define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
258  OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
259 
264 #define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
265  OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
266  OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
267 
277 #define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
278  bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
279  { oscap_list_add(obj->MNAME, item); return true; }
280 
281 /* Generate iterator getter and list inserter */
282 #define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
283  OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
284 /* Generate iterator getter, list inserter, and iterator manipulation functions. */
285 #define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
286  OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
287 
296  const int value; /* integer/enum value */
297  const char *string; /* string representation of the value */
298 };
299 
304 static inline char *oscap_strdup(const char *str) {
305  if (str == NULL)
306  return NULL;
307 
308 #ifdef _MSC_VER
309  return _strdup(str);
310 #else
311  return strdup(str);
312 #endif
313 }
314 
316 static inline int oscap_strcmp(const char *s1, const char *s2) {
317  if (s1 == NULL) s1 = "";
318  if (s2 == NULL) s2 = "";
319  return strcmp(s1, s2);
320 }
321 
323 static inline bool oscap_streq(const char *s1, const char *s2) {
324  return oscap_strcmp(s1, s2) == 0;
325 }
326 
328 static inline bool oscap_str_startswith(const char *str, const char *prefix) {
329  return strncmp(str, prefix, strlen(prefix)) == 0;
330 }
331 
333 static inline bool oscap_str_endswith(const char *str, const char *suffix) {
334  const size_t str_len = strlen(str);
335  const size_t suffix_len = strlen(suffix);
336  if (suffix_len > str_len)
337  return false;
338  return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
339 }
340 
342 static inline void *oscap_aligned_malloc(size_t size, size_t alignment) {
343 #ifdef WIN32
344  return _aligned_malloc(size, alignment);
345 #else
346  void *ptr = NULL;
347  posix_memalign(&ptr, alignment, size);
348  return ptr;
349 #endif
350 }
351 
353 static inline void oscap_aligned_free(void *memblock) {
354 #ifdef WIN32
355  _aligned_free(memblock);
356 #else
357  free(memblock);
358 #endif
359 }
360 
362 char *oscap_trim(char *str);
364 char *oscap_vsprintf(const char *fmt, va_list ap);
365 
375 char *oscap_path_join(const char *path1, const char *path2);
376 
378 const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
380 char *oscap_rtrim(char *str, char ch);
382 void oscap_strtoupper(char *str);
383 
384 // check pointer equality
385 bool oscap_ptr_cmp(void *node1, void *node2);
386 
398 char *oscap_expand_ipv6(const char *input);
399 
400 #ifndef OSCAP_CONCAT
401 # define OSCAP_CONCAT1(a,b) a ## b
402 # define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
403 #endif
404 
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 
422 int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
423 
430 const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
431 
440 char **oscap_split(char *str, const char *delim);
441 
450 int oscap_strcasecmp(const char *s1, const char *s2);
451 
461 int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
462 
469 char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
470 
482 int oscap_get_substrings(char *str, int *ofs, pcre *re, int want_substrs, char ***substrings);
483 
484 #ifdef OS_WINDOWS
485 
492 char *oscap_windows_wstr_to_str(const wchar_t *wstr);
493 
501 wchar_t *oscap_windows_str_to_wstr(const char *str);
502 
510 char *oscap_windows_error_message(unsigned long error_code);
511 #endif
512 
513 #endif /* OSCAP_UTIL_H_ */
General OpenScap functions and types.
Define mapping between symbolic constant and its string representation.
Definition: util.h:295
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
Convert a string to an enumeration constant.
Definition: util.c:52
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:61