]>
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 | 63 | /* count of characters used to encode one unicode char */ |
7c421857 LP |
64 | static size_t utf8_encoded_expected_len(const char *str) { |
65 | uint8_t c; | |
ba961854 | 66 | |
7e8185ef LP |
67 | assert(str); |
68 | ||
7c421857 | 69 | c = (uint8_t) 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; | |
7c421857 | 89 | size_t 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; |
7c421857 | 121 | |
7991ac34 | 122 | unichar <<= 6; |
c932fb71 | 123 | unichar |= (char32_t)str[i] & 0x3f; |
7991ac34 DR |
124 | } |
125 | ||
c932fb71 SL |
126 | *ret_unichar = unichar; |
127 | ||
128 | return 0; | |
ba961854 ZJS |
129 | } |
130 | ||
0ade5ffe | 131 | bool utf8_is_printable_newline(const char* str, size_t length, bool newline) { |
6ed62be0 | 132 | const char *p; |
7f110ff9 LP |
133 | |
134 | assert(str); | |
135 | ||
6ed62be0 | 136 | for (p = str; length;) { |
c932fb71 SL |
137 | int encoded_len, r; |
138 | char32_t val; | |
7e8185ef | 139 | |
6ed62be0 | 140 | encoded_len = utf8_encoded_valid_unichar(p); |
7e8185ef | 141 | if (encoded_len < 0 || |
144b3d9e LP |
142 | (size_t) encoded_len > length) |
143 | return false; | |
144 | ||
c932fb71 SL |
145 | r = utf8_encoded_to_unichar(p, &val); |
146 | if (r < 0 || | |
f3ee6297 | 147 | unichar_is_control(val) || |
0ade5ffe | 148 | (!newline && val == '\n')) |
7991ac34 | 149 | return false; |
7f110ff9 | 150 | |
7991ac34 | 151 | length -= encoded_len; |
a7176505 | 152 | p += encoded_len; |
7f110ff9 LP |
153 | } |
154 | ||
7991ac34 | 155 | return true; |
7f110ff9 LP |
156 | } |
157 | ||
e71fb4b3 LP |
158 | char *utf8_is_valid(const char *str) { |
159 | const char *p; | |
7f110ff9 LP |
160 | |
161 | assert(str); | |
162 | ||
e71fb4b3 LP |
163 | p = str; |
164 | while (*p) { | |
faaa5728 LP |
165 | int len; |
166 | ||
e71fb4b3 | 167 | len = utf8_encoded_valid_unichar(p); |
7991ac34 DR |
168 | if (len < 0) |
169 | return NULL; | |
170 | ||
171 | p += len; | |
172 | } | |
7f110ff9 | 173 | |
e71fb4b3 | 174 | return (char*) str; |
7f110ff9 LP |
175 | } |
176 | ||
550a40ec ZJS |
177 | char *utf8_escape_invalid(const char *str) { |
178 | char *p, *s; | |
179 | ||
180 | assert(str); | |
181 | ||
182 | p = s = malloc(strlen(str) * 4 + 1); | |
183 | if (!p) | |
184 | return NULL; | |
185 | ||
186 | while (*str) { | |
187 | int len; | |
188 | ||
189 | len = utf8_encoded_valid_unichar(str); | |
190 | if (len > 0) { | |
191 | s = mempcpy(s, str, len); | |
192 | str += len; | |
193 | } else { | |
3c6d3052 | 194 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
550a40ec ZJS |
195 | str += 1; |
196 | } | |
197 | } | |
7e8185ef | 198 | |
550a40ec ZJS |
199 | *s = '\0'; |
200 | ||
201 | return p; | |
202 | } | |
203 | ||
fec84576 WC |
204 | char *utf8_escape_non_printable(const char *str) { |
205 | char *p, *s; | |
206 | ||
207 | assert(str); | |
208 | ||
209 | p = s = malloc(strlen(str) * 4 + 1); | |
210 | if (!p) | |
211 | return NULL; | |
212 | ||
213 | while (*str) { | |
214 | int len; | |
215 | ||
216 | len = utf8_encoded_valid_unichar(str); | |
217 | if (len > 0) { | |
218 | if (utf8_is_printable(str, len)) { | |
219 | s = mempcpy(s, str, len); | |
220 | str += len; | |
221 | } else { | |
3c6d3052 | 222 | while (len > 0) { |
fec84576 WC |
223 | *(s++) = '\\'; |
224 | *(s++) = 'x'; | |
225 | *(s++) = hexchar((int) *str >> 4); | |
226 | *(s++) = hexchar((int) *str); | |
fec84576 | 227 | |
3c6d3052 | 228 | str += 1; |
313cefa1 | 229 | len--; |
3c6d3052 | 230 | } |
fec84576 WC |
231 | } |
232 | } else { | |
3c6d3052 | 233 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
fec84576 WC |
234 | str += 1; |
235 | } | |
236 | } | |
237 | ||
238 | *s = '\0'; | |
239 | ||
240 | return p; | |
241 | } | |
242 | ||
7f110ff9 LP |
243 | char *ascii_is_valid(const char *str) { |
244 | const char *p; | |
245 | ||
294a3121 ZJS |
246 | /* Check whether the string consists of valid ASCII bytes, |
247 | * i.e values between 0 and 127, inclusive. */ | |
248 | ||
7f110ff9 LP |
249 | assert(str); |
250 | ||
251 | for (p = str; *p; p++) | |
252 | if ((unsigned char) *p >= 128) | |
253 | return NULL; | |
254 | ||
255 | return (char*) str; | |
256 | } | |
257 | ||
294a3121 ZJS |
258 | char *ascii_is_valid_n(const char *str, size_t len) { |
259 | size_t i; | |
260 | ||
261 | /* Very similar to ascii_is_valid(), but checks exactly len | |
262 | * bytes and rejects any NULs in that range. */ | |
263 | ||
264 | assert(str); | |
265 | ||
266 | for (i = 0; i < len; i++) | |
267 | if ((unsigned char) str[i] >= 128 || str[i] == 0) | |
268 | return NULL; | |
269 | ||
270 | return (char*) str; | |
271 | } | |
272 | ||
2bb4c7e3 TG |
273 | /** |
274 | * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8 | |
275 | * @out_utf8: output buffer of at least 4 bytes or NULL | |
276 | * @g: UCS-4 character to encode | |
277 | * | |
278 | * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8. | |
279 | * The length of the character is returned. It is not zero-terminated! If the | |
280 | * output buffer is NULL, only the length is returned. | |
281 | * | |
282 | * Returns: The length in bytes that the UTF-8 representation does or would | |
283 | * occupy. | |
284 | */ | |
c932fb71 | 285 | size_t utf8_encode_unichar(char *out_utf8, char32_t g) { |
f3ee6297 | 286 | |
2bb4c7e3 TG |
287 | if (g < (1 << 7)) { |
288 | if (out_utf8) | |
289 | out_utf8[0] = g & 0x7f; | |
e7eebcfc | 290 | return 1; |
2bb4c7e3 TG |
291 | } else if (g < (1 << 11)) { |
292 | if (out_utf8) { | |
293 | out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f); | |
294 | out_utf8[1] = 0x80 | (g & 0x3f); | |
295 | } | |
e7eebcfc | 296 | return 2; |
2bb4c7e3 TG |
297 | } else if (g < (1 << 16)) { |
298 | if (out_utf8) { | |
299 | out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f); | |
300 | out_utf8[1] = 0x80 | ((g >> 6) & 0x3f); | |
301 | out_utf8[2] = 0x80 | (g & 0x3f); | |
302 | } | |
e7eebcfc | 303 | return 3; |
2bb4c7e3 TG |
304 | } else if (g < (1 << 21)) { |
305 | if (out_utf8) { | |
306 | out_utf8[0] = 0xf0 | ((g >> 18) & 0x07); | |
307 | out_utf8[1] = 0x80 | ((g >> 12) & 0x3f); | |
308 | out_utf8[2] = 0x80 | ((g >> 6) & 0x3f); | |
309 | out_utf8[3] = 0x80 | (g & 0x3f); | |
310 | } | |
311 | return 4; | |
e7eebcfc | 312 | } |
f3ee6297 LP |
313 | |
314 | return 0; | |
e7eebcfc LP |
315 | } |
316 | ||
2ac2ff3f | 317 | char *utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) { |
2e3d0692 | 318 | const uint8_t *f; |
e7eebcfc | 319 | char *r, *t; |
2e3d0692 | 320 | |
2ac2ff3f LP |
321 | assert(s); |
322 | ||
323 | /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may | |
324 | * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */ | |
325 | if (length * 2 < length) | |
326 | return NULL; /* overflow */ | |
327 | ||
328 | r = new(char, length * 2 + 1); | |
2e3d0692 LP |
329 | if (!r) |
330 | return NULL; | |
331 | ||
2ac2ff3f | 332 | f = (const uint8_t*) s; |
04166cb7 TG |
333 | t = r; |
334 | ||
2ac2ff3f | 335 | while (f + 1 < (const uint8_t*) s + length) { |
c932fb71 | 336 | char16_t w1, w2; |
04166cb7 TG |
337 | |
338 | /* see RFC 2781 section 2.2 */ | |
339 | ||
340 | w1 = f[1] << 8 | f[0]; | |
341 | f += 2; | |
342 | ||
343 | if (!utf16_is_surrogate(w1)) { | |
dcd12626 | 344 | t += utf8_encode_unichar(t, w1); |
04166cb7 TG |
345 | continue; |
346 | } | |
347 | ||
348 | if (utf16_is_trailing_surrogate(w1)) | |
2ac2ff3f LP |
349 | continue; /* spurious trailing surrogate, ignore */ |
350 | ||
351 | if (f + 1 >= (const uint8_t*) s + length) | |
04166cb7 TG |
352 | break; |
353 | ||
354 | w2 = f[1] << 8 | f[0]; | |
355 | f += 2; | |
356 | ||
357 | if (!utf16_is_trailing_surrogate(w2)) { | |
358 | f -= 2; | |
2ac2ff3f | 359 | continue; /* surrogate missing its trailing surrogate, ignore */ |
04166cb7 TG |
360 | } |
361 | ||
362 | t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2)); | |
363 | } | |
2e3d0692 LP |
364 | |
365 | *t = 0; | |
7b4d7cc0 | 366 | return r; |
2e3d0692 | 367 | } |
02a36bc9 | 368 | |
80b0a597 LP |
369 | size_t utf16_encode_unichar(char16_t *out, char32_t c) { |
370 | ||
371 | /* Note that this encodes as little-endian. */ | |
372 | ||
373 | switch (c) { | |
374 | ||
375 | case 0 ... 0xd7ffU: | |
376 | case 0xe000U ... 0xffffU: | |
377 | out[0] = htole16(c); | |
378 | return 1; | |
379 | ||
380 | case 0x10000U ... 0x10ffffU: | |
381 | c -= 0x10000U; | |
382 | out[0] = htole16((c >> 10) + 0xd800U); | |
383 | out[1] = htole16((c & 0x3ffU) + 0xdc00U); | |
384 | return 2; | |
385 | ||
386 | default: /* A surrogate (invalid) */ | |
387 | return 0; | |
388 | } | |
389 | } | |
390 | ||
391 | char16_t *utf8_to_utf16(const char *s, size_t length) { | |
392 | char16_t *n, *p; | |
393 | size_t i; | |
394 | int r; | |
395 | ||
396 | assert(s); | |
397 | ||
398 | n = new(char16_t, length + 1); | |
399 | if (!n) | |
400 | return NULL; | |
401 | ||
402 | p = n; | |
403 | ||
404 | for (i = 0; i < length;) { | |
405 | char32_t unichar; | |
406 | size_t e; | |
407 | ||
408 | e = utf8_encoded_expected_len(s + i); | |
409 | if (e <= 1) /* Invalid and single byte characters are copied as they are */ | |
410 | goto copy; | |
411 | ||
412 | if (i + e > length) /* sequence longer than input buffer, then copy as-is */ | |
413 | goto copy; | |
414 | ||
415 | r = utf8_encoded_to_unichar(s + i, &unichar); | |
416 | if (r < 0) /* sequence invalid, then copy as-is */ | |
417 | goto copy; | |
418 | ||
419 | p += utf16_encode_unichar(p, unichar); | |
420 | i += e; | |
421 | continue; | |
422 | ||
423 | copy: | |
424 | *(p++) = htole16(s[i++]); | |
425 | } | |
426 | ||
427 | *p = 0; | |
428 | return n; | |
429 | } | |
430 | ||
431 | size_t char16_strlen(const char16_t *s) { | |
432 | size_t n = 0; | |
433 | ||
434 | assert(s); | |
435 | ||
436 | while (*s != 0) | |
437 | n++, s++; | |
438 | ||
439 | return n; | |
440 | } | |
441 | ||
02a36bc9 | 442 | /* expected size used to encode one unicode char */ |
c932fb71 | 443 | static int utf8_unichar_to_encoded_len(char32_t unichar) { |
7e8185ef | 444 | |
02a36bc9 DR |
445 | if (unichar < 0x80) |
446 | return 1; | |
447 | if (unichar < 0x800) | |
448 | return 2; | |
449 | if (unichar < 0x10000) | |
450 | return 3; | |
451 | if (unichar < 0x200000) | |
452 | return 4; | |
453 | if (unichar < 0x4000000) | |
454 | return 5; | |
7e8185ef | 455 | |
02a36bc9 DR |
456 | return 6; |
457 | } | |
458 | ||
459 | /* validate one encoded unicode char and return its length */ | |
460 | int utf8_encoded_valid_unichar(const char *str) { | |
c932fb71 | 461 | char32_t unichar; |
7c421857 LP |
462 | size_t len, i; |
463 | int r; | |
7e8185ef LP |
464 | |
465 | assert(str); | |
02a36bc9 DR |
466 | |
467 | len = utf8_encoded_expected_len(str); | |
468 | if (len == 0) | |
7e8185ef | 469 | return -EINVAL; |
02a36bc9 DR |
470 | |
471 | /* ascii is valid */ | |
472 | if (len == 1) | |
473 | return 1; | |
474 | ||
475 | /* check if expected encoded chars are available */ | |
476 | for (i = 0; i < len; i++) | |
477 | if ((str[i] & 0x80) != 0x80) | |
7e8185ef | 478 | return -EINVAL; |
02a36bc9 | 479 | |
c932fb71 SL |
480 | r = utf8_encoded_to_unichar(str, &unichar); |
481 | if (r < 0) | |
482 | return r; | |
02a36bc9 DR |
483 | |
484 | /* check if encoded length matches encoded value */ | |
7c421857 | 485 | if (utf8_unichar_to_encoded_len(unichar) != (int) len) |
7e8185ef | 486 | return -EINVAL; |
02a36bc9 DR |
487 | |
488 | /* check if value has valid range */ | |
f3ee6297 | 489 | if (!unichar_is_valid(unichar)) |
7e8185ef | 490 | return -EINVAL; |
02a36bc9 | 491 | |
7c421857 | 492 | return (int) len; |
02a36bc9 | 493 | } |
65ee8660 LP |
494 | |
495 | size_t utf8_n_codepoints(const char *str) { | |
496 | size_t n = 0; | |
497 | ||
498 | /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */ | |
499 | ||
500 | while (*str != 0) { | |
501 | int k; | |
502 | ||
503 | k = utf8_encoded_valid_unichar(str); | |
504 | if (k < 0) | |
505 | return (size_t) -1; | |
506 | ||
507 | str += k; | |
508 | n++; | |
509 | } | |
510 | ||
511 | return n; | |
512 | } | |
3f536d5b LP |
513 | |
514 | size_t utf8_console_width(const char *str) { | |
515 | size_t n = 0; | |
516 | ||
517 | /* Returns the approximate width a string will take on screen when printed on a character cell | |
518 | * terminal/console. */ | |
519 | ||
520 | while (*str != 0) { | |
521 | char32_t c; | |
522 | ||
523 | if (utf8_encoded_to_unichar(str, &c) < 0) | |
524 | return (size_t) -1; | |
525 | ||
526 | str = utf8_next_char(str); | |
527 | ||
528 | n += unichar_iswide(c) ? 2 : 1; | |
529 | } | |
530 | ||
531 | return n; | |
532 | } |