]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/device-nodes.c
update TODO
[thirdparty/systemd.git] / src / shared / device-nodes.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "device-nodes.h"
8 #include "string-util.h"
9 #include "utf8.h"
10
11 int allow_listed_char_for_devnode(char c, const char *white) {
12 return
13 ascii_isdigit(c) ||
14 ascii_isalpha(c) ||
15 strchr("#+-.:=@_", c) ||
16 (white && strchr(white, c));
17 }
18
19 int encode_devnode_name(const char *str, char *str_enc, size_t len) {
20 size_t i, j;
21
22 if (!str || !str_enc)
23 return -EINVAL;
24
25 for (i = 0, j = 0; str[i] != '\0'; i++) {
26 int seqlen;
27
28 seqlen = utf8_encoded_valid_unichar(str + i, SIZE_MAX);
29 if (seqlen > 1) {
30
31 if (len-j < (size_t) seqlen)
32 return -EINVAL;
33
34 memcpy(&str_enc[j], &str[i], seqlen);
35 j += seqlen;
36 i += (seqlen-1);
37
38 } else if (str[i] == '\\' || !allow_listed_char_for_devnode(str[i], NULL)) {
39
40 if (len-j < 4)
41 return -EINVAL;
42
43 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
44 j += 4;
45
46 } else {
47 if (len-j < 1)
48 return -EINVAL;
49
50 str_enc[j] = str[i];
51 j++;
52 }
53 }
54
55 if (len-j < 1)
56 return -EINVAL;
57
58 str_enc[j] = '\0';
59 return 0;
60 }