Open SCAP Library
Loading...
Searching...
No Matches
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
45typedef void (*oscap_destruct_func) (void *);
46
50typedef void* (*oscap_clone_func) (void *);
51
55typedef 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
305static 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
322static 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
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);
339}
340
342static inline bool oscap_streq(const char *s1, const char *s2) {
343 return oscap_strcmp(s1, s2) == 0;
344}
345
347static inline bool oscap_str_startswith(const char *str, const char *prefix) {
348 return strncmp(str, prefix, strlen(prefix)) == 0;
349}
350
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)
356 return false;
357 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
358}
359
361static 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 int ret = posix_memalign(&ptr, alignment, size);
367 if (ret == 0)
368 return ptr;
369 return NULL;
370#endif
371}
372
374static inline void oscap_aligned_free(void *memblock) {
375#ifdef WIN32
376 _aligned_free(memblock);
377#else
378 free(memblock);
379#endif
380}
381
383char *oscap_trim(char *str);
385char *oscap_vsprintf(const char *fmt, va_list ap);
386
397char *oscap_generate_random_string(size_t len, char *charset);
398
408char *oscap_path_join(const char *path1, const char *path2);
409
411const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
413char *oscap_rtrim(char *str, char ch);
415void oscap_strtoupper(char *str);
416
417// check pointer equality
418bool oscap_ptr_cmp(void *node1, void *node2);
419
431char *oscap_expand_ipv6(const char *input);
432
433#ifndef OSCAP_CONCAT
434# define OSCAP_CONCAT1(a,b) a ## b
435# define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
436#endif
437
444#define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
445
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__))
448
455int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
456
463const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
464
473char **oscap_split(char *str, const char *delim);
474
483int oscap_strcasecmp(const char *s1, const char *s2);
484
494int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
495
502char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
503
504#ifndef OS_WINDOWS
513FILE *oscap_fopen_with_prefix(const char *prefix, const char *path);
514#endif
515
516#ifdef OS_WINDOWS
524char *oscap_windows_wstr_to_str(const wchar_t *wstr);
525
533wchar_t *oscap_windows_str_to_wstr(const char *str);
534
542char *oscap_windows_error_message(unsigned long error_code);
543#endif
544
555int oscap_open_writable(const char *filename);
556
563bool oscap_path_startswith(const char *path, const char *prefix);
564
572char *oscap_concat(char *str1, char *str2);
573
574#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