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];
29 char target[UTIL_PATH_SIZE];
33 util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
34 len = readlink(path, target, sizeof(target));
35 if (len <= 0 || len == (ssize_t)sizeof(target))
38 pos = strrchr(target, '/');
42 dbg(udev, "resolved link to: '%s'\n", pos);
43 return util_strscpy(value, size, pos);
46 ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size)
48 return get_sys_link(udev, "subsystem", syspath, subsystem, size);
51 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size)
53 return get_sys_link(udev, "driver", syspath, driver, size);
56 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
58 char link_target[UTIL_PATH_SIZE];
65 len = readlink(syspath, link_target, sizeof(link_target));
66 if (len <= 0 || len == (ssize_t)sizeof(link_target))
68 link_target[len] = '\0';
69 dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target);
71 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
73 dbg(udev, "base '%s', tail '%s', back %i\n", syspath, &link_target[back * 3], back);
74 for (i = 0; i <= back; i++) {
75 base = strrchr(syspath, '/');
80 dbg(udev, "after moving back '%s'\n", syspath);
81 util_strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
85 int util_log_priority(const char *priority)
90 prio = strtol(priority, &endptr, 10);
91 if (endptr[0] == '\0' || isspace(endptr[0]))
93 if (strncmp(priority, "err", 3) == 0)
95 if (strncmp(priority, "info", 4) == 0)
97 if (strncmp(priority, "debug", 5) == 0)
102 size_t util_path_encode(const char *src, char *dest, size_t size)
106 for (i = 0, j = 0; src[i] != '\0'; i++) {
112 memcpy(&dest[j], "\\x2f", 4);
114 } else if (src[i] == '\\') {
119 memcpy(&dest[j], "\\x5c", 4);
134 size_t util_path_decode(char *s)
138 for (i = 0, j = 0; s[i] != '\0'; j++) {
139 if (memcmp(&s[i], "\\x2f", 4) == 0) {
142 } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
154 void util_remove_trailing_chars(char *path, char c)
161 while (len > 0 && path[len-1] == c)
166 * Concatenates strings. In any case, terminates in _all_ cases with '\0'
167 * and moves the @dest pointer forward to the added '\0'. Returns the
168 * remaining size, and 0 if the string was truncated.
170 size_t util_strpcpy(char **dest, size_t size, const char *src)
177 *dest = mempcpy(*dest, src, size-1);
182 *dest = mempcpy(*dest, src, len);
190 /* concatenates list of strings, moves dest forward */
191 size_t util_strpcpyl(char **dest, size_t size, const char *src, ...)
197 size = util_strpcpy(dest, size, src);
198 src = va_arg(va, char *);
199 } while (src != NULL);
206 size_t util_strscpy(char *dest, size_t size, const char *src)
211 return util_strpcpy(&s, size, src);
214 /* concatenates list of strings */
215 size_t util_strscpyl(char *dest, size_t size, const char *src, ...)
223 size = util_strpcpy(&s, size, src);
224 src = va_arg(va, char *);
225 } while (src != NULL);
231 /* count of characters used to encode one unicode char */
232 static int utf8_encoded_expected_len(const char *str)
234 unsigned char c = (unsigned char)str[0];
238 if ((c & 0xe0) == 0xc0)
240 if ((c & 0xf0) == 0xe0)
242 if ((c & 0xf8) == 0xf0)
244 if ((c & 0xfc) == 0xf8)
246 if ((c & 0xfe) == 0xfc)
251 /* decode one unicode char */
252 static int utf8_encoded_to_unichar(const char *str)
258 len = utf8_encoded_expected_len(str);
263 unichar = str[0] & 0x1f;
266 unichar = (int)str[0] & 0x0f;
269 unichar = (int)str[0] & 0x07;
272 unichar = (int)str[0] & 0x03;
275 unichar = (int)str[0] & 0x01;
281 for (i = 1; i < len; i++) {
282 if (((int)str[i] & 0xc0) != 0x80)
285 unichar |= (int)str[i] & 0x3f;
291 /* expected size used to encode one unicode char */
292 static int utf8_unichar_to_encoded_len(int unichar)
298 if (unichar < 0x10000)
300 if (unichar < 0x200000)
302 if (unichar < 0x4000000)
307 /* check if unicode char has a valid numeric range */
308 static int utf8_unichar_valid_range(int unichar)
310 if (unichar > 0x10ffff)
312 if ((unichar & 0xfffff800) == 0xd800)
314 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
316 if ((unichar & 0xffff) == 0xffff)
321 /* validate one encoded unicode char and return its length */
322 static int utf8_encoded_valid_unichar(const char *str)
328 len = utf8_encoded_expected_len(str);
336 /* check if expected encoded chars are available */
337 for (i = 0; i < len; i++)
338 if ((str[i] & 0x80) != 0x80)
341 unichar = utf8_encoded_to_unichar(str);
343 /* check if encoded length matches encoded value */
344 if (utf8_unichar_to_encoded_len(unichar) != len)
347 /* check if value has valid range */
348 if (!utf8_unichar_valid_range(unichar))
354 int udev_util_replace_whitespace(const char *str, char *to, size_t len)
358 /* strip trailing whitespace */
359 len = strnlen(str, len);
360 while (len && isspace(str[len-1]))
363 /* strip leading whitespace */
365 while (isspace(str[i]) && (i < len))
370 /* substitute multiple whitespace with a single '_' */
371 if (isspace(str[i])) {
372 while (isspace(str[i]))
382 static int is_whitelisted(char c, const char *white)
384 if ((c >= '0' && c <= '9') ||
385 (c >= 'A' && c <= 'Z') ||
386 (c >= 'a' && c <= 'z') ||
387 strchr("#+-.:=@_", c) != NULL ||
388 (white != NULL && strchr(white, c) != NULL))
393 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
394 int udev_util_replace_chars(char *str, const char *white)
399 while (str[i] != '\0') {
402 if (is_whitelisted(str[i], white)) {
407 /* accept hex encoding */
408 if (str[i] == '\\' && str[i+1] == 'x') {
413 /* accept valid utf8 */
414 len = utf8_encoded_valid_unichar(&str[i]);
420 /* if space is allowed, replace whitespace with ordinary space */
421 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
428 /* everything else is replaced with '_' */
437 * util_encode_string:
438 * @str: input string to be encoded
439 * @str_enc: output string to store the encoded input string
440 * @len: maximum size of the output string, which may be
441 * four times as long as the input string
443 * Encode all potentially unsafe characters of a string to the
444 * corresponding hex value prefixed by '\x'.
446 * Returns: 0 if the entire string was copied, non-zero otherwise.
448 int udev_util_encode_string(const char *str, char *str_enc, size_t len)
452 if (str == NULL || str_enc == NULL)
455 for (i = 0, j = 0; str[i] != '\0'; i++) {
458 seqlen = utf8_encoded_valid_unichar(&str[i]);
460 if (len-j < (size_t)seqlen)
462 memcpy(&str_enc[j], &str[i], seqlen);
465 } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
468 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
486 * http://sites.google.com/site/murmurhash/
488 * All code is released to the public domain. For business purposes,
489 * Murmurhash is under the MIT license.
492 static unsigned int murmur_hash2(const char *key, int len, unsigned int seed)
495 * 'm' and 'r' are mixing constants generated offline.
496 * They're not really 'magic', they just happen to work well.
498 const unsigned int m = 0x5bd1e995;
501 /* initialize the hash to a 'random' value */
502 unsigned int h = seed ^ len;
504 /* mix 4 bytes at a time into the hash */
505 const unsigned char * data = (const unsigned char *)key;
508 unsigned int k = *(unsigned int *)data;
520 /* handle the last few bytes of the input array */
531 /* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */
539 unsigned int util_string_hash32(const char *str)
541 return murmur_hash2(str, strlen(str), 0);
544 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
545 uint64_t util_string_bloom64(const char *str)
548 unsigned int hash = util_string_hash32(str);
550 bits |= 1LLU << (hash & 63);
551 bits |= 1LLU << ((hash >> 6) & 63);
552 bits |= 1LLU << ((hash >> 12) & 63);
553 bits |= 1LLU << ((hash >> 18) & 63);