]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7f110ff9 | 2 | |
036ae95a | 3 | /* Parts of this file are based on the GLIB utf8 validation functions. The |
7f110ff9 LP |
4 | * original license text follows. */ |
5 | ||
6 | /* gutf8.c - Operations on UTF-8 strings. | |
7 | * | |
8 | * Copyright (C) 1999 Tom Tromey | |
9 | * Copyright (C) 2000 Red Hat, Inc. | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
23757887 | 12 | * modify it under the terms of the GNU Library General Public |
7f110ff9 LP |
13 | * License as published by the Free Software Foundation; either |
14 | * version 2 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23757887 SK |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | * Library General Public License for more details. | |
7f110ff9 | 20 | * |
23757887 SK |
21 | * You should have received a copy of the GNU Library General Public |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
7f110ff9 LP |
24 | */ |
25 | ||
26 | #include <errno.h> | |
7f110ff9 | 27 | #include <stdbool.h> |
cf0fbc49 TA |
28 | #include <stdlib.h> |
29 | #include <string.h> | |
7f110ff9 | 30 | |
b5efdb8a | 31 | #include "alloc-util.h" |
3f536d5b | 32 | #include "gunicode.h" |
e4e73a63 | 33 | #include "hexdecoct.h" |
11c3a366 | 34 | #include "macro.h" |
7f110ff9 LP |
35 | #include "utf8.h" |
36 | ||
c932fb71 | 37 | bool unichar_is_valid(char32_t ch) { |
7f110ff9 LP |
38 | |
39 | if (ch >= 0x110000) /* End of unicode space */ | |
40 | return false; | |
41 | if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */ | |
42 | return false; | |
43 | if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */ | |
44 | return false; | |
45 | if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */ | |
46 | return false; | |
47 | ||
48 | return true; | |
49 | } | |
50 | ||
c932fb71 | 51 | static bool unichar_is_control(char32_t ch) { |
ba961854 ZJS |
52 | |
53 | /* | |
54 | 0 to ' '-1 is the C0 range. | |
55 | DEL=0x7F, and DEL+1 to 0x9F is C1 range. | |
56 | '\t' is in C0 range, but more or less harmless and commonly used. | |
57 | */ | |
58 | ||
4c701096 | 59 | return (ch < ' ' && !IN_SET(ch, '\t', '\n')) || |
ba961854 ZJS |
60 | (0x7F <= ch && ch <= 0x9F); |
61 | } | |
62 | ||
7991ac34 DR |
63 | /* count of characters used to encode one unicode char */ |
64 | static int utf8_encoded_expected_len(const char *str) { | |
7e8185ef | 65 | unsigned char c; |
ba961854 | 66 | |
7e8185ef LP |
67 | assert(str); |
68 | ||
69 | c = (unsigned char) str[0]; | |
7991ac34 DR |
70 | if (c < 0x80) |
71 | return 1; | |
72 | if ((c & 0xe0) == 0xc0) | |
73 | return 2; | |
74 | if ((c & 0xf0) == 0xe0) | |
75 | return 3; | |
76 | if ((c & 0xf8) == 0xf0) | |
77 | return 4; | |
78 | if ((c & 0xfc) == 0xf8) | |
79 | return 5; | |
80 | if ((c & 0xfe) == 0xfc) | |
81 | return 6; | |
7e8185ef | 82 | |
7991ac34 DR |
83 | return 0; |
84 | } | |
ba961854 | 85 | |
7991ac34 | 86 | /* decode one unicode char */ |
c932fb71 SL |
87 | int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) { |
88 | char32_t unichar; | |
89 | int len, i; | |
7e8185ef LP |
90 | |
91 | assert(str); | |
ba961854 | 92 | |
7991ac34 | 93 | len = utf8_encoded_expected_len(str); |
7e8185ef | 94 | |
7991ac34 DR |
95 | switch (len) { |
96 | case 1: | |
c932fb71 SL |
97 | *ret_unichar = (char32_t)str[0]; |
98 | return 0; | |
7991ac34 DR |
99 | case 2: |
100 | unichar = str[0] & 0x1f; | |
101 | break; | |
102 | case 3: | |
c932fb71 | 103 | unichar = (char32_t)str[0] & 0x0f; |
7991ac34 DR |
104 | break; |
105 | case 4: | |
c932fb71 | 106 | unichar = (char32_t)str[0] & 0x07; |
7991ac34 DR |
107 | break; |
108 | case 5: | |
c932fb71 | 109 | unichar = (char32_t)str[0] & 0x03; |
7991ac34 DR |
110 | break; |
111 | case 6: | |
c932fb71 | 112 | unichar = (char32_t)str[0] & 0x01; |
7991ac34 DR |
113 | break; |
114 | default: | |
7e8185ef | 115 | return -EINVAL; |
ba961854 ZJS |
116 | } |
117 | ||
7991ac34 | 118 | for (i = 1; i < len; i++) { |
c932fb71 | 119 | if (((char32_t)str[i] & 0xc0) != 0x80) |
7e8185ef | 120 | return -EINVAL; |
7991ac34 | 121 | unichar <<= 6; |
c932fb71 | 122 | unichar |= (char32_t)str[i] & 0x3f; |
7991ac34 DR |
123 | } |
124 | ||
c932fb71 SL |
125 | *ret_unichar = unichar; |
126 | ||
127 | return 0; | |
ba961854 ZJS |
128 | } |
129 | ||
0ade5ffe | 130 | bool utf8_is_printable_newline(const char* str, size_t length, bool newline) { |
6ed62be0 | 131 | const char *p; |
7f110ff9 LP |
132 | |
133 | assert(str); | |
134 | ||
6ed62be0 | 135 | for (p = str; length;) { |
c932fb71 SL |
136 | int encoded_len, r; |
137 | char32_t val; | |
7e8185ef | 138 | |
6ed62be0 | 139 | encoded_len = utf8_encoded_valid_unichar(p); |
7e8185ef | 140 | if (encoded_len < 0 || |
144b3d9e LP |
141 | (size_t) encoded_len > length) |
142 | return false; | |
143 | ||
c932fb71 SL |
144 | r = utf8_encoded_to_unichar(p, &val); |
145 | if (r < 0 || | |
f3ee6297 | 146 | unichar_is_control(val) || |
0ade5ffe | 147 | (!newline && val == '\n')) |
7991ac34 | 148 | return false; |
7f110ff9 | 149 | |
7991ac34 | 150 | length -= encoded_len; |
a7176505 | 151 | p += encoded_len; |
7f110ff9 LP |
152 | } |
153 | ||
7991ac34 | 154 | return true; |
7f110ff9 LP |
155 | } |
156 | ||
7991ac34 DR |
157 | const char *utf8_is_valid(const char *str) { |
158 | const uint8_t *p; | |
7f110ff9 LP |
159 | |
160 | assert(str); | |
161 | ||
7991ac34 | 162 | for (p = (const uint8_t*) str; *p; ) { |
faaa5728 LP |
163 | int len; |
164 | ||
165 | len = utf8_encoded_valid_unichar((const char *)p); | |
7991ac34 DR |
166 | if (len < 0) |
167 | return NULL; | |
168 | ||
169 | p += len; | |
170 | } | |
7f110ff9 | 171 | |
7991ac34 | 172 | return str; |
7f110ff9 LP |
173 | } |
174 | ||
550a40ec ZJS |
175 | char *utf8_escape_invalid(const char *str) { |
176 | char *p, *s; | |
177 | ||
178 | assert(str); | |
179 | ||
180 | p = s = malloc(strlen(str) * 4 + 1); | |
181 | if (!p) | |
182 | return NULL; | |
183 | ||
184 | while (*str) { | |
185 | int len; | |
186 | ||
187 | len = utf8_encoded_valid_unichar(str); | |
188 | if (len > 0) { | |
189 | s = mempcpy(s, str, len); | |
190 | str += len; | |
191 | } else { | |
3c6d3052 | 192 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
550a40ec ZJS |
193 | str += 1; |
194 | } | |
195 | } | |
7e8185ef | 196 | |
550a40ec ZJS |
197 | *s = '\0'; |
198 | ||
199 | return p; | |
200 | } | |
201 | ||
fec84576 WC |
202 | char *utf8_escape_non_printable(const char *str) { |
203 | char *p, *s; | |
204 | ||
205 | assert(str); | |
206 | ||
207 | p = s = malloc(strlen(str) * 4 + 1); | |
208 | if (!p) | |
209 | return NULL; | |
210 | ||
211 | while (*str) { | |
212 | int len; | |
213 | ||
214 | len = utf8_encoded_valid_unichar(str); | |
215 | if (len > 0) { | |
216 | if (utf8_is_printable(str, len)) { | |
217 | s = mempcpy(s, str, len); | |
218 | str += len; | |
219 | } else { | |
3c6d3052 | 220 | while (len > 0) { |
fec84576 WC |
221 | *(s++) = '\\'; |
222 | *(s++) = 'x'; | |
223 | *(s++) = hexchar((int) *str >> 4); | |
224 | *(s++) = hexchar((int) *str); | |
fec84576 | 225 | |
3c6d3052 | 226 | str += 1; |
313cefa1 | 227 | len--; |
3c6d3052 | 228 | } |
fec84576 WC |
229 | } |
230 | } else { | |
3c6d3052 | 231 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
fec84576 WC |
232 | str += 1; |
233 | } | |
234 | } | |
235 | ||
236 | *s = '\0'; | |
237 | ||
238 | return p; | |
239 | } | |
240 | ||
7f110ff9 LP |
241 | char *ascii_is_valid(const char *str) { |
242 | const char *p; | |
243 | ||
294a3121 ZJS |
244 | /* Check whether the string consists of valid ASCII bytes, |
245 | * i.e values between 0 and 127, inclusive. */ | |
246 | ||
7f110ff9 LP |
247 | assert(str); |
248 | ||
249 | for (p = str; *p; p++) | |
250 | if ((unsigned char) *p >= 128) | |
251 | return NULL; | |
252 | ||
253 | return (char*) str; | |
254 | } | |
255 | ||
294a3121 ZJS |
256 | char *ascii_is_valid_n(const char *str, size_t len) { |
257 | size_t i; | |
258 | ||
259 | /* Very similar to ascii_is_valid(), but checks exactly len | |
260 | * bytes and rejects any NULs in that range. */ | |
261 | ||
262 | assert(str); | |
263 | ||
264 | for (i = 0; i < len; i++) | |
265 | if ((unsigned char) str[i] >= 128 || str[i] == 0) | |
266 | return NULL; | |
267 | ||
268 | return (char*) str; | |
269 | } | |
270 | ||
2bb4c7e3 TG |
271 | /** |
272 | * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8 | |
273 | * @out_utf8: output buffer of at least 4 bytes or NULL | |
274 | * @g: UCS-4 character to encode | |
275 | * | |
276 | * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8. | |
277 | * The length of the character is returned. It is not zero-terminated! If the | |
278 | * output buffer is NULL, only the length is returned. | |
279 | * | |
280 | * Returns: The length in bytes that the UTF-8 representation does or would | |
281 | * occupy. | |
282 | */ | |
c932fb71 | 283 | size_t utf8_encode_unichar(char *out_utf8, char32_t g) { |
f3ee6297 | 284 | |
2bb4c7e3 TG |
285 | if (g < (1 << 7)) { |
286 | if (out_utf8) | |
287 | out_utf8[0] = g & 0x7f; | |
e7eebcfc | 288 | return 1; |
2bb4c7e3 TG |
289 | } else if (g < (1 << 11)) { |
290 | if (out_utf8) { | |
291 | out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f); | |
292 | out_utf8[1] = 0x80 | (g & 0x3f); | |
293 | } | |
e7eebcfc | 294 | return 2; |
2bb4c7e3 TG |
295 | } else if (g < (1 << 16)) { |
296 | if (out_utf8) { | |
297 | out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f); | |
298 | out_utf8[1] = 0x80 | ((g >> 6) & 0x3f); | |
299 | out_utf8[2] = 0x80 | (g & 0x3f); | |
300 | } | |
e7eebcfc | 301 | return 3; |
2bb4c7e3 TG |
302 | } else if (g < (1 << 21)) { |
303 | if (out_utf8) { | |
304 | out_utf8[0] = 0xf0 | ((g >> 18) & 0x07); | |
305 | out_utf8[1] = 0x80 | ((g >> 12) & 0x3f); | |
306 | out_utf8[2] = 0x80 | ((g >> 6) & 0x3f); | |
307 | out_utf8[3] = 0x80 | (g & 0x3f); | |
308 | } | |
309 | return 4; | |
e7eebcfc | 310 | } |
f3ee6297 LP |
311 | |
312 | return 0; | |
e7eebcfc LP |
313 | } |
314 | ||
2e3d0692 | 315 | char *utf16_to_utf8(const void *s, size_t length) { |
2e3d0692 | 316 | const uint8_t *f; |
e7eebcfc | 317 | char *r, *t; |
2e3d0692 | 318 | |
04166cb7 | 319 | r = new(char, (length * 4 + 1) / 2 + 1); |
2e3d0692 LP |
320 | if (!r) |
321 | return NULL; | |
322 | ||
04166cb7 TG |
323 | f = s; |
324 | t = r; | |
325 | ||
326 | while (f < (const uint8_t*) s + length) { | |
c932fb71 | 327 | char16_t w1, w2; |
04166cb7 TG |
328 | |
329 | /* see RFC 2781 section 2.2 */ | |
330 | ||
331 | w1 = f[1] << 8 | f[0]; | |
332 | f += 2; | |
333 | ||
334 | if (!utf16_is_surrogate(w1)) { | |
dcd12626 | 335 | t += utf8_encode_unichar(t, w1); |
04166cb7 TG |
336 | |
337 | continue; | |
338 | } | |
339 | ||
340 | if (utf16_is_trailing_surrogate(w1)) | |
341 | continue; | |
342 | else if (f >= (const uint8_t*) s + length) | |
343 | break; | |
344 | ||
345 | w2 = f[1] << 8 | f[0]; | |
346 | f += 2; | |
347 | ||
348 | if (!utf16_is_trailing_surrogate(w2)) { | |
349 | f -= 2; | |
350 | continue; | |
351 | } | |
352 | ||
353 | t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2)); | |
354 | } | |
2e3d0692 LP |
355 | |
356 | *t = 0; | |
7b4d7cc0 | 357 | return r; |
2e3d0692 | 358 | } |
02a36bc9 | 359 | |
02a36bc9 | 360 | /* expected size used to encode one unicode char */ |
c932fb71 | 361 | static int utf8_unichar_to_encoded_len(char32_t unichar) { |
7e8185ef | 362 | |
02a36bc9 DR |
363 | if (unichar < 0x80) |
364 | return 1; | |
365 | if (unichar < 0x800) | |
366 | return 2; | |
367 | if (unichar < 0x10000) | |
368 | return 3; | |
369 | if (unichar < 0x200000) | |
370 | return 4; | |
371 | if (unichar < 0x4000000) | |
372 | return 5; | |
7e8185ef | 373 | |
02a36bc9 DR |
374 | return 6; |
375 | } | |
376 | ||
377 | /* validate one encoded unicode char and return its length */ | |
378 | int utf8_encoded_valid_unichar(const char *str) { | |
c932fb71 SL |
379 | int len, i, r; |
380 | char32_t unichar; | |
7e8185ef LP |
381 | |
382 | assert(str); | |
02a36bc9 DR |
383 | |
384 | len = utf8_encoded_expected_len(str); | |
385 | if (len == 0) | |
7e8185ef | 386 | return -EINVAL; |
02a36bc9 DR |
387 | |
388 | /* ascii is valid */ | |
389 | if (len == 1) | |
390 | return 1; | |
391 | ||
392 | /* check if expected encoded chars are available */ | |
393 | for (i = 0; i < len; i++) | |
394 | if ((str[i] & 0x80) != 0x80) | |
7e8185ef | 395 | return -EINVAL; |
02a36bc9 | 396 | |
c932fb71 SL |
397 | r = utf8_encoded_to_unichar(str, &unichar); |
398 | if (r < 0) | |
399 | return r; | |
02a36bc9 DR |
400 | |
401 | /* check if encoded length matches encoded value */ | |
402 | if (utf8_unichar_to_encoded_len(unichar) != len) | |
7e8185ef | 403 | return -EINVAL; |
02a36bc9 DR |
404 | |
405 | /* check if value has valid range */ | |
f3ee6297 | 406 | if (!unichar_is_valid(unichar)) |
7e8185ef | 407 | return -EINVAL; |
02a36bc9 DR |
408 | |
409 | return len; | |
410 | } | |
65ee8660 LP |
411 | |
412 | size_t utf8_n_codepoints(const char *str) { | |
413 | size_t n = 0; | |
414 | ||
415 | /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */ | |
416 | ||
417 | while (*str != 0) { | |
418 | int k; | |
419 | ||
420 | k = utf8_encoded_valid_unichar(str); | |
421 | if (k < 0) | |
422 | return (size_t) -1; | |
423 | ||
424 | str += k; | |
425 | n++; | |
426 | } | |
427 | ||
428 | return n; | |
429 | } | |
3f536d5b LP |
430 | |
431 | size_t utf8_console_width(const char *str) { | |
432 | size_t n = 0; | |
433 | ||
434 | /* Returns the approximate width a string will take on screen when printed on a character cell | |
435 | * terminal/console. */ | |
436 | ||
437 | while (*str != 0) { | |
438 | char32_t c; | |
439 | ||
440 | if (utf8_encoded_to_unichar(str, &c) < 0) | |
441 | return (size_t) -1; | |
442 | ||
443 | str = utf8_next_char(str); | |
444 | ||
445 | n += unichar_iswide(c) ? 2 : 1; | |
446 | } | |
447 | ||
448 | return n; | |
449 | } |