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.
25 #include "libudev-private.h"
27 ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
29 char path[UTIL_PATH_SIZE];
30 char target[UTIL_PATH_SIZE];
34 util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
35 len = readlink(path, target, sizeof(target));
36 if (len <= 0 || len == (ssize_t)sizeof(target))
39 pos = strrchr(target, '/');
43 dbg(udev, "resolved link to: '%s'\n", pos);
44 return util_strscpy(value, size, pos);
47 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
49 char link_target[UTIL_PATH_SIZE];
56 len = readlink(syspath, link_target, sizeof(link_target));
57 if (len <= 0 || len == (ssize_t)sizeof(link_target))
59 link_target[len] = '\0';
60 dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target);
62 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
64 dbg(udev, "base '%s', tail '%s', back %i\n", syspath, &link_target[back * 3], back);
65 for (i = 0; i <= back; i++) {
66 base = strrchr(syspath, '/');
71 dbg(udev, "after moving back '%s'\n", syspath);
72 util_strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
76 int util_log_priority(const char *priority)
81 prio = strtol(priority, &endptr, 10);
82 if (endptr[0] == '\0' || isspace(endptr[0]))
84 if (strncmp(priority, "err", 3) == 0)
86 if (strncmp(priority, "info", 4) == 0)
88 if (strncmp(priority, "debug", 5) == 0)
93 size_t util_path_encode(const char *src, char *dest, size_t size)
97 for (i = 0, j = 0; src[i] != '\0'; i++) {
103 memcpy(&dest[j], "\\x2f", 4);
105 } else if (src[i] == '\\') {
110 memcpy(&dest[j], "\\x5c", 4);
125 size_t util_path_decode(char *s)
129 for (i = 0, j = 0; s[i] != '\0'; j++) {
130 if (memcmp(&s[i], "\\x2f", 4) == 0) {
133 } else if (memcmp(&s[i], "\\x5c", 4) == 0) {
145 void util_remove_trailing_chars(char *path, char c)
152 while (len > 0 && path[len-1] == c)
157 * Concatenates strings. In any case, terminates in _all_ cases with '\0'
158 * and moves the @dest pointer forward to the added '\0'. Returns the
159 * remaining size, and 0 if the string was truncated.
161 size_t util_strpcpy(char **dest, size_t size, const char *src)
168 *dest = mempcpy(*dest, src, size-1);
173 *dest = mempcpy(*dest, src, len);
181 /* concatenates list of strings, moves dest forward */
182 size_t util_strpcpyl(char **dest, size_t size, const char *src, ...)
188 size = util_strpcpy(dest, size, src);
189 src = va_arg(va, char *);
190 } while (src != NULL);
197 size_t util_strscpy(char *dest, size_t size, const char *src)
202 return util_strpcpy(&s, size, src);
205 /* concatenates list of strings */
206 size_t util_strscpyl(char *dest, size_t size, const char *src, ...)
214 size = util_strpcpy(&s, size, src);
215 src = va_arg(va, char *);
216 } while (src != NULL);
222 /* count of characters used to encode one unicode char */
223 static int utf8_encoded_expected_len(const char *str)
225 unsigned char c = (unsigned char)str[0];
229 if ((c & 0xe0) == 0xc0)
231 if ((c & 0xf0) == 0xe0)
233 if ((c & 0xf8) == 0xf0)
235 if ((c & 0xfc) == 0xf8)
237 if ((c & 0xfe) == 0xfc)
242 /* decode one unicode char */
243 static int utf8_encoded_to_unichar(const char *str)
249 len = utf8_encoded_expected_len(str);
254 unichar = str[0] & 0x1f;
257 unichar = (int)str[0] & 0x0f;
260 unichar = (int)str[0] & 0x07;
263 unichar = (int)str[0] & 0x03;
266 unichar = (int)str[0] & 0x01;
272 for (i = 1; i < len; i++) {
273 if (((int)str[i] & 0xc0) != 0x80)
276 unichar |= (int)str[i] & 0x3f;
282 /* expected size used to encode one unicode char */
283 static int utf8_unichar_to_encoded_len(int unichar)
289 if (unichar < 0x10000)
291 if (unichar < 0x200000)
293 if (unichar < 0x4000000)
298 /* check if unicode char has a valid numeric range */
299 static int utf8_unichar_valid_range(int unichar)
301 if (unichar > 0x10ffff)
303 if ((unichar & 0xfffff800) == 0xd800)
305 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
307 if ((unichar & 0xffff) == 0xffff)
312 /* validate one encoded unicode char and return its length */
313 static int utf8_encoded_valid_unichar(const char *str)
319 len = utf8_encoded_expected_len(str);
327 /* check if expected encoded chars are available */
328 for (i = 0; i < len; i++)
329 if ((str[i] & 0x80) != 0x80)
332 unichar = utf8_encoded_to_unichar(str);
334 /* check if encoded length matches encoded value */
335 if (utf8_unichar_to_encoded_len(unichar) != len)
338 /* check if value has valid range */
339 if (!utf8_unichar_valid_range(unichar))
345 int udev_util_replace_whitespace(const char *str, char *to, size_t len)
349 /* strip trailing whitespace */
350 len = strnlen(str, len);
351 while (len && isspace(str[len-1]))
354 /* strip leading whitespace */
356 while (isspace(str[i]) && (i < len))
361 /* substitute multiple whitespace with a single '_' */
362 if (isspace(str[i])) {
363 while (isspace(str[i]))
373 static int is_whitelisted(char c, const char *white)
375 if ((c >= '0' && c <= '9') ||
376 (c >= 'A' && c <= 'Z') ||
377 (c >= 'a' && c <= 'z') ||
378 strchr("#+-.:=@_", c) != NULL ||
379 (white != NULL && strchr(white, c) != NULL))
384 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
385 int udev_util_replace_chars(char *str, const char *white)
390 while (str[i] != '\0') {
393 if (is_whitelisted(str[i], white)) {
398 /* accept hex encoding */
399 if (str[i] == '\\' && str[i+1] == 'x') {
404 /* accept valid utf8 */
405 len = utf8_encoded_valid_unichar(&str[i]);
411 /* if space is allowed, replace whitespace with ordinary space */
412 if (isspace(str[i]) && white != NULL && strchr(white, ' ') != NULL) {
419 /* everything else is replaced with '_' */
428 * util_encode_string:
429 * @str: input string to be encoded
430 * @str_enc: output string to store the encoded input string
431 * @len: maximum size of the output string, which may be
432 * four times as long as the input string
434 * Encode all potentially unsafe characters of a string to the
435 * corresponding hex value prefixed by '\x'.
437 * Returns: 0 if the entire string was copied, non-zero otherwise.
439 int udev_util_encode_string(const char *str, char *str_enc, size_t len)
443 if (str == NULL || str_enc == NULL)
446 for (i = 0, j = 0; str[i] != '\0'; i++) {
449 seqlen = utf8_encoded_valid_unichar(&str[i]);
451 if (len-j < (size_t)seqlen)
453 memcpy(&str_enc[j], &str[i], seqlen);
456 } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
459 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
477 * http://sites.google.com/site/murmurhash/
479 * All code is released to the public domain. For business purposes,
480 * Murmurhash is under the MIT license.
483 static unsigned int murmur_hash2(const char *key, int len, unsigned int seed)
486 * 'm' and 'r' are mixing constants generated offline.
487 * They're not really 'magic', they just happen to work well.
489 const unsigned int m = 0x5bd1e995;
492 /* initialize the hash to a 'random' value */
493 unsigned int h = seed ^ len;
495 /* mix 4 bytes at a time into the hash */
496 const unsigned char * data = (const unsigned char *)key;
499 unsigned int k = *(unsigned int *)data;
511 /* handle the last few bytes of the input array */
522 /* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */
530 unsigned int util_string_hash32(const char *str)
532 return murmur_hash2(str, strlen(str), 0);
535 /* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
536 uint64_t util_string_bloom64(const char *str)
539 unsigned int hash = util_string_hash32(str);
541 bits |= 1LLU << (hash & 63);
542 bits |= 1LLU << ((hash >> 6) & 63);
543 bits |= 1LLU << ((hash >> 12) & 63);
544 bits |= 1LLU << ((hash >> 18) & 63);
548 #define USEC_PER_SEC 1000000ULL
549 #define NSEC_PER_USEC 1000ULL
550 unsigned long long now_usec(void)
554 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
556 return (unsigned long long) ts.tv_sec * USEC_PER_SEC +
557 (unsigned long long) ts.tv_nsec / NSEC_PER_USEC;