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