1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 /* Parts of this file are based on the GLIB utf8 validation functions. The
4 * original license text follows. */
6 /* gutf8.c - Operations on UTF-8 strings.
8 * Copyright (C) 1999 Tom Tromey
9 * Copyright (C) 2000 Red Hat, Inc.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
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
31 #include "alloc-util.h"
33 #include "hexdecoct.h"
37 bool unichar_is_valid(char32_t ch
) {
39 if (ch
>= 0x110000) /* End of unicode space */
41 if ((ch
& 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
43 if ((ch
>= 0xFDD0) && (ch
<= 0xFDEF)) /* Reserved */
45 if ((ch
& 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
51 static bool unichar_is_control(char32_t ch
) {
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.
59 return (ch
< ' ' && !IN_SET(ch
, '\t', '\n')) ||
60 (0x7F <= ch
&& ch
<= 0x9F);
63 /* count of characters used to encode one unicode char */
64 static size_t utf8_encoded_expected_len(const char *str
) {
72 if ((c
& 0xe0) == 0xc0)
74 if ((c
& 0xf0) == 0xe0)
76 if ((c
& 0xf8) == 0xf0)
78 if ((c
& 0xfc) == 0xf8)
80 if ((c
& 0xfe) == 0xfc)
86 /* decode one unicode char */
87 int utf8_encoded_to_unichar(const char *str
, char32_t
*ret_unichar
) {
93 len
= utf8_encoded_expected_len(str
);
97 *ret_unichar
= (char32_t
)str
[0];
100 unichar
= str
[0] & 0x1f;
103 unichar
= (char32_t
)str
[0] & 0x0f;
106 unichar
= (char32_t
)str
[0] & 0x07;
109 unichar
= (char32_t
)str
[0] & 0x03;
112 unichar
= (char32_t
)str
[0] & 0x01;
118 for (i
= 1; i
< len
; i
++) {
119 if (((char32_t
)str
[i
] & 0xc0) != 0x80)
123 unichar
|= (char32_t
)str
[i
] & 0x3f;
126 *ret_unichar
= unichar
;
131 bool utf8_is_printable_newline(const char* str
, size_t length
, bool newline
) {
136 for (p
= str
; length
;) {
140 encoded_len
= utf8_encoded_valid_unichar(p
);
141 if (encoded_len
< 0 ||
142 (size_t) encoded_len
> length
)
145 r
= utf8_encoded_to_unichar(p
, &val
);
147 unichar_is_control(val
) ||
148 (!newline
&& val
== '\n'))
151 length
-= encoded_len
;
158 char *utf8_is_valid(const char *str
) {
167 len
= utf8_encoded_valid_unichar(p
);
177 char *utf8_escape_invalid(const char *str
) {
182 p
= s
= malloc(strlen(str
) * 4 + 1);
189 len
= utf8_encoded_valid_unichar(str
);
191 s
= mempcpy(s
, str
, len
);
194 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
204 char *utf8_escape_non_printable(const char *str
) {
209 p
= s
= malloc(strlen(str
) * 4 + 1);
216 len
= utf8_encoded_valid_unichar(str
);
218 if (utf8_is_printable(str
, len
)) {
219 s
= mempcpy(s
, str
, len
);
225 *(s
++) = hexchar((int) *str
>> 4);
226 *(s
++) = hexchar((int) *str
);
233 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
243 char *ascii_is_valid(const char *str
) {
246 /* Check whether the string consists of valid ASCII bytes,
247 * i.e values between 0 and 127, inclusive. */
251 for (p
= str
; *p
; p
++)
252 if ((unsigned char) *p
>= 128)
258 char *ascii_is_valid_n(const char *str
, size_t len
) {
261 /* Very similar to ascii_is_valid(), but checks exactly len
262 * bytes and rejects any NULs in that range. */
266 for (i
= 0; i
< len
; i
++)
267 if ((unsigned char) str
[i
] >= 128 || str
[i
] == 0)
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
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.
282 * Returns: The length in bytes that the UTF-8 representation does or would
285 size_t utf8_encode_unichar(char *out_utf8
, char32_t g
) {
289 out_utf8
[0] = g
& 0x7f;
291 } else if (g
< (1 << 11)) {
293 out_utf8
[0] = 0xc0 | ((g
>> 6) & 0x1f);
294 out_utf8
[1] = 0x80 | (g
& 0x3f);
297 } else if (g
< (1 << 16)) {
299 out_utf8
[0] = 0xe0 | ((g
>> 12) & 0x0f);
300 out_utf8
[1] = 0x80 | ((g
>> 6) & 0x3f);
301 out_utf8
[2] = 0x80 | (g
& 0x3f);
304 } else if (g
< (1 << 21)) {
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);
317 char *utf16_to_utf8(const char16_t
*s
, size_t length
/* bytes! */) {
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 */
328 r
= new(char, length
* 2 + 1);
332 f
= (const uint8_t*) s
;
335 while (f
+ 1 < (const uint8_t*) s
+ length
) {
338 /* see RFC 2781 section 2.2 */
340 w1
= f
[1] << 8 | f
[0];
343 if (!utf16_is_surrogate(w1
)) {
344 t
+= utf8_encode_unichar(t
, w1
);
348 if (utf16_is_trailing_surrogate(w1
))
349 continue; /* spurious trailing surrogate, ignore */
351 if (f
+ 1 >= (const uint8_t*) s
+ length
)
354 w2
= f
[1] << 8 | f
[0];
357 if (!utf16_is_trailing_surrogate(w2
)) {
359 continue; /* surrogate missing its trailing surrogate, ignore */
362 t
+= utf8_encode_unichar(t
, utf16_surrogate_pair_to_unichar(w1
, w2
));
369 size_t utf16_encode_unichar(char16_t
*out
, char32_t c
) {
371 /* Note that this encodes as little-endian. */
376 case 0xe000U
... 0xffffU
:
380 case 0x10000U
... 0x10ffffU
:
382 out
[0] = htole16((c
>> 10) + 0xd800U
);
383 out
[1] = htole16((c
& 0x3ffU
) + 0xdc00U
);
386 default: /* A surrogate (invalid) */
391 char16_t
*utf8_to_utf16(const char *s
, size_t length
) {
398 n
= new(char16_t
, length
+ 1);
404 for (i
= 0; i
< length
;) {
408 e
= utf8_encoded_expected_len(s
+ i
);
409 if (e
<= 1) /* Invalid and single byte characters are copied as they are */
412 if (i
+ e
> length
) /* sequence longer than input buffer, then copy as-is */
415 r
= utf8_encoded_to_unichar(s
+ i
, &unichar
);
416 if (r
< 0) /* sequence invalid, then copy as-is */
419 p
+= utf16_encode_unichar(p
, unichar
);
424 *(p
++) = htole16(s
[i
++]);
431 size_t char16_strlen(const char16_t
*s
) {
442 /* expected size used to encode one unicode char */
443 static int utf8_unichar_to_encoded_len(char32_t unichar
) {
449 if (unichar
< 0x10000)
451 if (unichar
< 0x200000)
453 if (unichar
< 0x4000000)
459 /* validate one encoded unicode char and return its length */
460 int utf8_encoded_valid_unichar(const char *str
) {
467 len
= utf8_encoded_expected_len(str
);
475 /* check if expected encoded chars are available */
476 for (i
= 0; i
< len
; i
++)
477 if ((str
[i
] & 0x80) != 0x80)
480 r
= utf8_encoded_to_unichar(str
, &unichar
);
484 /* check if encoded length matches encoded value */
485 if (utf8_unichar_to_encoded_len(unichar
) != (int) len
)
488 /* check if value has valid range */
489 if (!unichar_is_valid(unichar
))
495 size_t utf8_n_codepoints(const char *str
) {
498 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
503 k
= utf8_encoded_valid_unichar(str
);
514 size_t utf8_console_width(const char *str
) {
517 /* Returns the approximate width a string will take on screen when printed on a character cell
518 * terminal/console. */
523 if (utf8_encoded_to_unichar(str
, &c
) < 0)
526 str
= utf8_next_char(str
);
528 n
+= unichar_iswide(c
) ? 2 : 1;