2 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <sys/utsname.h>
33 int string_is_true(const char *str)
35 if (strcasecmp(str, "true") == 0)
37 if (strcasecmp(str, "yes") == 0)
39 if (strcasecmp(str, "1") == 0)
44 void remove_trailing_chars(char *path, char c)
49 while (len > 0 && path[len-1] == c)
53 /* count of characters used to encode one unicode char */
54 static int utf8_encoded_expected_len(const char *str)
56 unsigned char c = (unsigned char)str[0];
60 if ((c & 0xe0) == 0xc0)
62 if ((c & 0xf0) == 0xe0)
64 if ((c & 0xf8) == 0xf0)
66 if ((c & 0xfc) == 0xf8)
68 if ((c & 0xfe) == 0xfc)
73 /* decode one unicode char */
74 static int utf8_encoded_to_unichar(const char *str)
80 len = utf8_encoded_expected_len(str);
85 unichar = str[0] & 0x1f;
88 unichar = (int)str[0] & 0x0f;
91 unichar = (int)str[0] & 0x07;
94 unichar = (int)str[0] & 0x03;
97 unichar = (int)str[0] & 0x01;
103 for (i = 1; i < len; i++) {
104 if (((int)str[i] & 0xc0) != 0x80)
107 unichar |= (int)str[i] & 0x3f;
113 /* expected size used to encode one unicode char */
114 static int utf8_unichar_to_encoded_len(int unichar)
120 if (unichar < 0x10000)
122 if (unichar < 0x200000)
124 if (unichar < 0x4000000)
129 /* check if unicode char has a valid numeric range */
130 static int utf8_unichar_valid_range(int unichar)
132 if (unichar > 0x10ffff)
134 if ((unichar & 0xfffff800) == 0xd800)
136 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
138 if ((unichar & 0xffff) == 0xffff)
143 /* validate one encoded unicode char and return its length */
144 int utf8_encoded_valid_unichar(const char *str)
150 len = utf8_encoded_expected_len(str);
158 /* check if expected encoded chars are available */
159 for (i = 0; i < len; i++)
160 if ((str[i] & 0x80) != 0x80)
163 unichar = utf8_encoded_to_unichar(str);
165 /* check if encoded length matches encoded value */
166 if (utf8_unichar_to_encoded_len(unichar) != len)
169 /* check if value has valid range */
170 if (!utf8_unichar_valid_range(unichar))
176 /* replace everything but whitelisted plain ascii and valid utf8 */
177 int replace_untrusted_chars(char *str)
182 while (str[i] != '\0') {
185 /* valid printable ascii char */
186 if ((str[i] >= '0' && str[i] <= '9') ||
187 (str[i] >= 'A' && str[i] <= 'Z') ||
188 (str[i] >= 'a' && str[i] <= 'z') ||
189 strchr(" #$%+-./:=?@_,", str[i])) {
193 /* valid utf8 is accepted */
194 len = utf8_encoded_valid_unichar(&str[i]);
200 /* everything else is garbage */