]>
| Commit | Line | Data |
|---|---|---|
| 6952ebae | 1 | /* SPDX-License-Identifier: LGPL-2.0-or-later */ |
| 7f110ff9 | 2 | |
| 6952ebae | 3 | /* Parts of this file are based on the GLIB utf8 validation functions. The original copyright follows. |
| 7f110ff9 | 4 | * |
| 6952ebae | 5 | * gutf8.c - Operations on UTF-8 strings. |
| 7f110ff9 LP |
6 | * Copyright (C) 1999 Tom Tromey |
| 7 | * Copyright (C) 2000 Red Hat, Inc. | |
| 7f110ff9 LP |
8 | */ |
| 9 | ||
| b5efdb8a | 10 | #include "alloc-util.h" |
| 3f536d5b | 11 | #include "gunicode.h" |
| e4e73a63 | 12 | #include "hexdecoct.h" |
| 4c6d5139 | 13 | #include "string-util.h" |
| 7f110ff9 LP |
14 | #include "utf8.h" |
| 15 | ||
| c932fb71 | 16 | bool unichar_is_valid(char32_t ch) { |
| 7f110ff9 LP |
17 | |
| 18 | if (ch >= 0x110000) /* End of unicode space */ | |
| 19 | return false; | |
| 20 | if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */ | |
| 21 | return false; | |
| 22 | if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */ | |
| 23 | return false; | |
| 24 | if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */ | |
| 25 | return false; | |
| 26 | ||
| 27 | return true; | |
| 28 | } | |
| 29 | ||
| c932fb71 | 30 | static bool unichar_is_control(char32_t ch) { |
| ba961854 ZJS |
31 | |
| 32 | /* | |
| 33 | 0 to ' '-1 is the C0 range. | |
| 34 | DEL=0x7F, and DEL+1 to 0x9F is C1 range. | |
| 35 | '\t' is in C0 range, but more or less harmless and commonly used. | |
| 36 | */ | |
| 37 | ||
| 4c701096 | 38 | return (ch < ' ' && !IN_SET(ch, '\t', '\n')) || |
| ba961854 ZJS |
39 | (0x7F <= ch && ch <= 0x9F); |
| 40 | } | |
| 41 | ||
| 7991ac34 | 42 | /* count of characters used to encode one unicode char */ |
| 84319aa7 | 43 | static size_t utf8_encoded_expected_len(uint8_t c) { |
| 7991ac34 DR |
44 | if (c < 0x80) |
| 45 | return 1; | |
| 46 | if ((c & 0xe0) == 0xc0) | |
| 47 | return 2; | |
| 48 | if ((c & 0xf0) == 0xe0) | |
| 49 | return 3; | |
| 50 | if ((c & 0xf8) == 0xf0) | |
| 51 | return 4; | |
| 52 | if ((c & 0xfc) == 0xf8) | |
| 53 | return 5; | |
| 54 | if ((c & 0xfe) == 0xfc) | |
| 55 | return 6; | |
| 7e8185ef | 56 | |
| 7991ac34 DR |
57 | return 0; |
| 58 | } | |
| ba961854 | 59 | |
| 7991ac34 | 60 | /* decode one unicode char */ |
| c932fb71 SL |
61 | int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) { |
| 62 | char32_t unichar; | |
| fe96c0f8 | 63 | size_t len; |
| 7e8185ef LP |
64 | |
| 65 | assert(str); | |
| 18e70eec | 66 | assert(ret_unichar); |
| ba961854 | 67 | |
| 84319aa7 | 68 | len = utf8_encoded_expected_len(str[0]); |
| 7e8185ef | 69 | |
| 7991ac34 DR |
70 | switch (len) { |
| 71 | case 1: | |
| c932fb71 | 72 | *ret_unichar = (char32_t)str[0]; |
| 9579e9a5 | 73 | return 1; |
| 7991ac34 DR |
74 | case 2: |
| 75 | unichar = str[0] & 0x1f; | |
| 76 | break; | |
| 77 | case 3: | |
| c932fb71 | 78 | unichar = (char32_t)str[0] & 0x0f; |
| 7991ac34 DR |
79 | break; |
| 80 | case 4: | |
| c932fb71 | 81 | unichar = (char32_t)str[0] & 0x07; |
| 7991ac34 DR |
82 | break; |
| 83 | case 5: | |
| c932fb71 | 84 | unichar = (char32_t)str[0] & 0x03; |
| 7991ac34 DR |
85 | break; |
| 86 | case 6: | |
| c932fb71 | 87 | unichar = (char32_t)str[0] & 0x01; |
| 7991ac34 DR |
88 | break; |
| 89 | default: | |
| 7e8185ef | 90 | return -EINVAL; |
| ba961854 ZJS |
91 | } |
| 92 | ||
| fe96c0f8 | 93 | for (size_t i = 1; i < len; i++) { |
| c932fb71 | 94 | if (((char32_t)str[i] & 0xc0) != 0x80) |
| 7e8185ef | 95 | return -EINVAL; |
| 7c421857 | 96 | |
| 7991ac34 | 97 | unichar <<= 6; |
| c932fb71 | 98 | unichar |= (char32_t)str[i] & 0x3f; |
| 7991ac34 DR |
99 | } |
| 100 | ||
| c932fb71 | 101 | *ret_unichar = unichar; |
| 9579e9a5 | 102 | return len; |
| ba961854 ZJS |
103 | } |
| 104 | ||
| 618727da | 105 | bool utf8_is_printable_newline(const char* str, size_t length, bool allow_newline) { |
| 7f110ff9 LP |
106 | assert(str); |
| 107 | ||
| a456086b | 108 | for (const char *p = str; length > 0;) { |
| 9579e9a5 | 109 | int encoded_len; |
| c932fb71 | 110 | char32_t val; |
| 7e8185ef | 111 | |
| 92e068b4 ZJS |
112 | encoded_len = utf8_encoded_valid_unichar(p, length); |
| 113 | if (encoded_len < 0) | |
| 144b3d9e | 114 | return false; |
| 92e068b4 | 115 | assert(encoded_len > 0 && (size_t) encoded_len <= length); |
| 144b3d9e | 116 | |
| 9579e9a5 | 117 | if (utf8_encoded_to_unichar(p, &val) < 0 || |
| f3ee6297 | 118 | unichar_is_control(val) || |
| 618727da | 119 | (!allow_newline && val == '\n')) |
| 7991ac34 | 120 | return false; |
| 7f110ff9 | 121 | |
| 7991ac34 | 122 | length -= encoded_len; |
| a7176505 | 123 | p += encoded_len; |
| 7f110ff9 LP |
124 | } |
| 125 | ||
| 7991ac34 | 126 | return true; |
| 7f110ff9 LP |
127 | } |
| 128 | ||
| 7ff71610 | 129 | char* utf8_is_valid_n(const char *str, size_t len_bytes) { |
| 80ab31a4 ZJS |
130 | /* Check if the string is composed of valid utf8 characters. If length len_bytes is given, stop after |
| 131 | * len_bytes. Otherwise, stop at NUL. */ | |
| 7f110ff9 LP |
132 | |
| 133 | assert(str); | |
| 134 | ||
| 7ff71610 | 135 | for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) { |
| faaa5728 LP |
136 | int len; |
| 137 | ||
| 7ff71610 | 138 | if (_unlikely_(str[i] == '\0')) |
| 80ab31a4 ZJS |
139 | return NULL; /* embedded NUL */ |
| 140 | ||
| 7ff71610 MY |
141 | len = utf8_encoded_valid_unichar(str + i, |
| 142 | len_bytes != SIZE_MAX ? len_bytes - i : SIZE_MAX); | |
| 80ab31a4 ZJS |
143 | if (_unlikely_(len < 0)) |
| 144 | return NULL; /* invalid character */ | |
| 7991ac34 | 145 | |
| 7ff71610 | 146 | i += len; |
| 7991ac34 | 147 | } |
| 7f110ff9 | 148 | |
| e71fb4b3 | 149 | return (char*) str; |
| 7f110ff9 LP |
150 | } |
| 151 | ||
| ff3f2953 | 152 | char* utf8_escape_invalid(const char *str) { |
| 550a40ec ZJS |
153 | char *p, *s; |
| 154 | ||
| 155 | assert(str); | |
| 156 | ||
| 157 | p = s = malloc(strlen(str) * 4 + 1); | |
| 158 | if (!p) | |
| 159 | return NULL; | |
| 160 | ||
| 161 | while (*str) { | |
| 162 | int len; | |
| 163 | ||
| f5fbe71d | 164 | len = utf8_encoded_valid_unichar(str, SIZE_MAX); |
| 550a40ec ZJS |
165 | if (len > 0) { |
| 166 | s = mempcpy(s, str, len); | |
| 167 | str += len; | |
| 168 | } else { | |
| 3c6d3052 | 169 | s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER); |
| 550a40ec ZJS |
170 | str += 1; |
| 171 | } | |
| 172 | } | |
| 7e8185ef | 173 | |
| 550a40ec | 174 | *s = '\0'; |
| 523e1b14 | 175 | return str_realloc(p); |
| 550a40ec ZJS |
176 | } |
| 177 | ||
| 9632f8b4 | 178 | int utf8_char_console_width(const char *str) { |
| da88f542 ZJS |
179 | char32_t c; |
| 180 | int r; | |
| 181 | ||
| 5c0d4b50 YW |
182 | assert(str); |
| 183 | ||
| da88f542 ZJS |
184 | r = utf8_encoded_to_unichar(str, &c); |
| 185 | if (r < 0) | |
| 186 | return r; | |
| 187 | ||
| 5c0d4b50 YW |
188 | return unichar_console_width(c); |
| 189 | } | |
| 190 | ||
| 191 | int unichar_console_width(char32_t c) { | |
| b4aaba2b LP |
192 | if (c == '\t') |
| 193 | return 8; /* Assume a tab width of 8 */ | |
| 194 | ||
| da88f542 ZJS |
195 | /* TODO: we should detect combining characters */ |
| 196 | ||
| 197 | return unichar_iswide(c) ? 2 : 1; | |
| 198 | } | |
| 199 | ||
| ff3f2953 | 200 | char* utf8_escape_non_printable_full(const char *str, size_t console_width, bool force_ellipsis) { |
| da88f542 ZJS |
201 | char *p, *s, *prev_s; |
| 202 | size_t n = 0; /* estimated print width */ | |
| fec84576 WC |
203 | |
| 204 | assert(str); | |
| 205 | ||
| da88f542 ZJS |
206 | if (console_width == 0) |
| 207 | return strdup(""); | |
| 208 | ||
| 17260a97 | 209 | size_t body = strlen(str) * 4; |
| 210 | p = s = prev_s = malloc(body + STRLEN("…") + 1); | |
| fec84576 WC |
211 | if (!p) |
| 212 | return NULL; | |
| 213 | ||
| da88f542 | 214 | for (;;) { |
| fec84576 | 215 | int len; |
| da88f542 ZJS |
216 | char *saved_s = s; |
| 217 | ||
| fc96e5c0 ZJS |
218 | if (!*str) { /* done! */ |
| 219 | if (force_ellipsis) | |
| 220 | goto truncation; | |
| 221 | else | |
| 222 | goto finish; | |
| 223 | } | |
| fec84576 | 224 | |
| f5fbe71d | 225 | len = utf8_encoded_valid_unichar(str, SIZE_MAX); |
| fec84576 WC |
226 | if (len > 0) { |
| 227 | if (utf8_is_printable(str, len)) { | |
| da88f542 ZJS |
228 | int w; |
| 229 | ||
| 230 | w = utf8_char_console_width(str); | |
| 231 | assert(w >= 0); | |
| 232 | if (n + w > console_width) | |
| 233 | goto truncation; | |
| 234 | ||
| fec84576 WC |
235 | s = mempcpy(s, str, len); |
| 236 | str += len; | |
| da88f542 ZJS |
237 | n += w; |
| 238 | ||
| fec84576 | 239 | } else { |
| da88f542 ZJS |
240 | for (; len > 0; len--) { |
| 241 | if (n + 4 > console_width) | |
| 242 | goto truncation; | |
| 243 | ||
| fec84576 WC |
244 | *(s++) = '\\'; |
| 245 | *(s++) = 'x'; | |
| 246 | *(s++) = hexchar((int) *str >> 4); | |
| 247 | *(s++) = hexchar((int) *str); | |
| fec84576 | 248 | |
| 3c6d3052 | 249 | str += 1; |
| da88f542 | 250 | n += 4; |
| 3c6d3052 | 251 | } |
| fec84576 WC |
252 | } |
| 253 | } else { | |
| da88f542 ZJS |
254 | if (n + 1 > console_width) |
| 255 | goto truncation; | |
| 256 | ||
| 257 | s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER)); | |
| fec84576 | 258 | str += 1; |
| da88f542 | 259 | n += 1; |
| fec84576 | 260 | } |
| da88f542 ZJS |
261 | |
| 262 | prev_s = saved_s; | |
| fec84576 WC |
263 | } |
| 264 | ||
| da88f542 ZJS |
265 | truncation: |
| 266 | /* Try to go back one if we don't have enough space for the ellipsis */ | |
| fc96e5c0 | 267 | if (n + 1 > console_width) |
| da88f542 ZJS |
268 | s = prev_s; |
| 269 | ||
| 270 | s = mempcpy(s, "…", strlen("…")); | |
| fec84576 | 271 | |
| da88f542 ZJS |
272 | finish: |
| 273 | *s = '\0'; | |
| 523e1b14 | 274 | return str_realloc(p); |
| fec84576 WC |
275 | } |
| 276 | ||
| 4d06bf59 MY |
277 | char* ascii_is_valid_n(const char *str, size_t len) { |
| 278 | /* Check whether the string consists of valid ASCII bytes, i.e values between 1 and 127, inclusive. | |
| 279 | * Stops at len, or NUL byte if len is SIZE_MAX. */ | |
| 294a3121 | 280 | |
| 7f110ff9 LP |
281 | assert(str); |
| 282 | ||
| 4d06bf59 MY |
283 | for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++) |
| 284 | if ((unsigned char) str[i] >= 128 || str[i] == '\0') | |
| 294a3121 ZJS |
285 | return NULL; |
| 286 | ||
| 287 | return (char*) str; | |
| 288 | } | |
| 289 | ||
| 9b49a3b4 ZJS |
290 | int utf8_to_ascii(const char *str, char replacement_char, char **ret) { |
| 291 | /* Convert to a string that has only ASCII chars, replacing anything that is not ASCII | |
| 292 | * by replacement_char. */ | |
| 293 | ||
| bd141bd8 MV |
294 | assert(str); |
| 295 | assert(ret); | |
| 296 | ||
| 9b49a3b4 ZJS |
297 | _cleanup_free_ char *ans = new(char, strlen(str) + 1); |
| 298 | if (!ans) | |
| 299 | return -ENOMEM; | |
| 300 | ||
| 301 | char *q = ans; | |
| 302 | ||
| 303 | for (const char *p = str; *p; q++) { | |
| 304 | int l; | |
| 305 | ||
| 306 | l = utf8_encoded_valid_unichar(p, SIZE_MAX); | |
| 307 | if (l < 0) /* Non-UTF-8, let's not even try to propagate the garbage */ | |
| 308 | return l; | |
| 309 | ||
| 310 | if (l == 1) | |
| 311 | *q = *p; | |
| 312 | else | |
| 313 | /* non-ASCII, we need to replace it */ | |
| 314 | *q = replacement_char; | |
| 315 | ||
| 316 | p += l; | |
| 317 | } | |
| 318 | *q = '\0'; | |
| 319 | ||
| 320 | *ret = TAKE_PTR(ans); | |
| 321 | return 0; | |
| 322 | } | |
| 323 | ||
| 2bb4c7e3 TG |
324 | /** |
| 325 | * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8 | |
| 326 | * @out_utf8: output buffer of at least 4 bytes or NULL | |
| 327 | * @g: UCS-4 character to encode | |
| 328 | * | |
| 329 | * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8. | |
| 330 | * The length of the character is returned. It is not zero-terminated! If the | |
| 331 | * output buffer is NULL, only the length is returned. | |
| 332 | * | |
| 333 | * Returns: The length in bytes that the UTF-8 representation does or would | |
| 334 | * occupy. | |
| 335 | */ | |
| c932fb71 | 336 | size_t utf8_encode_unichar(char *out_utf8, char32_t g) { |
| f3ee6297 | 337 | |
| 2bb4c7e3 TG |
338 | if (g < (1 << 7)) { |
| 339 | if (out_utf8) | |
| 340 | out_utf8[0] = g & 0x7f; | |
| e7eebcfc | 341 | return 1; |
| 2bb4c7e3 TG |
342 | } else if (g < (1 << 11)) { |
| 343 | if (out_utf8) { | |
| 344 | out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f); | |
| 345 | out_utf8[1] = 0x80 | (g & 0x3f); | |
| 346 | } | |
| e7eebcfc | 347 | return 2; |
| 2bb4c7e3 TG |
348 | } else if (g < (1 << 16)) { |
| 349 | if (out_utf8) { | |
| 350 | out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f); | |
| 351 | out_utf8[1] = 0x80 | ((g >> 6) & 0x3f); | |
| 352 | out_utf8[2] = 0x80 | (g & 0x3f); | |
| 353 | } | |
| e7eebcfc | 354 | return 3; |
| 2bb4c7e3 TG |
355 | } else if (g < (1 << 21)) { |
| 356 | if (out_utf8) { | |
| 357 | out_utf8[0] = 0xf0 | ((g >> 18) & 0x07); | |
| 358 | out_utf8[1] = 0x80 | ((g >> 12) & 0x3f); | |
| 359 | out_utf8[2] = 0x80 | ((g >> 6) & 0x3f); | |
| 360 | out_utf8[3] = 0x80 | (g & 0x3f); | |
| 361 | } | |
| 362 | return 4; | |
| e7eebcfc | 363 | } |
| f3ee6297 LP |
364 | |
| 365 | return 0; | |
| e7eebcfc LP |
366 | } |
| 367 | ||
| ff3f2953 | 368 | char* utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) { |
| 2e3d0692 | 369 | const uint8_t *f; |
| e7eebcfc | 370 | char *r, *t; |
| 2e3d0692 | 371 | |
| ba091282 LP |
372 | if (length == 0) |
| 373 | return new0(char, 1); | |
| 374 | ||
| 2ac2ff3f LP |
375 | assert(s); |
| 376 | ||
| ba091282 LP |
377 | if (length == SIZE_MAX) { |
| 378 | length = char16_strlen(s); | |
| 379 | ||
| 380 | if (length > SIZE_MAX/2) | |
| 381 | return NULL; /* overflow */ | |
| 382 | ||
| 383 | length *= 2; | |
| 384 | } | |
| 385 | ||
| 2ac2ff3f LP |
386 | /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may |
| 387 | * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */ | |
| ba091282 | 388 | if (length > (SIZE_MAX - 1) / 2) |
| 2ac2ff3f LP |
389 | return NULL; /* overflow */ |
| 390 | ||
| 391 | r = new(char, length * 2 + 1); | |
| 2e3d0692 LP |
392 | if (!r) |
| 393 | return NULL; | |
| 394 | ||
| 2ac2ff3f | 395 | f = (const uint8_t*) s; |
| 04166cb7 TG |
396 | t = r; |
| 397 | ||
| 2ac2ff3f | 398 | while (f + 1 < (const uint8_t*) s + length) { |
| c932fb71 | 399 | char16_t w1, w2; |
| 04166cb7 TG |
400 | |
| 401 | /* see RFC 2781 section 2.2 */ | |
| 402 | ||
| 403 | w1 = f[1] << 8 | f[0]; | |
| 404 | f += 2; | |
| 405 | ||
| 406 | if (!utf16_is_surrogate(w1)) { | |
| dcd12626 | 407 | t += utf8_encode_unichar(t, w1); |
| 04166cb7 TG |
408 | continue; |
| 409 | } | |
| 410 | ||
| 411 | if (utf16_is_trailing_surrogate(w1)) | |
| 2ac2ff3f LP |
412 | continue; /* spurious trailing surrogate, ignore */ |
| 413 | ||
| 414 | if (f + 1 >= (const uint8_t*) s + length) | |
| 04166cb7 TG |
415 | break; |
| 416 | ||
| 417 | w2 = f[1] << 8 | f[0]; | |
| 418 | f += 2; | |
| 419 | ||
| 420 | if (!utf16_is_trailing_surrogate(w2)) { | |
| 421 | f -= 2; | |
| 2ac2ff3f | 422 | continue; /* surrogate missing its trailing surrogate, ignore */ |
| 04166cb7 TG |
423 | } |
| 424 | ||
| 425 | t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2)); | |
| 426 | } | |
| 2e3d0692 LP |
427 | |
| 428 | *t = 0; | |
| 7b4d7cc0 | 429 | return r; |
| 2e3d0692 | 430 | } |
| 02a36bc9 | 431 | |
| 80b0a597 | 432 | size_t utf16_encode_unichar(char16_t *out, char32_t c) { |
| bc4dfc24 | 433 | assert(out); |
| 80b0a597 LP |
434 | |
| 435 | /* Note that this encodes as little-endian. */ | |
| 436 | ||
| 437 | switch (c) { | |
| 438 | ||
| 439 | case 0 ... 0xd7ffU: | |
| 440 | case 0xe000U ... 0xffffU: | |
| 441 | out[0] = htole16(c); | |
| 442 | return 1; | |
| 443 | ||
| 444 | case 0x10000U ... 0x10ffffU: | |
| 445 | c -= 0x10000U; | |
| 446 | out[0] = htole16((c >> 10) + 0xd800U); | |
| 447 | out[1] = htole16((c & 0x3ffU) + 0xdc00U); | |
| 448 | return 2; | |
| 449 | ||
| 450 | default: /* A surrogate (invalid) */ | |
| 451 | return 0; | |
| 452 | } | |
| 453 | } | |
| 454 | ||
| 455 | char16_t *utf8_to_utf16(const char *s, size_t length) { | |
| 456 | char16_t *n, *p; | |
| 80b0a597 LP |
457 | int r; |
| 458 | ||
| ba091282 LP |
459 | if (length == 0) |
| 460 | return new0(char16_t, 1); | |
| 461 | ||
| 80b0a597 LP |
462 | assert(s); |
| 463 | ||
| ba091282 LP |
464 | if (length == SIZE_MAX) |
| 465 | length = strlen(s); | |
| 466 | ||
| 467 | if (length > SIZE_MAX - 1) | |
| 468 | return NULL; /* overflow */ | |
| 469 | ||
| 80b0a597 LP |
470 | n = new(char16_t, length + 1); |
| 471 | if (!n) | |
| 472 | return NULL; | |
| 473 | ||
| 474 | p = n; | |
| 475 | ||
| fe96c0f8 | 476 | for (size_t i = 0; i < length;) { |
| 80b0a597 LP |
477 | char32_t unichar; |
| 478 | size_t e; | |
| 479 | ||
| 84319aa7 | 480 | e = utf8_encoded_expected_len(s[i]); |
| 80b0a597 LP |
481 | if (e <= 1) /* Invalid and single byte characters are copied as they are */ |
| 482 | goto copy; | |
| 483 | ||
| 484 | if (i + e > length) /* sequence longer than input buffer, then copy as-is */ | |
| 485 | goto copy; | |
| 486 | ||
| 487 | r = utf8_encoded_to_unichar(s + i, &unichar); | |
| 488 | if (r < 0) /* sequence invalid, then copy as-is */ | |
| 489 | goto copy; | |
| 490 | ||
| 491 | p += utf16_encode_unichar(p, unichar); | |
| 492 | i += e; | |
| 493 | continue; | |
| 494 | ||
| 495 | copy: | |
| 496 | *(p++) = htole16(s[i++]); | |
| 497 | } | |
| 498 | ||
| 499 | *p = 0; | |
| 500 | return n; | |
| 501 | } | |
| 502 | ||
| 503 | size_t char16_strlen(const char16_t *s) { | |
| 504 | size_t n = 0; | |
| 505 | ||
| 506 | assert(s); | |
| 507 | ||
| 508 | while (*s != 0) | |
| 509 | n++, s++; | |
| 510 | ||
| 511 | return n; | |
| 512 | } | |
| 513 | ||
| a07864a4 | 514 | size_t char16_strsize(const char16_t *s) { |
| 18e70eec MV |
515 | POINTER_MAY_BE_NULL(s); |
| 516 | ||
| a07864a4 DDM |
517 | return s ? (char16_strlen(s) + 1) * sizeof(*s) : 0; |
| 518 | } | |
| 519 | ||
| 02a36bc9 | 520 | /* expected size used to encode one unicode char */ |
| c932fb71 | 521 | static int utf8_unichar_to_encoded_len(char32_t unichar) { |
| 7e8185ef | 522 | |
| 02a36bc9 DR |
523 | if (unichar < 0x80) |
| 524 | return 1; | |
| 525 | if (unichar < 0x800) | |
| 526 | return 2; | |
| 527 | if (unichar < 0x10000) | |
| 528 | return 3; | |
| 529 | if (unichar < 0x200000) | |
| 530 | return 4; | |
| 531 | if (unichar < 0x4000000) | |
| 532 | return 5; | |
| 7e8185ef | 533 | |
| 02a36bc9 DR |
534 | return 6; |
| 535 | } | |
| 536 | ||
| 537 | /* validate one encoded unicode char and return its length */ | |
| 92e068b4 | 538 | int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) { |
| c932fb71 | 539 | char32_t unichar; |
| fe96c0f8 | 540 | size_t len; |
| 7c421857 | 541 | int r; |
| 7e8185ef LP |
542 | |
| 543 | assert(str); | |
| 92e068b4 ZJS |
544 | assert(length > 0); |
| 545 | ||
| f5fbe71d | 546 | /* We read until NUL, at most length bytes. SIZE_MAX may be used to disable the length check. */ |
| 02a36bc9 | 547 | |
| 84319aa7 | 548 | len = utf8_encoded_expected_len(str[0]); |
| 02a36bc9 | 549 | if (len == 0) |
| 7e8185ef | 550 | return -EINVAL; |
| 02a36bc9 | 551 | |
| 92e068b4 ZJS |
552 | /* Do we have a truncated multi-byte character? */ |
| 553 | if (len > length) | |
| 554 | return -EINVAL; | |
| 555 | ||
| 02a36bc9 DR |
556 | /* ascii is valid */ |
| 557 | if (len == 1) | |
| 558 | return 1; | |
| 559 | ||
| 560 | /* check if expected encoded chars are available */ | |
| fe96c0f8 | 561 | for (size_t i = 0; i < len; i++) |
| 02a36bc9 | 562 | if ((str[i] & 0x80) != 0x80) |
| 7e8185ef | 563 | return -EINVAL; |
| 02a36bc9 | 564 | |
| c932fb71 SL |
565 | r = utf8_encoded_to_unichar(str, &unichar); |
| 566 | if (r < 0) | |
| 567 | return r; | |
| 02a36bc9 DR |
568 | |
| 569 | /* check if encoded length matches encoded value */ | |
| 7c421857 | 570 | if (utf8_unichar_to_encoded_len(unichar) != (int) len) |
| 7e8185ef | 571 | return -EINVAL; |
| 02a36bc9 DR |
572 | |
| 573 | /* check if value has valid range */ | |
| f3ee6297 | 574 | if (!unichar_is_valid(unichar)) |
| 7e8185ef | 575 | return -EINVAL; |
| 02a36bc9 | 576 | |
| 7c421857 | 577 | return (int) len; |
| 02a36bc9 | 578 | } |
| 65ee8660 LP |
579 | |
| 580 | size_t utf8_n_codepoints(const char *str) { | |
| 581 | size_t n = 0; | |
| 582 | ||
| 18e70eec MV |
583 | assert(str); |
| 584 | ||
| f5fbe71d | 585 | /* Returns the number of UTF-8 codepoints in this string, or SIZE_MAX if the string is not valid UTF-8. */ |
| 65ee8660 LP |
586 | |
| 587 | while (*str != 0) { | |
| 588 | int k; | |
| 589 | ||
| f5fbe71d | 590 | k = utf8_encoded_valid_unichar(str, SIZE_MAX); |
| 65ee8660 | 591 | if (k < 0) |
| f5fbe71d | 592 | return SIZE_MAX; |
| 65ee8660 LP |
593 | |
| 594 | str += k; | |
| 595 | n++; | |
| 596 | } | |
| 597 | ||
| 598 | return n; | |
| 599 | } | |
| 3f536d5b LP |
600 | |
| 601 | size_t utf8_console_width(const char *str) { | |
| 18e70eec | 602 | POINTER_MAY_BE_NULL(str); |
| bba55185 LP |
603 | |
| 604 | if (isempty(str)) | |
| 605 | return 0; | |
| 3f536d5b LP |
606 | |
| 607 | /* Returns the approximate width a string will take on screen when printed on a character cell | |
| 608 | * terminal/console. */ | |
| 609 | ||
| bba55185 | 610 | size_t n = 0; |
| da88f542 ZJS |
611 | while (*str) { |
| 612 | int w; | |
| 3f536d5b | 613 | |
| da88f542 ZJS |
614 | w = utf8_char_console_width(str); |
| 615 | if (w < 0) | |
| f5fbe71d | 616 | return SIZE_MAX; |
| 3f536d5b | 617 | |
| da88f542 | 618 | n += w; |
| 3f536d5b | 619 | str = utf8_next_char(str); |
| 3f536d5b LP |
620 | } |
| 621 | ||
| 622 | return n; | |
| 623 | } | |
| 104a6b8c LP |
624 | |
| 625 | size_t utf8_last_length(const char *s, size_t n) { | |
| 626 | int r; | |
| 627 | ||
| 012658fc MY |
628 | assert(s); |
| 629 | ||
| 104a6b8c LP |
630 | if (n == SIZE_MAX) |
| 631 | n = strlen(s); | |
| 632 | ||
| 633 | /* Determines length in bytes of last UTF-8 codepoint in string. If the string is empty, returns | |
| 634 | * zero. Treats invalid UTF-8 codepoints as 1 sized ones. */ | |
| 635 | ||
| 636 | for (size_t last = 0;;) { | |
| 637 | if (n == 0) | |
| 638 | return last; | |
| 639 | ||
| 640 | r = utf8_encoded_valid_unichar(s, n); | |
| 641 | if (r <= 0) | |
| 642 | r = 1; /* treat invalid UTF-8 as byte-wide */ | |
| 643 | ||
| 644 | s += r; | |
| 645 | n -= r; | |
| 646 | last = r; | |
| 647 | } | |
| 648 | } |