1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2011 Kay Sievers
6 Copyright 2012 Lennart Poettering
9 /* Parts of this file are based on the GLIB utf8 validation functions. The
10 * original license text follows. */
12 /* gutf8.c - Operations on UTF-8 strings.
14 * Copyright (C) 1999 Tom Tromey
15 * Copyright (C) 2000 Red Hat, Inc.
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Library General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
27 * You should have received a copy of the GNU Library General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "alloc-util.h"
39 #include "hexdecoct.h"
43 bool unichar_is_valid(char32_t ch
) {
45 if (ch
>= 0x110000) /* End of unicode space */
47 if ((ch
& 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
49 if ((ch
>= 0xFDD0) && (ch
<= 0xFDEF)) /* Reserved */
51 if ((ch
& 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
57 static bool unichar_is_control(char32_t ch
) {
60 0 to ' '-1 is the C0 range.
61 DEL=0x7F, and DEL+1 to 0x9F is C1 range.
62 '\t' is in C0 range, but more or less harmless and commonly used.
65 return (ch
< ' ' && !IN_SET(ch
, '\t', '\n')) ||
66 (0x7F <= ch
&& ch
<= 0x9F);
69 /* count of characters used to encode one unicode char */
70 static int utf8_encoded_expected_len(const char *str
) {
75 c
= (unsigned char) str
[0];
78 if ((c
& 0xe0) == 0xc0)
80 if ((c
& 0xf0) == 0xe0)
82 if ((c
& 0xf8) == 0xf0)
84 if ((c
& 0xfc) == 0xf8)
86 if ((c
& 0xfe) == 0xfc)
92 /* decode one unicode char */
93 int utf8_encoded_to_unichar(const char *str
, char32_t
*ret_unichar
) {
99 len
= utf8_encoded_expected_len(str
);
103 *ret_unichar
= (char32_t
)str
[0];
106 unichar
= str
[0] & 0x1f;
109 unichar
= (char32_t
)str
[0] & 0x0f;
112 unichar
= (char32_t
)str
[0] & 0x07;
115 unichar
= (char32_t
)str
[0] & 0x03;
118 unichar
= (char32_t
)str
[0] & 0x01;
124 for (i
= 1; i
< len
; i
++) {
125 if (((char32_t
)str
[i
] & 0xc0) != 0x80)
128 unichar
|= (char32_t
)str
[i
] & 0x3f;
131 *ret_unichar
= unichar
;
136 bool utf8_is_printable_newline(const char* str
, size_t length
, bool newline
) {
141 for (p
= str
; length
;) {
145 encoded_len
= utf8_encoded_valid_unichar(p
);
146 if (encoded_len
< 0 ||
147 (size_t) encoded_len
> length
)
150 r
= utf8_encoded_to_unichar(p
, &val
);
152 unichar_is_control(val
) ||
153 (!newline
&& val
== '\n'))
156 length
-= encoded_len
;
163 const char *utf8_is_valid(const char *str
) {
168 for (p
= (const uint8_t*) str
; *p
; ) {
171 len
= utf8_encoded_valid_unichar((const char *)p
);
181 char *utf8_escape_invalid(const char *str
) {
186 p
= s
= malloc(strlen(str
) * 4 + 1);
193 len
= utf8_encoded_valid_unichar(str
);
195 s
= mempcpy(s
, str
, len
);
198 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
208 char *utf8_escape_non_printable(const char *str
) {
213 p
= s
= malloc(strlen(str
) * 4 + 1);
220 len
= utf8_encoded_valid_unichar(str
);
222 if (utf8_is_printable(str
, len
)) {
223 s
= mempcpy(s
, str
, len
);
229 *(s
++) = hexchar((int) *str
>> 4);
230 *(s
++) = hexchar((int) *str
);
237 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
247 char *ascii_is_valid(const char *str
) {
250 /* Check whether the string consists of valid ASCII bytes,
251 * i.e values between 0 and 127, inclusive. */
255 for (p
= str
; *p
; p
++)
256 if ((unsigned char) *p
>= 128)
262 char *ascii_is_valid_n(const char *str
, size_t len
) {
265 /* Very similar to ascii_is_valid(), but checks exactly len
266 * bytes and rejects any NULs in that range. */
270 for (i
= 0; i
< len
; i
++)
271 if ((unsigned char) str
[i
] >= 128 || str
[i
] == 0)
278 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
279 * @out_utf8: output buffer of at least 4 bytes or NULL
280 * @g: UCS-4 character to encode
282 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
283 * The length of the character is returned. It is not zero-terminated! If the
284 * output buffer is NULL, only the length is returned.
286 * Returns: The length in bytes that the UTF-8 representation does or would
289 size_t utf8_encode_unichar(char *out_utf8
, char32_t g
) {
293 out_utf8
[0] = g
& 0x7f;
295 } else if (g
< (1 << 11)) {
297 out_utf8
[0] = 0xc0 | ((g
>> 6) & 0x1f);
298 out_utf8
[1] = 0x80 | (g
& 0x3f);
301 } else if (g
< (1 << 16)) {
303 out_utf8
[0] = 0xe0 | ((g
>> 12) & 0x0f);
304 out_utf8
[1] = 0x80 | ((g
>> 6) & 0x3f);
305 out_utf8
[2] = 0x80 | (g
& 0x3f);
308 } else if (g
< (1 << 21)) {
310 out_utf8
[0] = 0xf0 | ((g
>> 18) & 0x07);
311 out_utf8
[1] = 0x80 | ((g
>> 12) & 0x3f);
312 out_utf8
[2] = 0x80 | ((g
>> 6) & 0x3f);
313 out_utf8
[3] = 0x80 | (g
& 0x3f);
321 char *utf16_to_utf8(const void *s
, size_t length
) {
325 r
= new(char, (length
* 4 + 1) / 2 + 1);
332 while (f
< (const uint8_t*) s
+ length
) {
335 /* see RFC 2781 section 2.2 */
337 w1
= f
[1] << 8 | f
[0];
340 if (!utf16_is_surrogate(w1
)) {
341 t
+= utf8_encode_unichar(t
, w1
);
346 if (utf16_is_trailing_surrogate(w1
))
348 else if (f
>= (const uint8_t*) s
+ length
)
351 w2
= f
[1] << 8 | f
[0];
354 if (!utf16_is_trailing_surrogate(w2
)) {
359 t
+= utf8_encode_unichar(t
, utf16_surrogate_pair_to_unichar(w1
, w2
));
366 /* expected size used to encode one unicode char */
367 static int utf8_unichar_to_encoded_len(char32_t unichar
) {
373 if (unichar
< 0x10000)
375 if (unichar
< 0x200000)
377 if (unichar
< 0x4000000)
383 /* validate one encoded unicode char and return its length */
384 int utf8_encoded_valid_unichar(const char *str
) {
390 len
= utf8_encoded_expected_len(str
);
398 /* check if expected encoded chars are available */
399 for (i
= 0; i
< len
; i
++)
400 if ((str
[i
] & 0x80) != 0x80)
403 r
= utf8_encoded_to_unichar(str
, &unichar
);
407 /* check if encoded length matches encoded value */
408 if (utf8_unichar_to_encoded_len(unichar
) != len
)
411 /* check if value has valid range */
412 if (!unichar_is_valid(unichar
))
418 size_t utf8_n_codepoints(const char *str
) {
421 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
426 k
= utf8_encoded_valid_unichar(str
);
437 size_t utf8_console_width(const char *str
) {
440 /* Returns the approximate width a string will take on screen when printed on a character cell
441 * terminal/console. */
446 if (utf8_encoded_to_unichar(str
, &c
) < 0)
449 str
= utf8_next_char(str
);
451 n
+= unichar_iswide(c
) ? 2 : 1;