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