chiark / gitweb /
libudev: add comments to libudev.h
[elogind.git] / udev / lib / libudev-util.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <ctype.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22
23 #include "libudev.h"
24 #include "libudev-private.h"
25
26 static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
27 {
28         char path[UTIL_PATH_SIZE];
29         ssize_t len;
30         const char *pos;
31
32         util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
33         len = readlink(path, path, sizeof(path));
34         if (len < 0 || len >= (ssize_t) sizeof(path))
35                 return -1;
36         path[len] = '\0';
37         pos = strrchr(path, '/');
38         if (pos == NULL)
39                 return -1;
40         pos = &pos[1];
41         dbg(udev, "resolved link to: '%s'\n", pos);
42         return util_strscpy(value, size, pos);
43 }
44
45 ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size)
46 {
47         return get_sys_link(udev, "subsystem", syspath, subsystem, size);
48 }
49
50 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size)
51 {
52         return get_sys_link(udev, "driver", syspath, driver, size);
53 }
54
55 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
56 {
57         char link_target[UTIL_PATH_SIZE];
58
59         int len;
60         int i;
61         int back;
62         char *base;
63
64         len = readlink(syspath, link_target, sizeof(link_target));
65         if (len <= 0)
66                 return -1;
67         link_target[len] = '\0';
68         dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target);
69
70         for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
71                 ;
72         dbg(udev, "base '%s', tail '%s', back %i\n", syspath, &link_target[back * 3], back);
73         for (i = 0; i <= back; i++) {
74                 base = strrchr(syspath, '/');
75                 if (base == NULL)
76                         return -1;
77                 base[0] = '\0';
78         }
79         dbg(udev, "after moving back '%s'\n", syspath);
80         util_strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
81         return 0;
82 }
83
84 int util_log_priority(const char *priority)
85 {
86         char *endptr;
87         int prio;
88
89         prio = strtol(priority, &endptr, 10);
90         if (endptr[0] == '\0')
91                 return prio;
92         if (strncmp(priority, "err", 3) == 0)
93                 return LOG_ERR;
94         if (strcmp(priority, "info") == 0)
95                 return LOG_INFO;
96         if (strcmp(priority, "debug") == 0)
97                 return LOG_DEBUG;
98         return 0;
99 }
100
101 size_t util_path_encode(const char *src, char *dest, size_t size)
102 {
103         size_t i, j;
104
105         for (i = 0, j = 0; src[i] != '\0'; i++) {
106                 if (src[i] == '/') {
107                         if (j+4 >= size) {
108                                 j = 0;
109                                 break;
110                         }
111                         memcpy(&dest[j], "\\x2f", 4);
112                         j += 4;
113                 } else if (src[i] == '\\') {
114                         if (j+4 >= size) {
115                                 j = 0;
116                                 break;
117                         }
118                         memcpy(&dest[j], "\\x5c", 4);
119                         j += 4;
120                 } else {
121                         if (j+1 >= size) {
122                                 j = 0;
123                                 break;
124                         }
125                         dest[j] = src[i];
126                         j++;
127                 }
128         }
129         dest[j] = '\0';
130         return j;
131 }
132
133 size_t util_path_decode(char *s)
134 {
135         size_t i, j;
136
137         for (i = 0, j = 0; s[i] != '\0'; j++) {
138                 if (memcmp(&s[i], "\\x2f", 4) == 0) {
139                         s[j] = '/';
140                         i += 4;
141                 } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
142                         s[j] = '\\';
143                         i += 4;
144                 } else {
145                         s[j] = s[i];
146                         i++;
147                 }
148         }
149         s[j] = '\0';
150         return j;
151 }
152
153 void util_remove_trailing_chars(char *path, char c)
154 {
155         size_t len;
156
157         if (path == NULL)
158                 return;
159         len = strlen(path);
160         while (len > 0 && path[len-1] == c)
161                 path[--len] = '\0';
162 }
163
164 /*
165  * Concatenates strings. In any case, terminates in _all_ cases with '\0'
166  * and moves the @dest pointer forward to the added '\0'. Returns the
167  * remaining size, and 0 if the string was truncated.
168  */
169 size_t util_strpcpy(char **dest, size_t size, const char *src)
170 {
171         size_t len;
172
173         len = strlen(src);
174         if (len >= size) {
175                 if (size > 1)
176                         *dest = mempcpy(*dest, src, size-1);
177                 size = 0;
178                 *dest[0] = '\0';
179         } else {
180                 if (len > 0) {
181                         *dest = mempcpy(*dest, src, len);
182                         size -= len;
183                 }
184                 *dest[0] = '\0';
185         }
186         return size;
187 }
188
189 /* concatenates list of strings, moves dest forward */
190 size_t util_strpcpyl(char **dest, size_t size, const char *src, ...)
191 {
192         va_list va;
193
194         va_start(va, src);
195         do {
196                 size = util_strpcpy(dest, size, src);
197                 src = va_arg(va, char *);
198         } while (src != NULL);
199         va_end(va);
200
201         return size;
202 }
203
204 /* copies string */
205 size_t util_strscpy(char *dest, size_t size, const char *src)
206 {
207         char *s;
208
209         s = dest;
210         return util_strpcpy(&s, size, src);
211 }
212
213 /* concatenates list of strings */
214 size_t util_strscpyl(char *dest, size_t size, const char *src, ...)
215 {
216         va_list va;
217         char *s;
218
219         va_start(va, src);
220         s = dest;
221         do {
222                 size = util_strpcpy(&s, size, src);
223                 src = va_arg(va, char *);
224         } while (src != NULL);
225         va_end(va);
226
227         return size;
228 }
229
230 /* count of characters used to encode one unicode char */
231 static int utf8_encoded_expected_len(const char *str)
232 {
233         unsigned char c = (unsigned char)str[0];
234
235         if (c < 0x80)
236                 return 1;
237         if ((c & 0xe0) == 0xc0)
238                 return 2;
239         if ((c & 0xf0) == 0xe0)
240                 return 3;
241         if ((c & 0xf8) == 0xf0)
242                 return 4;
243         if ((c & 0xfc) == 0xf8)
244                 return 5;
245         if ((c & 0xfe) == 0xfc)
246                 return 6;
247         return 0;
248 }
249
250 /* decode one unicode char */
251 static int utf8_encoded_to_unichar(const char *str)
252 {
253         int unichar;
254         int len;
255         int i;
256
257         len = utf8_encoded_expected_len(str);
258         switch (len) {
259         case 1:
260                 return (int)str[0];
261         case 2:
262                 unichar = str[0] & 0x1f;
263                 break;
264         case 3:
265                 unichar = (int)str[0] & 0x0f;
266                 break;
267         case 4:
268                 unichar = (int)str[0] & 0x07;
269                 break;
270         case 5:
271                 unichar = (int)str[0] & 0x03;
272                 break;
273         case 6:
274                 unichar = (int)str[0] & 0x01;
275                 break;
276         default:
277                 return -1;
278         }
279
280         for (i = 1; i < len; i++) {
281                 if (((int)str[i] & 0xc0) != 0x80)
282                         return -1;
283                 unichar <<= 6;
284                 unichar |= (int)str[i] & 0x3f;
285         }
286
287         return unichar;
288 }
289
290 /* expected size used to encode one unicode char */
291 static int utf8_unichar_to_encoded_len(int unichar)
292 {
293         if (unichar < 0x80)
294                 return 1;
295         if (unichar < 0x800)
296                 return 2;
297         if (unichar < 0x10000)
298                 return 3;
299         if (unichar < 0x200000)
300                 return 4;
301         if (unichar < 0x4000000)
302                 return 5;
303         return 6;
304 }
305
306 /* check if unicode char has a valid numeric range */
307 static int utf8_unichar_valid_range(int unichar)
308 {
309         if (unichar > 0x10ffff)
310                 return 0;
311         if ((unichar & 0xfffff800) == 0xd800)
312                 return 0;
313         if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
314                 return 0;
315         if ((unichar & 0xffff) == 0xffff)
316                 return 0;
317         return 1;
318 }
319
320 /* validate one encoded unicode char and return its length */
321 static int utf8_encoded_valid_unichar(const char *str)
322 {
323         int len;
324         int unichar;
325         int i;
326
327         len = utf8_encoded_expected_len(str);
328         if (len == 0)
329                 return -1;
330
331         /* ascii is valid */
332         if (len == 1)
333                 return 1;
334
335         /* check if expected encoded chars are available */
336         for (i = 0; i < len; i++)
337                 if ((str[i] & 0x80) != 0x80)
338                         return -1;
339
340         unichar = utf8_encoded_to_unichar(str);
341
342         /* check if encoded length matches encoded value */
343         if (utf8_unichar_to_encoded_len(unichar) != len)
344                 return -1;
345
346         /* check if value has valid range */
347         if (!utf8_unichar_valid_range(unichar))
348                 return -1;
349
350         return len;
351 }
352
353 int udev_util_replace_whitespace(const char *str, char *to, size_t len)
354 {
355         size_t i, j;
356
357         /* strip trailing whitespace */
358         len = strnlen(str, len);
359         while (len && isspace(str[len-1]))
360                 len--;
361
362         /* strip leading whitespace */
363         i = 0;
364         while (isspace(str[i]) && (i < len))
365                 i++;
366
367         j = 0;
368         while (i < len) {
369                 /* substitute multiple whitespace with a single '_' */
370                 if (isspace(str[i])) {
371                         while (isspace(str[i]))
372                                 i++;
373                         to[j++] = '_';
374                 }
375                 to[j++] = str[i++];
376         }
377         to[j] = '\0';
378         return 0;
379 }
380
381 static int is_whitelisted(char c, const char *white)
382 {
383         if ((c >= '0' && c <= '9') ||
384             (c >= 'A' && c <= 'Z') ||
385             (c >= 'a' && c <= 'z') ||
386             strchr("#+-.:=@_", c) != NULL ||
387             (white != NULL && strchr(white, c) != NULL))
388                 return 1;
389         return 0;
390 }
391
392 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
393 int udev_util_replace_chars(char *str, const char *white)
394 {
395         size_t i = 0;
396         int replaced = 0;
397
398         while (str[i] != '\0') {
399                 int len;
400
401                 if (is_whitelisted(str[i], white)) {
402                         i++;
403                         continue;
404                 }
405
406                 /* accept hex encoding */
407                 if (str[i] == '\\' && str[i+1] == 'x') {
408                         i += 2;
409                         continue;
410                 }
411
412                 /* accept valid utf8 */
413                 len = utf8_encoded_valid_unichar(&str[i]);
414                 if (len > 1) {
415                         i += len;
416                         continue;
417                 }
418
419                 /* if space is allowed, replace whitespace with ordinary space */
420                 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
421                         str[i] = ' ';
422                         i++;
423                         replaced++;
424                         continue;
425                 }
426
427                 /* everything else is replaced with '_' */
428                 str[i] = '_';
429                 i++;
430                 replaced++;
431         }
432         return replaced;
433 }
434
435 /**
436  * util_encode_string:
437  * @str: input string to be encoded
438  * @str_enc: output string to store the encoded input string
439  * @len: maximum size of the output string, which may be
440  *       four times as long as the input string
441  *
442  * Encode all potentially unsafe characters of a string to the
443  * corresponding hex value prefixed by '\x'.
444  *
445  * Returns: 0 if the entire string was copied, non-zero otherwise.
446  **/
447 int udev_util_encode_string(const char *str, char *str_enc, size_t len)
448 {
449         size_t i, j;
450
451         if (str == NULL || str_enc == NULL || len == 0)
452                 return -1;
453
454         str_enc[0] = '\0';
455         for (i = 0, j = 0; str[i] != '\0'; i++) {
456                 int seqlen;
457
458                 seqlen = utf8_encoded_valid_unichar(&str[i]);
459                 if (seqlen > 1) {
460                         memcpy(&str_enc[j], &str[i], seqlen);
461                         j += seqlen;
462                         i += (seqlen-1);
463                 } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
464                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
465                         j += 4;
466                 } else {
467                         str_enc[j] = str[i];
468                         j++;
469                 }
470                 if (j+3 >= len)
471                         goto err;
472         }
473         str_enc[j] = '\0';
474         return 0;
475 err:
476         return -1;
477 }
478
479 void util_set_fd_cloexec(int fd)
480 {
481         int flags;
482
483         flags = fcntl(fd, F_GETFD);
484         if (flags < 0)
485                 flags = FD_CLOEXEC;
486         else
487                 flags |= FD_CLOEXEC;
488         fcntl(fd, F_SETFD, flags);
489 }
490
491 unsigned int util_string_hash32(const char *str)
492 {
493         unsigned int hash = 0;
494
495         while (str[0] != '\0') {
496                 hash += str[0] << 4;
497                 hash += str[0] >> 4;
498                 hash *= 11;
499                 str++;
500         }
501         return hash;
502 }