/src/util-linux/libblkid/src/encode.c
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | /* |
3 | | * encode.c - string conversion routines (mostly for compatibility with |
4 | | * udev/volume_id) |
5 | | * |
6 | | * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org> |
7 | | * Copyright (C) 2009 Karel Zak <kzak@redhat.com> |
8 | | * |
9 | | * This file may be redistributed under the terms of the |
10 | | * GNU Lesser General Public License. |
11 | | */ |
12 | | #include <stdio.h> |
13 | | #include <stdlib.h> |
14 | | #include <stddef.h> |
15 | | #include <unistd.h> |
16 | | #include <errno.h> |
17 | | #include <string.h> |
18 | | #include <ctype.h> |
19 | | |
20 | | #include "blkidP.h" |
21 | | #include "strutils.h" |
22 | | |
23 | | /** |
24 | | * SECTION: encode |
25 | | * @title: Encoding utils |
26 | | * @short_description: encode strings to safe udev-compatible formats |
27 | | * |
28 | | */ |
29 | | |
30 | | /* count of characters used to encode one unicode char */ |
31 | | static int utf8_encoded_expected_len(const char *str) |
32 | 1.62k | { |
33 | 1.62k | unsigned char c = (unsigned char)str[0]; |
34 | | |
35 | 1.62k | if (c < 0x80) |
36 | 1.62k | return 1; |
37 | 0 | if ((c & 0xe0) == 0xc0) |
38 | 0 | return 2; |
39 | 0 | if ((c & 0xf0) == 0xe0) |
40 | 0 | return 3; |
41 | 0 | if ((c & 0xf8) == 0xf0) |
42 | 0 | return 4; |
43 | 0 | if ((c & 0xfc) == 0xf8) |
44 | 0 | return 5; |
45 | 0 | if ((c & 0xfe) == 0xfc) |
46 | 0 | return 6; |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | | /* decode one unicode char */ |
51 | | static int utf8_encoded_to_unichar(const char *str) |
52 | 0 | { |
53 | 0 | int unichar; |
54 | 0 | int len; |
55 | 0 | int i; |
56 | |
|
57 | 0 | len = utf8_encoded_expected_len(str); |
58 | 0 | switch (len) { |
59 | 0 | case 1: |
60 | 0 | return (int)str[0]; |
61 | 0 | case 2: |
62 | 0 | unichar = str[0] & 0x1f; |
63 | 0 | break; |
64 | 0 | case 3: |
65 | 0 | unichar = (int)str[0] & 0x0f; |
66 | 0 | break; |
67 | 0 | case 4: |
68 | 0 | unichar = (int)str[0] & 0x07; |
69 | 0 | break; |
70 | 0 | case 5: |
71 | 0 | unichar = (int)str[0] & 0x03; |
72 | 0 | break; |
73 | 0 | case 6: |
74 | 0 | unichar = (int)str[0] & 0x01; |
75 | 0 | break; |
76 | 0 | default: |
77 | 0 | return -1; |
78 | 0 | } |
79 | | |
80 | 0 | for (i = 1; i < len; i++) { |
81 | 0 | if (((int)str[i] & 0xc0) != 0x80) |
82 | 0 | return -1; |
83 | 0 | unichar <<= 6; |
84 | 0 | unichar |= (int)str[i] & 0x3f; |
85 | 0 | } |
86 | | |
87 | 0 | return unichar; |
88 | 0 | } |
89 | | |
90 | | /* expected size used to encode one unicode char */ |
91 | | static int utf8_unichar_to_encoded_len(int unichar) |
92 | 0 | { |
93 | 0 | if (unichar < 0x80) |
94 | 0 | return 1; |
95 | 0 | if (unichar < 0x800) |
96 | 0 | return 2; |
97 | 0 | if (unichar < 0x10000) |
98 | 0 | return 3; |
99 | 0 | if (unichar < 0x200000) |
100 | 0 | return 4; |
101 | 0 | if (unichar < 0x4000000) |
102 | 0 | return 5; |
103 | 0 | return 6; |
104 | 0 | } |
105 | | |
106 | | /* check if unicode char has a valid numeric range */ |
107 | | static int utf8_unichar_valid_range(int unichar) |
108 | 0 | { |
109 | 0 | if (unichar > 0x10ffff) |
110 | 0 | return 0; |
111 | 0 | if ((unichar & 0xfffff800) == 0xd800) |
112 | 0 | return 0; |
113 | 0 | if ((unichar > 0xfdcf) && (unichar < 0xfdf0)) |
114 | 0 | return 0; |
115 | 0 | if ((unichar & 0xffff) == 0xffff) |
116 | 0 | return 0; |
117 | 0 | return 1; |
118 | 0 | } |
119 | | |
120 | | /* validate one encoded unicode char and return its length */ |
121 | | static int utf8_encoded_valid_unichar(const char *str) |
122 | 1.62k | { |
123 | 1.62k | int len; |
124 | 1.62k | int unichar; |
125 | 1.62k | int i; |
126 | | |
127 | 1.62k | len = utf8_encoded_expected_len(str); |
128 | 1.62k | if (len == 0) |
129 | 0 | return -1; |
130 | | |
131 | | /* ascii is valid */ |
132 | 1.62k | if (len == 1) |
133 | 1.62k | return 1; |
134 | | |
135 | | /* check if expected encoded chars are available */ |
136 | 0 | for (i = 0; i < len; i++) |
137 | 0 | if ((str[i] & 0x80) != 0x80) |
138 | 0 | return -1; |
139 | | |
140 | 0 | unichar = utf8_encoded_to_unichar(str); |
141 | | |
142 | | /* check if encoded length matches encoded value */ |
143 | 0 | if (utf8_unichar_to_encoded_len(unichar) != len) |
144 | 0 | return -1; |
145 | | |
146 | | /* check if value has valid range */ |
147 | 0 | if (!utf8_unichar_valid_range(unichar)) |
148 | 0 | return -1; |
149 | | |
150 | 0 | return len; |
151 | 0 | } |
152 | | |
153 | | static int is_whitelisted(char c, const char *white) |
154 | 1.62k | { |
155 | 1.62k | if ((c >= '0' && c <= '9') || |
156 | 1.62k | (c >= 'A' && c <= 'Z') || |
157 | 1.62k | (c >= 'a' && c <= 'z') || |
158 | 1.62k | strchr("#+-.:=@_", c) != NULL || |
159 | 1.62k | (white != NULL && strchr(white, c) != NULL)) |
160 | 1.62k | return 1; |
161 | 0 | return 0; |
162 | 1.62k | } |
163 | | |
164 | | /** |
165 | | * blkid_encode_string: |
166 | | * @str: input string to be encoded |
167 | | * @str_enc: output string to store the encoded input string |
168 | | * @len: maximum size of the output string, which may be |
169 | | * four times as long as the input string |
170 | | * |
171 | | * Encode all potentially unsafe characters of a string to the |
172 | | * corresponding hex value prefixed by '\x'. |
173 | | * |
174 | | * Returns: 0 if the entire string was copied, non-zero otherwise. |
175 | | **/ |
176 | | int blkid_encode_string(const char *str, char *str_enc, size_t len) |
177 | 45 | { |
178 | 45 | size_t i, j; |
179 | | |
180 | 45 | if (!str || !str_enc || !len) |
181 | 0 | return -1; |
182 | | |
183 | 1.66k | for (i = 0, j = 0; str[i] != '\0'; i++) { |
184 | 1.62k | int seqlen; |
185 | | |
186 | 1.62k | seqlen = utf8_encoded_valid_unichar(&str[i]); |
187 | 1.62k | if (seqlen > 1) { |
188 | 0 | if (len-j < (size_t)seqlen) |
189 | 0 | goto err; |
190 | 0 | memcpy(&str_enc[j], &str[i], seqlen); |
191 | 0 | j += seqlen; |
192 | 0 | i += (seqlen-1); |
193 | 1.62k | } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) { |
194 | 0 | if (len-j < 4) |
195 | 0 | goto err; |
196 | 0 | sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]); |
197 | 0 | j += 4; |
198 | 1.62k | } else { |
199 | 1.62k | if (len-j < 1) |
200 | 0 | goto err; |
201 | 1.62k | str_enc[j] = str[i]; |
202 | 1.62k | j++; |
203 | 1.62k | } |
204 | 1.62k | if (j+3 >= len) |
205 | 0 | goto err; |
206 | 1.62k | } |
207 | 45 | if (len-j < 1) |
208 | 0 | goto err; |
209 | 45 | str_enc[j] = '\0'; |
210 | 45 | return 0; |
211 | 0 | err: |
212 | 0 | return -1; |
213 | 45 | } |
214 | | |
215 | | /** |
216 | | * blkid_safe_string: |
217 | | * @str: input string |
218 | | * @str_safe: output string |
219 | | * @len: size of output string |
220 | | * |
221 | | * Processing whitespace characters. Allows valid ascii,valid utf8. |
222 | | * Replace everything else with'_' |
223 | | * |
224 | | * Returns: 0 on success or -1 in case of error. |
225 | | */ |
226 | | int blkid_safe_string(const char *str, char *str_safe, size_t len) |
227 | 0 | { |
228 | 0 | size_t i = 0; |
229 | |
|
230 | 0 | if (!str || !str_safe || !len) |
231 | 0 | return -1; |
232 | | |
233 | 0 | __normalize_whitespace( |
234 | 0 | (const unsigned char *) str, strnlen(str, len), |
235 | 0 | (unsigned char *) str_safe, len); |
236 | |
|
237 | 0 | while (i < len && str_safe[i] != '\0') { |
238 | 0 | int seqsz; |
239 | | |
240 | | /* accept ASCII from ' ' to '~' */ |
241 | 0 | if (str_safe[i] > 0x20 && str_safe[i] <= 0x7E) |
242 | 0 | i++; |
243 | | |
244 | | /* accept hex encoding */ |
245 | 0 | else if (str_safe[i] == '\\' && str_safe[i+1] == 'x') |
246 | 0 | i += 2; |
247 | | |
248 | | /* replace whitespace */ |
249 | 0 | else if (isspace(str_safe[i])) |
250 | 0 | str_safe[i++] = '_'; |
251 | | |
252 | | /* accept valid utf8 */ |
253 | 0 | else if ((seqsz = utf8_encoded_valid_unichar(&str_safe[i])) >= 1) |
254 | 0 | i += seqsz; |
255 | | |
256 | | /* everything else is replaced with '_' */ |
257 | 0 | else |
258 | 0 | str_safe[i++] = '_'; |
259 | 0 | } |
260 | |
|
261 | 0 | str_safe[len - 1] = '\0'; |
262 | 0 | return 0; |
263 | 0 | } |