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