]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / libudev / libudev-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
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
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "libudev.h"
28
29 #include "MurmurHash2.h"
30 #include "device-nodes.h"
31 #include "libudev-private.h"
32 #include "utf8.h"
33
34 /**
35 * SECTION:libudev-util
36 * @short_description: utils
37 *
38 * Utilities useful when dealing with devices and device node names.
39 */
40
41 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
42 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
43 char *result, size_t maxsize, int read_value)
44 {
45 char temp[UTIL_PATH_SIZE];
46 char *subsys;
47 char *sysname;
48 struct udev_device *dev;
49 char *attr;
50
51 if (string[0] != '[')
52 return -1;
53
54 strscpy(temp, sizeof(temp), string);
55
56 subsys = &temp[1];
57
58 sysname = strchr(subsys, '/');
59 if (sysname == NULL)
60 return -1;
61 sysname[0] = '\0';
62 sysname = &sysname[1];
63
64 attr = strchr(sysname, ']');
65 if (attr == NULL)
66 return -1;
67 attr[0] = '\0';
68 attr = &attr[1];
69 if (attr[0] == '/')
70 attr = &attr[1];
71 if (attr[0] == '\0')
72 attr = NULL;
73
74 if (read_value && attr == NULL)
75 return -1;
76
77 dev = udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
78 if (dev == NULL)
79 return -1;
80
81 if (read_value) {
82 const char *val;
83
84 val = udev_device_get_sysattr_value(dev, attr);
85 if (val != NULL)
86 strscpy(result, maxsize, val);
87 else
88 result[0] = '\0';
89 log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
90 } else {
91 size_t l;
92 char *s;
93
94 s = result;
95 l = strpcpyl(&s, maxsize, udev_device_get_syspath(dev), NULL);
96 if (attr != NULL)
97 strpcpyl(&s, l, "/", attr, NULL);
98 log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, attr, result);
99 }
100 udev_device_unref(dev);
101 return 0;
102 }
103
104 int util_log_priority(const char *priority)
105 {
106 char *endptr;
107 int prio;
108
109 prio = strtoul(priority, &endptr, 10);
110 if (endptr[0] == '\0' || isspace(endptr[0])) {
111 if (prio >= 0 && prio <= 7)
112 return prio;
113 else
114 return -ERANGE;
115 }
116
117 return log_level_from_string(priority);
118 }
119
120 size_t util_path_encode(const char *src, char *dest, size_t size)
121 {
122 size_t i, j;
123
124 for (i = 0, j = 0; src[i] != '\0'; i++) {
125 if (src[i] == '/') {
126 if (j+4 >= size) {
127 j = 0;
128 break;
129 }
130 memcpy(&dest[j], "\\x2f", 4);
131 j += 4;
132 } else if (src[i] == '\\') {
133 if (j+4 >= size) {
134 j = 0;
135 break;
136 }
137 memcpy(&dest[j], "\\x5c", 4);
138 j += 4;
139 } else {
140 if (j+1 >= size) {
141 j = 0;
142 break;
143 }
144 dest[j] = src[i];
145 j++;
146 }
147 }
148 dest[j] = '\0';
149 return j;
150 }
151
152 void util_remove_trailing_chars(char *path, char c)
153 {
154 size_t len;
155
156 if (path == NULL)
157 return;
158 len = strlen(path);
159 while (len > 0 && path[len-1] == c)
160 path[--len] = '\0';
161 }
162
163 int util_replace_whitespace(const char *str, char *to, size_t len)
164 {
165 size_t i, j;
166
167 /* strip trailing whitespace */
168 len = strnlen(str, len);
169 while (len && isspace(str[len-1]))
170 len--;
171
172 /* strip leading whitespace */
173 i = 0;
174 while ((i < len) && isspace(str[i]))
175 i++;
176
177 j = 0;
178 while (i < len) {
179 /* substitute multiple whitespace with a single '_' */
180 if (isspace(str[i])) {
181 while (isspace(str[i]))
182 i++;
183 to[j++] = '_';
184 }
185 to[j++] = str[i++];
186 }
187 to[j] = '\0';
188 return 0;
189 }
190
191 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
192 int util_replace_chars(char *str, const char *white)
193 {
194 size_t i = 0;
195 int replaced = 0;
196
197 while (str[i] != '\0') {
198 int len;
199
200 if (whitelisted_char_for_devnode(str[i], white)) {
201 i++;
202 continue;
203 }
204
205 /* accept hex encoding */
206 if (str[i] == '\\' && str[i+1] == 'x') {
207 i += 2;
208 continue;
209 }
210
211 /* accept valid utf8 */
212 len = utf8_encoded_valid_unichar(&str[i]);
213 if (len > 1) {
214 i += len;
215 continue;
216 }
217
218 /* if space is allowed, replace whitespace with ordinary space */
219 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
220 str[i] = ' ';
221 i++;
222 replaced++;
223 continue;
224 }
225
226 /* everything else is replaced with '_' */
227 str[i] = '_';
228 i++;
229 replaced++;
230 }
231 return replaced;
232 }
233
234 /**
235 * udev_util_encode_string:
236 * @str: input string to be encoded
237 * @str_enc: output string to store the encoded input string
238 * @len: maximum size of the output string, which may be
239 * four times as long as the input string
240 *
241 * Encode all potentially unsafe characters of a string to the
242 * corresponding 2 char hex value prefixed by '\x'.
243 *
244 * Returns: 0 if the entire string was copied, non-zero otherwise.
245 **/
246 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
247 {
248 return encode_devnode_name(str, str_enc, len);
249 }
250
251 unsigned int util_string_hash32(const char *str)
252 {
253 return MurmurHash2(str, strlen(str), 0);
254 }
255
256 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
257 uint64_t util_string_bloom64(const char *str)
258 {
259 uint64_t bits = 0;
260 unsigned int hash = util_string_hash32(str);
261
262 bits |= 1LLU << (hash & 63);
263 bits |= 1LLU << ((hash >> 6) & 63);
264 bits |= 1LLU << ((hash >> 12) & 63);
265 bits |= 1LLU << ((hash >> 18) & 63);
266 return bits;
267 }