2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2011 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.
25 #include "libudev-private.h"
28 * SECTION:libudev-util
29 * @short_description: utils
32 ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
34 char path[UTIL_PATH_SIZE];
35 char target[UTIL_PATH_SIZE];
39 util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
40 len = readlink(path, target, sizeof(target));
41 if (len <= 0 || len == (ssize_t)sizeof(target))
44 pos = strrchr(target, '/');
48 return util_strscpy(value, size, pos);
51 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
53 char link_target[UTIL_PATH_SIZE];
60 len = readlink(syspath, link_target, sizeof(link_target));
61 if (len <= 0 || len == (ssize_t)sizeof(link_target))
63 link_target[len] = '\0';
65 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
67 for (i = 0; i <= back; i++) {
68 base = strrchr(syspath, '/');
75 util_strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
79 int util_log_priority(const char *priority)
84 prio = strtol(priority, &endptr, 10);
85 if (endptr[0] == '\0' || isspace(endptr[0]))
87 if (strncmp(priority, "err", 3) == 0)
89 if (strncmp(priority, "info", 4) == 0)
91 if (strncmp(priority, "debug", 5) == 0)
96 size_t util_path_encode(const char *src, char *dest, size_t size)
100 for (i = 0, j = 0; src[i] != '\0'; i++) {
106 memcpy(&dest[j], "\\x2f", 4);
108 } else if (src[i] == '\\') {
113 memcpy(&dest[j], "\\x5c", 4);
128 size_t util_path_decode(char *s)
132 for (i = 0, j = 0; s[i] != '\0'; j++) {
133 if (memcmp(&s[i], "\\x2f", 4) == 0) {
136 } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
148 void util_remove_trailing_chars(char *path, char c)
155 while (len > 0 && path[len-1] == c)
160 * Concatenates strings. In any case, terminates in _all_ cases with '\0'
161 * and moves the @dest pointer forward to the added '\0'. Returns the
162 * remaining size, and 0 if the string was truncated.
164 size_t util_strpcpy(char **dest, size_t size, const char *src)
171 *dest = mempcpy(*dest, src, size-1);
176 *dest = mempcpy(*dest, src, len);
184 /* concatenates list of strings, moves dest forward */
185 size_t util_strpcpyl(char **dest, size_t size, const char *src, ...)
191 size = util_strpcpy(dest, size, src);
192 src = va_arg(va, char *);
193 } while (src != NULL);
200 size_t util_strscpy(char *dest, size_t size, const char *src)
205 return util_strpcpy(&s, size, src);
208 /* concatenates list of strings */
209 size_t util_strscpyl(char *dest, size_t size, const char *src, ...)
217 size = util_strpcpy(&s, size, src);
218 src = va_arg(va, char *);
219 } while (src != NULL);
225 /* count of characters used to encode one unicode char */
226 static int utf8_encoded_expected_len(const char *str)
228 unsigned char c = (unsigned char)str[0];
232 if ((c & 0xe0) == 0xc0)
234 if ((c & 0xf0) == 0xe0)
236 if ((c & 0xf8) == 0xf0)
238 if ((c & 0xfc) == 0xf8)
240 if ((c & 0xfe) == 0xfc)
245 /* decode one unicode char */
246 static int utf8_encoded_to_unichar(const char *str)
252 len = utf8_encoded_expected_len(str);
257 unichar = str[0] & 0x1f;
260 unichar = (int)str[0] & 0x0f;
263 unichar = (int)str[0] & 0x07;
266 unichar = (int)str[0] & 0x03;
269 unichar = (int)str[0] & 0x01;
275 for (i = 1; i < len; i++) {
276 if (((int)str[i] & 0xc0) != 0x80)
279 unichar |= (int)str[i] & 0x3f;
285 /* expected size used to encode one unicode char */
286 static int utf8_unichar_to_encoded_len(int unichar)
292 if (unichar < 0x10000)
294 if (unichar < 0x200000)
296 if (unichar < 0x4000000)
301 /* check if unicode char has a valid numeric range */
302 static int utf8_unichar_valid_range(int unichar)
304 if (unichar > 0x10ffff)
306 if ((unichar & 0xfffff800) == 0xd800)
308 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
310 if ((unichar & 0xffff) == 0xffff)
315 /* validate one encoded unicode char and return its length */
316 static int utf8_encoded_valid_unichar(const char *str)
322 len = utf8_encoded_expected_len(str);
330 /* check if expected encoded chars are available */
331 for (i = 0; i < len; i++)
332 if ((str[i] & 0x80) != 0x80)
335 unichar = utf8_encoded_to_unichar(str);
337 /* check if encoded length matches encoded value */
338 if (utf8_unichar_to_encoded_len(unichar) != len)
341 /* check if value has valid range */
342 if (!utf8_unichar_valid_range(unichar))
348 int util_replace_whitespace(const char *str, char *to, size_t len)
352 /* strip trailing whitespace */
353 len = strnlen(str, len);
354 while (len && isspace(str[len-1]))
357 /* strip leading whitespace */
359 while (isspace(str[i]) && (i < len))
364 /* substitute multiple whitespace with a single '_' */
365 if (isspace(str[i])) {
366 while (isspace(str[i]))
376 static int is_whitelisted(char c, const char *white)
378 if ((c >= '0' && c <= '9') ||
379 (c >= 'A' && c <= 'Z') ||
380 (c >= 'a' && c <= 'z') ||
381 strchr("#+-.:=@_", c) != NULL ||
382 (white != NULL && strchr(white, c) != NULL))
387 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
388 int util_replace_chars(char *str, const char *white)
393 while (str[i] != '\0') {
396 if (is_whitelisted(str[i], white)) {
401 /* accept hex encoding */
402 if (str[i] == '\\' && str[i+1] == 'x') {
407 /* accept valid utf8 */
408 len = utf8_encoded_valid_unichar(&str[i]);
414 /* if space is allowed, replace whitespace with ordinary space */
415 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
422 /* everything else is replaced with '_' */
431 * udev_util_encode_string:
432 * @str: input string to be encoded
433 * @str_enc: output string to store the encoded input string
434 * @len: maximum size of the output string, which may be
435 * four times as long as the input string
437 * Encode all potentially unsafe characters of a string to the
438 * corresponding 2 char hex value prefixed by '\x'.
440 * Returns: 0 if the entire string was copied, non-zero otherwise.
442 _public_ int udev_util_encode_string(const char *str, char *str_enc, size_t len)
446 if (str == NULL || str_enc == NULL)
449 for (i = 0, j = 0; str[i] != '\0'; i++) {
452 seqlen = utf8_encoded_valid_unichar(&str[i]);
454 if (len-j < (size_t)seqlen)
456 memcpy(&str_enc[j], &str[i], seqlen);
459 } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
462 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
480 * http://sites.google.com/site/murmurhash/
482 * All code is released to the public domain. For business purposes,
483 * Murmurhash is under the MIT license.
486 static unsigned int murmur_hash2(const char *key, int len, unsigned int seed)
489 * 'm' and 'r' are mixing constants generated offline.
490 * They're not really 'magic', they just happen to work well.
492 const unsigned int m = 0x5bd1e995;
495 /* initialize the hash to a 'random' value */
496 unsigned int h = seed ^ len;
498 /* mix 4 bytes at a time into the hash */
499 const unsigned char * data = (const unsigned char *)key;
502 unsigned int k = *(unsigned int *)data;
514 /* handle the last few bytes of the input array */
525 /* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */
533 unsigned int util_string_hash32(const char *str)
535 return murmur_hash2(str, strlen(str), 0);
538 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
539 uint64_t util_string_bloom64(const char *str)
542 unsigned int hash = util_string_hash32(str);
544 bits |= 1LLU << (hash & 63);
545 bits |= 1LLU << ((hash >> 6) & 63);
546 bits |= 1LLU << ((hash >> 12) & 63);
547 bits |= 1LLU << ((hash >> 18) & 63);
551 #define USEC_PER_SEC 1000000ULL
552 #define NSEC_PER_USEC 1000ULL
553 unsigned long long ts_usec(const struct timespec *ts)
555 return (unsigned long long) ts->tv_sec * USEC_PER_SEC +
556 (unsigned long long) ts->tv_nsec / NSEC_PER_USEC;
559 unsigned long long now_usec(void)
563 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)