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.
30 #include <sys/utsname.h>
34 int string_is_true(const char *str)
36 if (strcasecmp(str, "true") == 0)
38 if (strcasecmp(str, "yes") == 0)
40 if (strcasecmp(str, "1") == 0)
45 void remove_trailing_chars(char *path, char c)
52 while (len > 0 && path[len-1] == c)
56 size_t path_encode(char *s, size_t len)
62 for (i = 0, j = 0; s[i] != '\0'; i++) {
64 memcpy(&t[j], "\\x2f", 4);
66 } else if (s[i] == '\\') {
67 memcpy(&t[j], "\\x5c", 4);
79 size_t path_decode(char *s)
83 for (i = 0, j = 0; s[i] != '\0'; j++) {
84 if (memcmp(&s[i], "\\x2f", 4) == 0) {
87 }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
99 /* count of characters used to encode one unicode char */
100 static int utf8_encoded_expected_len(const char *str)
102 unsigned char c = (unsigned char)str[0];
106 if ((c & 0xe0) == 0xc0)
108 if ((c & 0xf0) == 0xe0)
110 if ((c & 0xf8) == 0xf0)
112 if ((c & 0xfc) == 0xf8)
114 if ((c & 0xfe) == 0xfc)
119 /* decode one unicode char */
120 static int utf8_encoded_to_unichar(const char *str)
126 len = utf8_encoded_expected_len(str);
131 unichar = str[0] & 0x1f;
134 unichar = (int)str[0] & 0x0f;
137 unichar = (int)str[0] & 0x07;
140 unichar = (int)str[0] & 0x03;
143 unichar = (int)str[0] & 0x01;
149 for (i = 1; i < len; i++) {
150 if (((int)str[i] & 0xc0) != 0x80)
153 unichar |= (int)str[i] & 0x3f;
159 /* expected size used to encode one unicode char */
160 static int utf8_unichar_to_encoded_len(int unichar)
166 if (unichar < 0x10000)
168 if (unichar < 0x200000)
170 if (unichar < 0x4000000)
175 /* check if unicode char has a valid numeric range */
176 static int utf8_unichar_valid_range(int unichar)
178 if (unichar > 0x10ffff)
180 if ((unichar & 0xfffff800) == 0xd800)
182 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
184 if ((unichar & 0xffff) == 0xffff)
189 /* validate one encoded unicode char and return its length */
190 int utf8_encoded_valid_unichar(const char *str)
196 len = utf8_encoded_expected_len(str);
204 /* check if expected encoded chars are available */
205 for (i = 0; i < len; i++)
206 if ((str[i] & 0x80) != 0x80)
209 unichar = utf8_encoded_to_unichar(str);
211 /* check if encoded length matches encoded value */
212 if (utf8_unichar_to_encoded_len(unichar) != len)
215 /* check if value has valid range */
216 if (!utf8_unichar_valid_range(unichar))
222 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
223 int replace_chars(char *str, const char *white)
228 while (str[i] != '\0') {
231 /* accept whitelist */
232 if (white != NULL && strchr(white, str[i]) != NULL) {
237 /* accept plain ascii char */
238 if ((str[i] >= '0' && str[i] <= '9') ||
239 (str[i] >= 'A' && str[i] <= 'Z') ||
240 (str[i] >= 'a' && str[i] <= 'z')) {
245 /* accept hex encoding */
246 if (str[i] == '\\' && str[i+1] == 'x') {
251 /* accept valid utf8 */
252 len = utf8_encoded_valid_unichar(&str[i]);
258 /* if space is allowed, replace whitespace with ordinary space */
259 if (isspace(str[i]) && strchr(white, ' ') != NULL) {
266 /* everything else is replaced with '_' */