| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "nulstr-util.h" |
| 5 |
#include "set.h" |
| 6 |
#include "string-util.h" |
| 7 |
#include "strv.h" |
| 8 |
|
| 9 |
const char* nulstr_get(const char *nulstr, const char *needle) { |
| 10 |
if (!nulstr) |
| 11 |
return NULL; |
| 12 |
|
| 13 |
NULSTR_FOREACH(i, nulstr) |
| 14 |
if (streq(i, needle)) |
| 15 |
return i; |
| 16 |
|
| 17 |
return NULL; |
| 18 |
} |
| 19 |
|
| 20 |
char** strv_parse_nulstr_full(const char *s, size_t l, bool drop_trailing_nuls) { |
| 21 |
_cleanup_strv_free_ char **v = NULL; |
| 22 |
size_t c = 0, i = 0; |
| 23 |
|
| 24 |
/* l is the length of the input data, which will be split at NULs into elements of the resulting |
| 25 |
* strv. Hence, the number of items in the resulting strv will be equal to one plus the number of NUL |
| 26 |
* bytes in the l bytes starting at s, unless s[l-1] is NUL, in which case the final empty string is |
| 27 |
* not stored in the resulting strv, and length is equal to the number of NUL bytes. |
| 28 |
* |
| 29 |
* Note that contrary to a normal nulstr which cannot contain empty strings, because the input data |
| 30 |
* is terminated by any two consequent NUL bytes, this parser accepts empty strings in s. */ |
| 31 |
|
| 32 |
assert(s || l <= 0); |
| 33 |
|
| 34 |
if (drop_trailing_nuls) |
| 35 |
while (l > 0 && s[l-1] == '\0') |
| 36 |
l--; |
| 37 |
|
| 38 |
if (l <= 0) |
| 39 |
return new0(char*, 1); |
| 40 |
|
| 41 |
for (const char *p = s; p < s + l; p++) |
| 42 |
if (*p == 0) |
| 43 |
c++; |
| 44 |
|
| 45 |
if (s[l-1] != 0) |
| 46 |
c++; |
| 47 |
|
| 48 |
v = new0(char*, c+1); |
| 49 |
if (!v) |
| 50 |
return NULL; |
| 51 |
|
| 52 |
for (const char *p = s; p < s + l;) { |
| 53 |
const char *e; |
| 54 |
|
| 55 |
e = memchr(p, 0, s + l - p); |
| 56 |
|
| 57 |
v[i] = memdup_suffix0(p, e ? e - p : s + l - p); |
| 58 |
if (!v[i]) |
| 59 |
return NULL; |
| 60 |
i++; |
| 61 |
|
| 62 |
if (!e) |
| 63 |
break; |
| 64 |
|
| 65 |
p = e + 1; |
| 66 |
} |
| 67 |
|
| 68 |
assert(i == c); |
| 69 |
|
| 70 |
return TAKE_PTR(v); |
| 71 |
} |
| 72 |
|
| 73 |
char** strv_split_nulstr(const char *s) { |
| 74 |
_cleanup_strv_free_ char **l = NULL; |
| 75 |
|
| 76 |
/* This parses a nulstr, without specification of size, and stops at an empty string. This cannot |
| 77 |
* parse nulstrs with embedded empty strings hence, as an empty string is an end marker. Use |
| 78 |
* strv_parse_nulstr() above to parse a nulstr with embedded empty strings (which however requires a |
| 79 |
* size to be specified) */ |
| 80 |
|
| 81 |
NULSTR_FOREACH(i, s) |
| 82 |
if (strv_extend(&l, i) < 0) |
| 83 |
return NULL; |
| 84 |
|
| 85 |
return l ? TAKE_PTR(l) : strv_new(NULL); |
| 86 |
} |
| 87 |
|
| 88 |
int strv_make_nulstr(char * const *l, char **ret, size_t *ret_size) { |
| 89 |
_cleanup_free_ char *m = NULL; |
| 90 |
size_t n = 0; |
| 91 |
|
| 92 |
/* Builds a nulstr and returns it together with the size. An extra NUL byte will be appended (⚠️ but |
| 93 |
* not included in the size! ⚠️). This is done so that the nulstr can be used both in |
| 94 |
* strv_parse_nulstr() and in NULSTR_FOREACH()/strv_split_nulstr() contexts, i.e. with and without a |
| 95 |
* size parameter. In the former case we can include empty strings, in the latter case we cannot (as |
| 96 |
* that is the end marker). |
| 97 |
* |
| 98 |
* When NULSTR_FOREACH()/strv_split_nulstr() is used it is often assumed that the nulstr ends in two |
| 99 |
* NUL bytes (which it will, if not empty). To ensure that this assumption *always* holds, we'll |
| 100 |
* return a buffer with two NUL bytes in that case, but return a size of zero. */ |
| 101 |
|
| 102 |
assert(ret); |
| 103 |
|
| 104 |
STRV_FOREACH(i, l) { |
| 105 |
size_t z; |
| 106 |
|
| 107 |
z = strlen(*i) + 1; |
| 108 |
|
| 109 |
if (!GREEDY_REALLOC(m, n + z + 1)) /* One extra NUL at the end as marker */ |
| 110 |
return -ENOMEM; |
| 111 |
|
| 112 |
memcpy(m + n, *i, z); |
| 113 |
n += z; |
| 114 |
} |
| 115 |
|
| 116 |
if (!m) { |
| 117 |
/* return a buffer with an extra NUL, so that the assumption that we always have two trailing NULs holds */ |
| 118 |
m = new0(char, 2); |
| 119 |
if (!m) |
| 120 |
return -ENOMEM; |
| 121 |
|
| 122 |
n = 0; |
| 123 |
} else |
| 124 |
/* Extra NUL is not counted in size returned */ |
| 125 |
m[n] = '\0'; |
| 126 |
|
| 127 |
*ret = TAKE_PTR(m); |
| 128 |
if (ret_size) |
| 129 |
*ret_size = n; |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
int set_make_nulstr(Set *s, char **ret, size_t *ret_size) { |
| 135 |
/* Use _cleanup_free_ instead of _cleanup_strv_free_ because we need to clean the strv only, not |
| 136 |
* the strings owned by the set. */ |
| 137 |
_cleanup_free_ char **strv = NULL; |
| 138 |
|
| 139 |
assert(ret); |
| 140 |
|
| 141 |
strv = set_get_strv(s); |
| 142 |
if (!strv) |
| 143 |
return -ENOMEM; |
| 144 |
|
| 145 |
return strv_make_nulstr(strv, ret, ret_size); |
| 146 |
} |