chiark / gitweb /
man: correct udevinfo --export-db
[elogind.git] / 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 <fcntl.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <syslog.h>
29 #include <sys/utsname.h>
30
31 #include "udev.h"
32
33 int string_is_true(const char *str)
34 {
35         if (strcasecmp(str, "true") == 0)
36                 return 1;
37         if (strcasecmp(str, "yes") == 0)
38                 return 1;
39         if (strcasecmp(str, "1") == 0)
40                 return 1;
41         return 0;
42 }
43
44 void remove_trailing_chars(char *path, char c)
45 {
46         size_t len;
47
48         len = strlen(path);
49         while (len > 0 && path[len-1] == c)
50                 path[--len] = '\0';
51 }
52
53 /* count of characters used to encode one unicode char */
54 static int utf8_encoded_expected_len(const char *str)
55 {
56         unsigned char c = (unsigned char)str[0];
57
58         if (c < 0x80)
59                 return 1;
60         if ((c & 0xe0) == 0xc0)
61                 return 2;
62         if ((c & 0xf0) == 0xe0)
63                 return 3;
64         if ((c & 0xf8) == 0xf0)
65                 return 4;
66         if ((c & 0xfc) == 0xf8)
67                 return 5;
68         if ((c & 0xfe) == 0xfc)
69                 return 6;
70         return 0;
71 }
72
73 /* decode one unicode char */
74 static int utf8_encoded_to_unichar(const char *str)
75 {
76         int unichar;
77         int len;
78         int i;
79
80         len = utf8_encoded_expected_len(str);
81         switch (len) {
82         case 1:
83                 return (int)str[0];
84         case 2:
85                 unichar = str[0] & 0x1f;
86                 break;
87         case 3:
88                 unichar = (int)str[0] & 0x0f;
89                 break;
90         case 4:
91                 unichar = (int)str[0] & 0x07;
92                 break;
93         case 5:
94                 unichar = (int)str[0] & 0x03;
95                 break;
96         case 6:
97                 unichar = (int)str[0] & 0x01;
98                 break;
99         default:
100                 return -1;
101         }
102
103         for (i = 1; i < len; i++) {
104                 if (((int)str[i] & 0xc0) != 0x80)
105                         return -1;
106                 unichar <<= 6;
107                 unichar |= (int)str[i] & 0x3f;
108         }
109
110         return unichar;
111 }
112
113 /* expected size used to encode one unicode char */
114 static int utf8_unichar_to_encoded_len(int unichar)
115 {
116         if (unichar < 0x80)
117                 return 1;
118         if (unichar < 0x800)
119                 return 2;
120         if (unichar < 0x10000)
121                 return 3;
122         if (unichar < 0x200000)
123                 return 4;
124         if (unichar < 0x4000000)
125                 return 5;
126         return 6;
127 }
128
129 /* check if unicode char has a valid numeric range */
130 static int utf8_unichar_valid_range(int unichar)
131 {
132         if (unichar > 0x10ffff)
133                 return 0;
134         if ((unichar & 0xfffff800) == 0xd800)
135                 return 0;
136         if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
137                 return 0;
138         if ((unichar & 0xffff) == 0xffff)
139                 return 0;
140         return 1;
141 }
142
143 /* validate one encoded unicode char and return its length */
144 int utf8_encoded_valid_unichar(const char *str)
145 {
146         int len;
147         int unichar;
148         int i;
149
150         len = utf8_encoded_expected_len(str);
151         if (len == 0)
152                 return -1;
153
154         /* ascii is valid */
155         if (len == 1)
156                 return 1;
157
158         /* check if expected encoded chars are available */
159         for (i = 0; i < len; i++)
160                 if ((str[i] & 0x80) != 0x80)
161                         return -1;
162
163         unichar = utf8_encoded_to_unichar(str);
164
165         /* check if encoded length matches encoded value */
166         if (utf8_unichar_to_encoded_len(unichar) != len)
167                 return -1;
168
169         /* check if value has valid range */
170         if (!utf8_unichar_valid_range(unichar))
171                 return -1;
172
173         return len;
174 }
175
176 /* replace everything but whitelisted plain ascii and valid utf8 */
177 int replace_untrusted_chars(char *str)
178 {
179         size_t i = 0;
180         int replaced = 0;
181
182         while (str[i] != '\0') {
183                 int len;
184
185                 /* valid printable ascii char */
186                 if ((str[i] >= '0' && str[i] <= '9') ||
187                     (str[i] >= 'A' && str[i] <= 'Z') ||
188                     (str[i] >= 'a' && str[i] <= 'z') ||
189                     strchr(" #$%+-./:=?@_,", str[i])) {
190                         i++;
191                         continue;
192                 }
193                 /* valid utf8 is accepted */
194                 len = utf8_encoded_valid_unichar(&str[i]);
195                 if (len > 1) {
196                         i += len;
197                         continue;
198                 }
199
200                 /* everything else is garbage */
201                 str[i] = '_';
202                 i++;
203                 replaced++;
204         }
205
206         return replaced;
207 }