]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_utils_string.c
replace libsysfs
[thirdparty/systemd.git] / udev_utils_string.c
1 /*
2 * udev_utils_string.c - string manipulation
3 *
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5 *
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.
9 *
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.
14 *
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.
18 *
19 */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <syslog.h>
31 #include <sys/utsname.h>
32
33 #include "udev.h"
34
35 /* compare string with pattern (like fnmatch(), supports * ? [0-9] [!A-Z]) */
36 int strcmp_pattern(const char *p, const char *s)
37 {
38 if (s[0] == '\0') {
39 while (p[0] == '*')
40 p++;
41 return (p[0] != '\0');
42 }
43 switch (p[0]) {
44 case '[':
45 {
46 int not = 0;
47 p++;
48 if (p[0] == '!') {
49 not = 1;
50 p++;
51 }
52 while ((p[0] != '\0') && (p[0] != ']')) {
53 int match = 0;
54 if (p[1] == '-') {
55 if ((s[0] >= p[0]) && (s[0] <= p[2]))
56 match = 1;
57 p += 3;
58 } else {
59 match = (p[0] == s[0]);
60 p++;
61 }
62 if (match ^ not) {
63 while ((p[0] != '\0') && (p[0] != ']'))
64 p++;
65 if (p[0] == ']')
66 return strcmp_pattern(p+1, s+1);
67 }
68 }
69 }
70 break;
71 case '*':
72 if (strcmp_pattern(p, s+1))
73 return strcmp_pattern(p+1, s);
74 return 0;
75 case '\0':
76 if (s[0] == '\0') {
77 return 0;
78 }
79 break;
80 default:
81 if ((p[0] == s[0]) || (p[0] == '?'))
82 return strcmp_pattern(p+1, s+1);
83 break;
84 }
85 return 1;
86 }
87
88 int string_is_true(const char *str)
89 {
90 if (strcasecmp(str, "true") == 0)
91 return 1;
92 if (strcasecmp(str, "yes") == 0)
93 return 1;
94 if (strcasecmp(str, "1") == 0)
95 return 1;
96 return 0;
97 }
98
99 void remove_trailing_chars(char *path, char c)
100 {
101 size_t len;
102
103 len = strlen(path);
104 while (len > 0 && path[len-1] == c)
105 path[--len] = '\0';
106 }
107
108 /* count of characters used to encode one unicode char */
109 static int utf8_encoded_expected_len(const char *str)
110 {
111 unsigned char c = (unsigned char)str[0];
112
113 if (c < 0x80)
114 return 1;
115 if ((c & 0xe0) == 0xc0)
116 return 2;
117 if ((c & 0xf0) == 0xe0)
118 return 3;
119 if ((c & 0xf8) == 0xf0)
120 return 4;
121 if ((c & 0xfc) == 0xf8)
122 return 5;
123 if ((c & 0xfe) == 0xfc)
124 return 6;
125 return 0;
126 }
127
128 /* decode one unicode char */
129 static int utf8_encoded_to_unichar(const char *str)
130 {
131 int unichar;
132 int len;
133 int i;
134
135 len = utf8_encoded_expected_len(str);
136 switch (len) {
137 case 1:
138 return (int)str[0];
139 case 2:
140 unichar = str[0] & 0x1f;
141 break;
142 case 3:
143 unichar = (int)str[0] & 0x0f;
144 break;
145 case 4:
146 unichar = (int)str[0] & 0x07;
147 break;
148 case 5:
149 unichar = (int)str[0] & 0x03;
150 break;
151 case 6:
152 unichar = (int)str[0] & 0x01;
153 break;
154 default:
155 return -1;
156 }
157
158 for (i = 1; i < len; i++) {
159 if (((int)str[i] & 0xc0) != 0x80)
160 return -1;
161 unichar <<= 6;
162 unichar |= (int)str[i] & 0x3f;
163 }
164
165 return unichar;
166 }
167
168 /* expected size used to encode one unicode char */
169 static int utf8_unichar_to_encoded_len(int unichar)
170 {
171 if (unichar < 0x80)
172 return 1;
173 if (unichar < 0x800)
174 return 2;
175 if (unichar < 0x10000)
176 return 3;
177 if (unichar < 0x200000)
178 return 4;
179 if (unichar < 0x4000000)
180 return 5;
181 return 6;
182 }
183
184 /* check if unicode char has a valid numeric range */
185 static int utf8_unichar_valid_range(int unichar)
186 {
187 if (unichar > 0x10ffff)
188 return 0;
189 if ((unichar & 0xfffff800) == 0xd800)
190 return 0;
191 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
192 return 0;
193 if ((unichar & 0xffff) == 0xffff)
194 return 0;
195 return 1;
196 }
197
198 /* validate one encoded unicode char and return its length */
199 int utf8_encoded_valid_unichar(const char *str)
200 {
201 int len;
202 int unichar;
203 int i;
204
205 len = utf8_encoded_expected_len(str);
206 if (len == 0)
207 return -1;
208
209 /* ascii is valid */
210 if (len == 1)
211 return 1;
212
213 /* check if expected encoded chars are available */
214 for (i = 0; i < len; i++)
215 if ((str[i] & 0x80) != 0x80)
216 return -1;
217
218 unichar = utf8_encoded_to_unichar(str);
219
220 /* check if encoded length matches encoded value */
221 if (utf8_unichar_to_encoded_len(unichar) != len)
222 return -1;
223
224 /* check if value has valid range */
225 if (!utf8_unichar_valid_range(unichar))
226 return -1;
227
228 return len;
229 }
230
231 /* replace everything but whitelisted plain ascii and valid utf8 */
232 int replace_untrusted_chars(char *str)
233 {
234 size_t i = 0;
235 int replaced = 0;
236
237 while (str[i] != '\0') {
238 int len;
239
240 /* valid printable ascii char */
241 if ((str[i] >= '0' && str[i] <= '9') ||
242 (str[i] >= 'A' && str[i] <= 'Z') ||
243 (str[i] >= 'a' && str[i] <= 'z') ||
244 strchr(" #$%+-./:=?@_", str[i])) {
245 i++;
246 continue;
247 }
248 /* valid utf8 is accepted */
249 len = utf8_encoded_valid_unichar(&str[i]);
250 if (len > 1) {
251 i += len;
252 continue;
253 }
254
255 /* everything else is garbage */
256 str[i] = '_';
257 i++;
258 replaced++;
259 }
260
261 return replaced;
262 }