]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/device-nodes.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / basic / device-nodes.c
CommitLineData
8f6ce71f
DR
1/***
2 This file is part of systemd.
3
036ae95a 4 Copyright 2008-2011 Kay Sievers
8f6ce71f
DR
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a366 20#include <errno.h>
8f6ce71f 21#include <stdio.h>
11c3a366 22#include <string.h>
8f6ce71f
DR
23
24#include "device-nodes.h"
25#include "utf8.h"
26
27int whitelisted_char_for_devnode(char c, const char *white) {
f0bc5047 28
8f6ce71f
DR
29 if ((c >= '0' && c <= '9') ||
30 (c >= 'A' && c <= 'Z') ||
31 (c >= 'a' && c <= 'z') ||
32 strchr("#+-.:=@_", c) != NULL ||
33 (white != NULL && strchr(white, c) != NULL))
34 return 1;
f0bc5047 35
8f6ce71f
DR
36 return 0;
37}
38
39int encode_devnode_name(const char *str, char *str_enc, size_t len) {
40 size_t i, j;
41
42 if (str == NULL || str_enc == NULL)
7e8185ef 43 return -EINVAL;
8f6ce71f
DR
44
45 for (i = 0, j = 0; str[i] != '\0'; i++) {
46 int seqlen;
47
48 seqlen = utf8_encoded_valid_unichar(&str[i]);
49 if (seqlen > 1) {
f0bc5047 50
8f6ce71f 51 if (len-j < (size_t)seqlen)
f0bc5047
LP
52 return -EINVAL;
53
8f6ce71f
DR
54 memcpy(&str_enc[j], &str[i], seqlen);
55 j += seqlen;
56 i += (seqlen-1);
f0bc5047 57
8f6ce71f 58 } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
f0bc5047 59
8f6ce71f 60 if (len-j < 4)
f0bc5047
LP
61 return -EINVAL;
62
8f6ce71f
DR
63 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
64 j += 4;
f0bc5047 65
8f6ce71f
DR
66 } else {
67 if (len-j < 1)
f0bc5047
LP
68 return -EINVAL;
69
8f6ce71f
DR
70 str_enc[j] = str[i];
71 j++;
72 }
73 }
f0bc5047 74
8f6ce71f 75 if (len-j < 1)
f0bc5047
LP
76 return -EINVAL;
77
8f6ce71f
DR
78 str_enc[j] = '\0';
79 return 0;
8f6ce71f 80}