]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_utils_string.c
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>
33 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
39 /* compare string with pattern (like fnmatch(), supports * ? [0-9] [!A-Z]) */
40 int strcmp_pattern(const char *p
, const char *s
)
45 return (p
[0] != '\0');
56 while ((p
[0] != '\0') && (p
[0] != ']')) {
59 if ((s
[0] >= p
[0]) && (s
[0] <= p
[2]))
63 match
= (p
[0] == s
[0]);
67 while ((p
[0] != '\0') && (p
[0] != ']'))
70 return strcmp_pattern(p
+1, s
+1);
76 if (strcmp_pattern(p
, s
+1))
77 return strcmp_pattern(p
+1, s
);
85 if ((p
[0] == s
[0]) || (p
[0] == '?'))
86 return strcmp_pattern(p
+1, s
+1);
92 int string_is_true(const char *str
)
94 if (strcasecmp(str
, "true") == 0)
96 if (strcasecmp(str
, "yes") == 0)
98 if (strcasecmp(str
, "1") == 0)
103 void remove_trailing_chars(char *path
, char c
)
108 while (len
> 0 && path
[len
-1] == c
)
112 /* count of characters used to encode one unicode char */
113 static int utf8_encoded_expected_len(const char *str
)
115 unsigned char c
= (unsigned char)str
[0];
119 if ((c
& 0xe0) == 0xc0)
121 if ((c
& 0xf0) == 0xe0)
123 if ((c
& 0xf8) == 0xf0)
125 if ((c
& 0xfc) == 0xf8)
127 if ((c
& 0xfe) == 0xfc)
132 /* decode one unicode char */
133 static int utf8_encoded_to_unichar(const char *str
)
139 len
= utf8_encoded_expected_len(str
);
144 unichar
= str
[0] & 0x1f;
147 unichar
= (int)str
[0] & 0x0f;
150 unichar
= (int)str
[0] & 0x07;
153 unichar
= (int)str
[0] & 0x03;
156 unichar
= (int)str
[0] & 0x01;
162 for (i
= 1; i
< len
; i
++) {
163 if (((int)str
[i
] & 0xc0) != 0x80)
166 unichar
|= (int)str
[i
] & 0x3f;
172 /* expected size used to encode one unicode char */
173 static int utf8_unichar_to_encoded_len(int unichar
)
179 if (unichar
< 0x10000)
181 if (unichar
< 0x200000)
183 if (unichar
< 0x4000000)
188 /* check if unicode char has a valid numeric range */
189 static int utf8_unichar_valid_range(int unichar
)
191 if (unichar
> 0x10ffff)
193 if ((unichar
& 0xfffff800) == 0xd800)
195 if ((unichar
> 0xfdcf) && (unichar
< 0xfdf0))
197 if ((unichar
& 0xffff) == 0xffff)
202 /* validate one encoded unicode char and return its length */
203 int utf8_encoded_valid_unichar(const char *str
)
209 len
= utf8_encoded_expected_len(str
);
217 /* check if expected encoded chars are available */
218 for (i
= 0; i
< len
; i
++)
219 if ((str
[i
] & 0x80) != 0x80)
222 unichar
= utf8_encoded_to_unichar(str
);
224 /* check if encoded length matches encoded value */
225 if (utf8_unichar_to_encoded_len(unichar
) != len
)
228 /* check if value has valid range */
229 if (!utf8_unichar_valid_range(unichar
))
235 /* replace everything but whitelisted plain ascii and valid utf8 */
236 int replace_untrusted_chars(char *str
)
241 while (str
[i
] != '\0') {
244 /* valid printable ascii char */
245 if ((str
[i
] >= '0' && str
[i
] <= '9') ||
246 (str
[i
] >= 'A' && str
[i
] <= 'Z') ||
247 (str
[i
] >= 'a' && str
[i
] <= 'z') ||
248 strchr(" #$%+-./:=?@_", str
[i
])) {
252 /* valid utf8 is accepted */
253 len
= utf8_encoded_valid_unichar(&str
[i
]);
259 /* everything else is garbage */