2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-09-18 18:12:04 +02:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <errno.h>
|
2013-09-18 18:12:04 +02:00
|
|
|
#include <stdio.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <string.h>
|
2013-09-18 18:12:04 +02:00
|
|
|
|
|
|
|
#include "device-nodes.h"
|
|
|
|
#include "utf8.h"
|
|
|
|
|
|
|
|
int whitelisted_char_for_devnode(char c, const char *white) {
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
if ((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
strchr("#+-.:=@_", c) != NULL ||
|
|
|
|
(white != NULL && strchr(white, c) != NULL))
|
|
|
|
return 1;
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int encode_devnode_name(const char *str, char *str_enc, size_t len) {
|
|
|
|
size_t i, j;
|
|
|
|
|
2017-12-08 20:52:38 +01:00
|
|
|
if (!str || !str_enc)
|
2014-08-11 18:35:54 +02:00
|
|
|
return -EINVAL;
|
2013-09-18 18:12:04 +02:00
|
|
|
|
|
|
|
for (i = 0, j = 0; str[i] != '\0'; i++) {
|
|
|
|
int seqlen;
|
|
|
|
|
2019-02-26 12:37:40 +01:00
|
|
|
seqlen = utf8_encoded_valid_unichar(str + i, (size_t) -1);
|
2013-09-18 18:12:04 +02:00
|
|
|
if (seqlen > 1) {
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
if (len-j < (size_t)seqlen)
|
2015-04-10 14:43:37 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
memcpy(&str_enc[j], &str[i], seqlen);
|
|
|
|
j += seqlen;
|
|
|
|
i += (seqlen-1);
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
} else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) {
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
if (len-j < 4)
|
2015-04-10 14:43:37 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
|
|
|
|
j += 4;
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
} else {
|
|
|
|
if (len-j < 1)
|
2015-04-10 14:43:37 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
str_enc[j] = str[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 14:43:37 +02:00
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
if (len-j < 1)
|
2015-04-10 14:43:37 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-18 18:12:04 +02:00
|
|
|
str_enc[j] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|