2 * udev_utils_string.c - string manipulation
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <sys/utsname.h>
35 int string_is_true(const char *str)
37 if (strcasecmp(str, "true") == 0)
39 if (strcasecmp(str, "yes") == 0)
41 if (strcasecmp(str, "1") == 0)
46 void remove_trailing_chars(char *path, char c)
51 while (len > 0 && path[len-1] == c)
55 /* count of characters used to encode one unicode char */
56 static int utf8_encoded_expected_len(const char *str)
58 unsigned char c = (unsigned char)str[0];
62 if ((c & 0xe0) == 0xc0)
64 if ((c & 0xf0) == 0xe0)
66 if ((c & 0xf8) == 0xf0)
68 if ((c & 0xfc) == 0xf8)
70 if ((c & 0xfe) == 0xfc)
75 /* decode one unicode char */
76 static int utf8_encoded_to_unichar(const char *str)
82 len = utf8_encoded_expected_len(str);
87 unichar = str[0] & 0x1f;
90 unichar = (int)str[0] & 0x0f;
93 unichar = (int)str[0] & 0x07;
96 unichar = (int)str[0] & 0x03;
99 unichar = (int)str[0] & 0x01;
105 for (i = 1; i < len; i++) {
106 if (((int)str[i] & 0xc0) != 0x80)
109 unichar |= (int)str[i] & 0x3f;
115 /* expected size used to encode one unicode char */
116 static int utf8_unichar_to_encoded_len(int unichar)
122 if (unichar < 0x10000)
124 if (unichar < 0x200000)
126 if (unichar < 0x4000000)
131 /* check if unicode char has a valid numeric range */
132 static int utf8_unichar_valid_range(int unichar)
134 if (unichar > 0x10ffff)
136 if ((unichar & 0xfffff800) == 0xd800)
138 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
140 if ((unichar & 0xffff) == 0xffff)
145 /* validate one encoded unicode char and return its length */
146 int utf8_encoded_valid_unichar(const char *str)
152 len = utf8_encoded_expected_len(str);
160 /* check if expected encoded chars are available */
161 for (i = 0; i < len; i++)
162 if ((str[i] & 0x80) != 0x80)
165 unichar = utf8_encoded_to_unichar(str);
167 /* check if encoded length matches encoded value */
168 if (utf8_unichar_to_encoded_len(unichar) != len)
171 /* check if value has valid range */
172 if (!utf8_unichar_valid_range(unichar))
178 /* replace everything but whitelisted plain ascii and valid utf8 */
179 int replace_untrusted_chars(char *str)
184 while (str[i] != '\0') {
187 /* valid printable ascii char */
188 if ((str[i] >= '0' && str[i] <= '9') ||
189 (str[i] >= 'A' && str[i] <= 'Z') ||
190 (str[i] >= 'a' && str[i] <= 'z') ||
191 strchr(" #$%+-./:=?@_,", str[i])) {
195 /* valid utf8 is accepted */
196 len = utf8_encoded_valid_unichar(&str[i]);
202 /* everything else is garbage */