2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
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.
24 #include "libudev-private.h"
26 static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
28 char path[UTIL_PATH_SIZE];
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))
37 pos = strrchr(path, '/');
41 dbg(udev, "resolved link to: '%s'\n", pos);
42 return util_strscpy(value, size, pos);
45 ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size)
47 return get_sys_link(udev, "subsystem", syspath, subsystem, size);
50 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size)
52 return get_sys_link(udev, "driver", syspath, driver, size);
55 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
57 char link_target[UTIL_PATH_SIZE];
64 len = readlink(syspath, link_target, sizeof(link_target));
65 if (len <= 0 || len == (ssize_t)sizeof(link_target))
67 link_target[len] = '\0';
68 dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target);
70 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
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, '/');
79 dbg(udev, "after moving back '%s'\n", syspath);
80 util_strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
84 int util_log_priority(const char *priority)
89 prio = strtol(priority, &endptr, 10);
90 if (endptr[0] == '\0')
92 if (strncmp(priority, "err", 3) == 0)
94 if (strcmp(priority, "info") == 0)
96 if (strcmp(priority, "debug") == 0)
101 size_t util_path_encode(const char *src, char *dest, size_t size)
105 for (i = 0, j = 0; src[i] != '\0'; i++) {
111 memcpy(&dest[j], "\\x2f", 4);
113 } else if (src[i] == '\\') {
118 memcpy(&dest[j], "\\x5c", 4);
133 size_t util_path_decode(char *s)
137 for (i = 0, j = 0; s[i] != '\0'; j++) {
138 if (memcmp(&s[i], "\\x2f", 4) == 0) {
141 } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
153 void util_remove_trailing_chars(char *path, char c)
160 while (len > 0 && path[len-1] == c)
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.
169 size_t util_strpcpy(char **dest, size_t size, const char *src)
176 *dest = mempcpy(*dest, src, size-1);
181 *dest = mempcpy(*dest, src, len);
189 /* concatenates list of strings, moves dest forward */
190 size_t util_strpcpyl(char **dest, size_t size, const char *src, ...)
196 size = util_strpcpy(dest, size, src);
197 src = va_arg(va, char *);
198 } while (src != NULL);
205 size_t util_strscpy(char *dest, size_t size, const char *src)
210 return util_strpcpy(&s, size, src);
213 /* concatenates list of strings */
214 size_t util_strscpyl(char *dest, size_t size, const char *src, ...)
222 size = util_strpcpy(&s, size, src);
223 src = va_arg(va, char *);
224 } while (src != NULL);
230 /* count of characters used to encode one unicode char */
231 static int utf8_encoded_expected_len(const char *str)
233 unsigned char c = (unsigned char)str[0];
237 if ((c & 0xe0) == 0xc0)
239 if ((c & 0xf0) == 0xe0)
241 if ((c & 0xf8) == 0xf0)
243 if ((c & 0xfc) == 0xf8)
245 if ((c & 0xfe) == 0xfc)
250 /* decode one unicode char */
251 static int utf8_encoded_to_unichar(const char *str)
257 len = utf8_encoded_expected_len(str);
262 unichar = str[0] & 0x1f;
265 unichar = (int)str[0] & 0x0f;
268 unichar = (int)str[0] & 0x07;
271 unichar = (int)str[0] & 0x03;
274 unichar = (int)str[0] & 0x01;
280 for (i = 1; i < len; i++) {
281 if (((int)str[i] & 0xc0) != 0x80)
284 unichar |= (int)str[i] & 0x3f;
290 /* expected size used to encode one unicode char */
291 static int utf8_unichar_to_encoded_len(int unichar)
297 if (unichar < 0x10000)
299 if (unichar < 0x200000)
301 if (unichar < 0x4000000)
306 /* check if unicode char has a valid numeric range */
307 static int utf8_unichar_valid_range(int unichar)
309 if (unichar > 0x10ffff)
311 if ((unichar & 0xfffff800) == 0xd800)
313 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
315 if ((unichar & 0xffff) == 0xffff)
320 /* validate one encoded unicode char and return its length */
321 static int utf8_encoded_valid_unichar(const char *str)
327 len = utf8_encoded_expected_len(str);
335 /* check if expected encoded chars are available */
336 for (i = 0; i < len; i++)
337 if ((str[i] & 0x80) != 0x80)
340 unichar = utf8_encoded_to_unichar(str);
342 /* check if encoded length matches encoded value */
343 if (utf8_unichar_to_encoded_len(unichar) != len)
346 /* check if value has valid range */
347 if (!utf8_unichar_valid_range(unichar))
353 int udev_util_replace_whitespace(const char *str, char *to, size_t len)
357 /* strip trailing whitespace */
358 len = strnlen(str, len);
359 while (len && isspace(str[len-1]))
362 /* strip leading whitespace */
364 while (isspace(str[i]) && (i < len))
369 /* substitute multiple whitespace with a single '_' */
370 if (isspace(str[i])) {
371 while (isspace(str[i]))
381 static int is_whitelisted(char c, const char *white)
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))
392 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
393 int udev_util_replace_chars(char *str, const char *white)
398 while (str[i] != '\0') {
401 if (is_whitelisted(str[i], white)) {
406 /* accept hex encoding */
407 if (str[i] == '\\' && str[i+1] == 'x') {
412 /* accept valid utf8 */
413 len = utf8_encoded_valid_unichar(&str[i]);
419 /* if space is allowed, replace whitespace with ordinary space */
420 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
427 /* everything else is replaced with '_' */
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
442 * Encode all potentially unsafe characters of a string to the
443 * corresponding hex value prefixed by '\x'.
445 * Returns: 0 if the entire string was copied, non-zero otherwise.
447 int udev_util_encode_string(const char *str, char *str_enc, size_t len)
451 if (str == NULL || str_enc == NULL)
454 for (i = 0, j = 0; str[i] != '\0'; i++) {
457 seqlen = utf8_encoded_valid_unichar(&str[i]);
459 if (len-j < (size_t)seqlen)
461 memcpy(&str_enc[j], &str[i], seqlen);
464 } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
467 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
484 unsigned int util_string_hash32(const char *str)
486 unsigned int hash = 0;
488 while (str[0] != '\0') {