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 <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 "oscap_export.h"
36 #include "oscap_pcre.h"
37 
38 #ifndef __attribute__nonnull__
39 #define __attribute__nonnull__(x) assert((x) != NULL)
40 #endif
41 
45 typedef void (*oscap_destruct_func) (void *);
46 
50 typedef void* (*oscap_clone_func) (void *);
51 
55 typedef 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 
305 static 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 
322 static inline void oscap_strrm(char *str, const char *substr) {
323  if (str == NULL)
324  return;
325 
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);
331  }
332 }
333 
335 static 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);
339 }
340 
342 static inline bool oscap_streq(const char *s1, const char *s2) {
343  return oscap_strcmp(s1, s2) == 0;
344 }
345 
347 static inline bool oscap_str_startswith(const char *str, const char *prefix) {
348  return strncmp(str, prefix, strlen(prefix)) == 0;
349 }
350 
352 static 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)
356  return false;
357  return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
358 }
359 
361 static inline void *oscap_aligned_malloc(size_t size, size_t alignment) {
362 #ifdef WIN32
363  return _aligned_malloc(size, alignment);
364 #else
365  void *ptr = NULL;
366  posix_memalign(&ptr, alignment, size);
367  return ptr;
368 #endif
369 }
370 
372 static inline void oscap_aligned_free(void *memblock) {
373 #ifdef WIN32
374  _aligned_free(memblock);
375 #else
376  free(memblock);
377 #endif
378 }
379 
381 char *oscap_trim(char *str);
383 char *oscap_vsprintf(const char *fmt, va_list ap);
384 
394 char *oscap_path_join(const char *path1, const char *path2);
395 
397 const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
399 char *oscap_rtrim(char *str, char ch);
401 void oscap_strtoupper(char *str);
402 
403 // check pointer equality
404 bool oscap_ptr_cmp(void *node1, void *node2);
405 
417 char *oscap_expand_ipv6(const char *input);
418 
419 #ifndef OSCAP_CONCAT
420 # define OSCAP_CONCAT1(a,b) a ## b
421 # define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
422 #endif
423 
430 #define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
431 
432 #define protect_errno \
433  for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
434 
441 int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
442 
449 const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
450 
459 char **oscap_split(char *str, const char *delim);
460 
469 int oscap_strcasecmp(const char *s1, const char *s2);
470 
480 int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
481 
488 char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
489 
490 #ifndef OS_WINDOWS
499 FILE *oscap_fopen_with_prefix(const char *prefix, const char *path);
500 #endif
501 
502 #ifdef OS_WINDOWS
510 char *oscap_windows_wstr_to_str(const wchar_t *wstr);
511 
519 wchar_t *oscap_windows_str_to_wstr(const char *str);
520 
528 char *oscap_windows_error_message(unsigned long error_code);
529 #endif
530 
541 int oscap_open_writable(const char *filename);
542 
549 bool oscap_path_startswith(const char *path, const char *prefix);
550 
551 #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
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
Convert a string to an enumeration constant.
Definition: util.c:53
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:62