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 size_t path_encode(char *s
, size_t len
)
59 for (i
= 0, j
= 0; s
[i
] != '\0'; i
++) {
61 memcpy(&t
[j
], "\\x2f", 4);
63 } else if (s
[i
] == '\\') {
64 memcpy(&t
[j
], "\\x5c", 4);
76 size_t path_decode(char *s
)
80 for (i
= 0, j
= 0; s
[i
] != '\0'; j
++) {
81 if (memcmp(&s
[i
], "\\x2f", 4) == 0) {
84 }else if (memcmp(&s
[i
], "\\x5c", 4) == 0) {
96 /* count of characters used to encode one unicode char */
97 static int utf8_encoded_expected_len(const char *str
)
99 unsigned char c
= (unsigned char)str
[0];
103 if ((c
& 0xe0) == 0xc0)
105 if ((c
& 0xf0) == 0xe0)
107 if ((c
& 0xf8) == 0xf0)
109 if ((c
& 0xfc) == 0xf8)
111 if ((c
& 0xfe) == 0xfc)
116 /* decode one unicode char */
117 static int utf8_encoded_to_unichar(const char *str
)
123 len
= utf8_encoded_expected_len(str
);
128 unichar
= str
[0] & 0x1f;
131 unichar
= (int)str
[0] & 0x0f;
134 unichar
= (int)str
[0] & 0x07;
137 unichar
= (int)str
[0] & 0x03;
140 unichar
= (int)str
[0] & 0x01;
146 for (i
= 1; i
< len
; i
++) {
147 if (((int)str
[i
] & 0xc0) != 0x80)
150 unichar
|= (int)str
[i
] & 0x3f;
156 /* expected size used to encode one unicode char */
157 static int utf8_unichar_to_encoded_len(int unichar
)
163 if (unichar
< 0x10000)
165 if (unichar
< 0x200000)
167 if (unichar
< 0x4000000)
172 /* check if unicode char has a valid numeric range */
173 static int utf8_unichar_valid_range(int unichar
)
175 if (unichar
> 0x10ffff)
177 if ((unichar
& 0xfffff800) == 0xd800)
179 if ((unichar
> 0xfdcf) && (unichar
< 0xfdf0))
181 if ((unichar
& 0xffff) == 0xffff)
186 /* validate one encoded unicode char and return its length */
187 int utf8_encoded_valid_unichar(const char *str
)
193 len
= utf8_encoded_expected_len(str
);
201 /* check if expected encoded chars are available */
202 for (i
= 0; i
< len
; i
++)
203 if ((str
[i
] & 0x80) != 0x80)
206 unichar
= utf8_encoded_to_unichar(str
);
208 /* check if encoded length matches encoded value */
209 if (utf8_unichar_to_encoded_len(unichar
) != len
)
212 /* check if value has valid range */
213 if (!utf8_unichar_valid_range(unichar
))
219 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
220 int replace_chars(char *str
, const char *white
)
225 while (str
[i
] != '\0') {
228 /* accept whitelist */
229 if (white
!= NULL
&& strchr(white
, str
[i
]) != NULL
) {
234 /* accept plain ascii char */
235 if ((str
[i
] >= '0' && str
[i
] <= '9') ||
236 (str
[i
] >= 'A' && str
[i
] <= 'Z') ||
237 (str
[i
] >= 'a' && str
[i
] <= 'z')) {
242 /* accept hex encoding */
243 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
248 /* accept valid utf8 */
249 len
= utf8_encoded_valid_unichar(&str
[i
]);
255 /* if space is allowed, replace whitespace with ordinary space */
256 if (isspace(str
[i
]) && strchr(white
, ' ') != NULL
) {
263 /* everything else is replaced with '_' */