]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "MurmurHash2.h"
13 #include "device-nodes.h"
14 #include "libudev-private.h"
15 #include "syslog-util.h"
19 * SECTION:libudev-util
20 * @short_description: utils
22 * Utilities useful when dealing with devices and device node names.
25 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
26 int util_resolve_subsys_kernel(struct udev
*udev
, const char *string
,
27 char *result
, size_t maxsize
, int read_value
)
29 char temp
[UTIL_PATH_SIZE
];
32 struct udev_device
*dev
;
38 strscpy(temp
, sizeof(temp
), string
);
42 sysname
= strchr(subsys
, '/');
46 sysname
= &sysname
[1];
48 attr
= strchr(sysname
, ']');
58 if (read_value
&& attr
== NULL
)
61 dev
= udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
68 val
= udev_device_get_sysattr_value(dev
, attr
);
70 strscpy(result
, maxsize
, val
);
73 log_debug("value '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
79 l
= strpcpyl(&s
, maxsize
, udev_device_get_syspath(dev
), NULL
);
81 strpcpyl(&s
, l
, "/", attr
, NULL
);
82 log_debug("path '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
84 udev_device_unref(dev
);
88 int util_log_priority(const char *priority
)
93 prio
= strtoul(priority
, &endptr
, 10);
94 if (endptr
[0] == '\0' || isspace(endptr
[0])) {
95 if (prio
>= 0 && prio
<= 7)
101 return log_level_from_string(priority
);
104 size_t util_path_encode(const char *src
, char *dest
, size_t size
)
108 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
114 memcpy(&dest
[j
], "\\x2f", 4);
116 } else if (src
[i
] == '\\') {
121 memcpy(&dest
[j
], "\\x5c", 4);
137 * Copy from 'str' to 'to', while removing all leading and trailing whitespace,
138 * and replacing each run of consecutive whitespace with a single underscore.
139 * The chars from 'str' are copied up to the \0 at the end of the string, or
140 * at most 'len' chars. This appends \0 to 'to', at the end of the copied
143 * If 'len' chars are copied into 'to', the final \0 is placed at len+1
144 * (i.e. 'to[len] = \0'), so the 'to' buffer must have at least len+1
147 * Note this may be called with 'str' == 'to', i.e. to replace whitespace
148 * in-place in a buffer. This function can handle that situation.
150 int util_replace_whitespace(const char *str
, char *to
, size_t len
)
154 /* strip trailing whitespace */
155 len
= strnlen(str
, len
);
156 while (len
&& isspace(str
[len
-1]))
159 /* strip leading whitespace */
161 while ((i
< len
) && isspace(str
[i
]))
166 /* substitute multiple whitespace with a single '_' */
167 if (isspace(str
[i
])) {
168 while (isspace(str
[i
]))
178 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
179 int util_replace_chars(char *str
, const char *white
)
184 while (str
[i
] != '\0') {
187 if (whitelisted_char_for_devnode(str
[i
], white
)) {
192 /* accept hex encoding */
193 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
198 /* accept valid utf8 */
199 len
= utf8_encoded_valid_unichar(&str
[i
]);
205 /* if space is allowed, replace whitespace with ordinary space */
206 if (isspace(str
[i
]) && white
!= NULL
&& strchr(white
, ' ') != NULL
) {
213 /* everything else is replaced with '_' */
222 * udev_util_encode_string:
223 * @str: input string to be encoded
224 * @str_enc: output string to store the encoded input string
225 * @len: maximum size of the output string, which may be
226 * four times as long as the input string
228 * Encode all potentially unsafe characters of a string to the
229 * corresponding 2 char hex value prefixed by '\x'.
231 * Returns: 0 if the entire string was copied, non-zero otherwise.
233 _public_
int udev_util_encode_string(const char *str
, char *str_enc
, size_t len
)
235 return encode_devnode_name(str
, str_enc
, len
);
238 unsigned int util_string_hash32(const char *str
)
240 return MurmurHash2(str
, strlen(str
), 0);
243 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
244 uint64_t util_string_bloom64(const char *str
)
247 unsigned int hash
= util_string_hash32(str
);
249 bits
|= 1LLU << (hash
& 63);
250 bits
|= 1LLU << ((hash
>> 6) & 63);
251 bits
|= 1LLU << ((hash
>> 12) & 63);
252 bits
|= 1LLU << ((hash
>> 18) & 63);