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)
50 while (len > 0 && path[len-1] == c)
54 size_t path_encode(char *s, size_t len)
60 for (i = 0, j = 0; s[i] != '\0'; i++) {
62 memcpy(&t[j], "\\x2f", 4);
64 } else if (s[i] == '\\') {
65 memcpy(&t[j], "\\x5c", 4);
77 size_t path_decode(char *s)
81 for (i = 0, j = 0; s[i] != '\0'; j++) {
82 if (memcmp(&s[i], "\\x2f", 4) == 0) {
85 }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
97 /* count of characters used to encode one unicode char */
98 static int utf8_encoded_expected_len(const char *str)
100 unsigned char c = (unsigned char)str[0];
104 if ((c & 0xe0) == 0xc0)
106 if ((c & 0xf0) == 0xe0)
108 if ((c & 0xf8) == 0xf0)
110 if ((c & 0xfc) == 0xf8)
112 if ((c & 0xfe) == 0xfc)
117 /* decode one unicode char */
118 static int utf8_encoded_to_unichar(const char *str)
124 len = utf8_encoded_expected_len(str);
129 unichar = str[0] & 0x1f;
132 unichar = (int)str[0] & 0x0f;
135 unichar = (int)str[0] & 0x07;
138 unichar = (int)str[0] & 0x03;
141 unichar = (int)str[0] & 0x01;
147 for (i = 1; i < len; i++) {
148 if (((int)str[i] & 0xc0) != 0x80)
151 unichar |= (int)str[i] & 0x3f;
157 /* expected size used to encode one unicode char */
158 static int utf8_unichar_to_encoded_len(int unichar)
164 if (unichar < 0x10000)
166 if (unichar < 0x200000)
168 if (unichar < 0x4000000)
173 /* check if unicode char has a valid numeric range */
174 static int utf8_unichar_valid_range(int unichar)
176 if (unichar > 0x10ffff)
178 if ((unichar & 0xfffff800) == 0xd800)
180 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
182 if ((unichar & 0xffff) == 0xffff)
187 /* validate one encoded unicode char and return its length */
188 int utf8_encoded_valid_unichar(const char *str)
194 len = utf8_encoded_expected_len(str);
202 /* check if expected encoded chars are available */
203 for (i = 0; i < len; i++)
204 if ((str[i] & 0x80) != 0x80)
207 unichar = utf8_encoded_to_unichar(str);
209 /* check if encoded length matches encoded value */
210 if (utf8_unichar_to_encoded_len(unichar) != len)
213 /* check if value has valid range */
214 if (!utf8_unichar_valid_range(unichar))
220 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
221 int replace_chars(char *str, const char *white)
226 while (str[i] != '\0') {
229 /* accept whitelist */
230 if (white != NULL && strchr(white, str[i]) != NULL) {
235 /* accept plain ascii char */
236 if ((str[i] >= '0' && str[i] <= '9') ||
237 (str[i] >= 'A' && str[i] <= 'Z') ||
238 (str[i] >= 'a' && str[i] <= 'z')) {
243 /* accept hex encoding */
244 if (str[i] == '\\' && str[i+1] == 'x') {
249 /* accept valid utf8 */
250 len = utf8_encoded_valid_unichar(&str[i]);
256 /* if space is allowed, replace whitespace with ordinary space */
257 if (isspace(str[i]) && strchr(white, ' ') != NULL) {
264 /* everything else is replaced with '_' */