2 This file is part of systemd.
4 Copyright 2008-2011 Kay Sievers
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 /* Parts of this file are based on the GLIB utf8 validation functions. The
22 * original license text follows. */
24 /* gutf8.c - Operations on UTF-8 strings.
26 * Copyright (C) 1999 Tom Tromey
27 * Copyright (C) 2000 Red Hat, Inc.
29 * This library is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Library General Public
31 * License as published by the Free Software Foundation; either
32 * version 2 of the License, or (at your option) any later version.
34 * This library is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * Library General Public License for more details.
39 * You should have received a copy of the GNU Library General Public
40 * License along with this library; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
49 #include "alloc-util.h"
50 #include "hexdecoct.h"
54 bool unichar_is_valid(char32_t ch
) {
56 if (ch
>= 0x110000) /* End of unicode space */
58 if ((ch
& 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
60 if ((ch
>= 0xFDD0) && (ch
<= 0xFDEF)) /* Reserved */
62 if ((ch
& 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
68 static bool unichar_is_control(char32_t ch
) {
71 0 to ' '-1 is the C0 range.
72 DEL=0x7F, and DEL+1 to 0x9F is C1 range.
73 '\t' is in C0 range, but more or less harmless and commonly used.
76 return (ch
< ' ' && ch
!= '\t' && ch
!= '\n') ||
77 (0x7F <= ch
&& ch
<= 0x9F);
80 /* count of characters used to encode one unicode char */
81 static int utf8_encoded_expected_len(const char *str
) {
86 c
= (unsigned char) str
[0];
89 if ((c
& 0xe0) == 0xc0)
91 if ((c
& 0xf0) == 0xe0)
93 if ((c
& 0xf8) == 0xf0)
95 if ((c
& 0xfc) == 0xf8)
97 if ((c
& 0xfe) == 0xfc)
103 /* decode one unicode char */
104 int utf8_encoded_to_unichar(const char *str
, char32_t
*ret_unichar
) {
110 len
= utf8_encoded_expected_len(str
);
114 *ret_unichar
= (char32_t
)str
[0];
117 unichar
= str
[0] & 0x1f;
120 unichar
= (char32_t
)str
[0] & 0x0f;
123 unichar
= (char32_t
)str
[0] & 0x07;
126 unichar
= (char32_t
)str
[0] & 0x03;
129 unichar
= (char32_t
)str
[0] & 0x01;
135 for (i
= 1; i
< len
; i
++) {
136 if (((char32_t
)str
[i
] & 0xc0) != 0x80)
139 unichar
|= (char32_t
)str
[i
] & 0x3f;
142 *ret_unichar
= unichar
;
147 bool utf8_is_printable_newline(const char* str
, size_t length
, bool newline
) {
152 for (p
= str
; length
;) {
156 encoded_len
= utf8_encoded_valid_unichar(p
);
157 if (encoded_len
< 0 ||
158 (size_t) encoded_len
> length
)
161 r
= utf8_encoded_to_unichar(p
, &val
);
163 unichar_is_control(val
) ||
164 (!newline
&& val
== '\n'))
167 length
-= encoded_len
;
174 const char *utf8_is_valid(const char *str
) {
179 for (p
= (const uint8_t*) str
; *p
; ) {
182 len
= utf8_encoded_valid_unichar((const char *)p
);
192 char *utf8_escape_invalid(const char *str
) {
197 p
= s
= malloc(strlen(str
) * 4 + 1);
204 len
= utf8_encoded_valid_unichar(str
);
206 s
= mempcpy(s
, str
, len
);
209 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
219 char *utf8_escape_non_printable(const char *str
) {
224 p
= s
= malloc(strlen(str
) * 4 + 1);
231 len
= utf8_encoded_valid_unichar(str
);
233 if (utf8_is_printable(str
, len
)) {
234 s
= mempcpy(s
, str
, len
);
240 *(s
++) = hexchar((int) *str
>> 4);
241 *(s
++) = hexchar((int) *str
);
248 s
= stpcpy(s
, UTF8_REPLACEMENT_CHARACTER
);
258 char *ascii_is_valid(const char *str
) {
263 for (p
= str
; *p
; p
++)
264 if ((unsigned char) *p
>= 128)
271 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
272 * @out_utf8: output buffer of at least 4 bytes or NULL
273 * @g: UCS-4 character to encode
275 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
276 * The length of the character is returned. It is not zero-terminated! If the
277 * output buffer is NULL, only the length is returned.
279 * Returns: The length in bytes that the UTF-8 representation does or would
282 size_t utf8_encode_unichar(char *out_utf8
, char32_t g
) {
286 out_utf8
[0] = g
& 0x7f;
288 } else if (g
< (1 << 11)) {
290 out_utf8
[0] = 0xc0 | ((g
>> 6) & 0x1f);
291 out_utf8
[1] = 0x80 | (g
& 0x3f);
294 } else if (g
< (1 << 16)) {
296 out_utf8
[0] = 0xe0 | ((g
>> 12) & 0x0f);
297 out_utf8
[1] = 0x80 | ((g
>> 6) & 0x3f);
298 out_utf8
[2] = 0x80 | (g
& 0x3f);
301 } else if (g
< (1 << 21)) {
303 out_utf8
[0] = 0xf0 | ((g
>> 18) & 0x07);
304 out_utf8
[1] = 0x80 | ((g
>> 12) & 0x3f);
305 out_utf8
[2] = 0x80 | ((g
>> 6) & 0x3f);
306 out_utf8
[3] = 0x80 | (g
& 0x3f);
314 char *utf16_to_utf8(const void *s
, size_t length
) {
318 r
= new(char, (length
* 4 + 1) / 2 + 1);
325 while (f
< (const uint8_t*) s
+ length
) {
328 /* see RFC 2781 section 2.2 */
330 w1
= f
[1] << 8 | f
[0];
333 if (!utf16_is_surrogate(w1
)) {
334 t
+= utf8_encode_unichar(t
, w1
);
339 if (utf16_is_trailing_surrogate(w1
))
341 else if (f
>= (const uint8_t*) s
+ length
)
344 w2
= f
[1] << 8 | f
[0];
347 if (!utf16_is_trailing_surrogate(w2
)) {
352 t
+= utf8_encode_unichar(t
, utf16_surrogate_pair_to_unichar(w1
, w2
));
359 /* expected size used to encode one unicode char */
360 static int utf8_unichar_to_encoded_len(char32_t unichar
) {
366 if (unichar
< 0x10000)
368 if (unichar
< 0x200000)
370 if (unichar
< 0x4000000)
376 /* validate one encoded unicode char and return its length */
377 int utf8_encoded_valid_unichar(const char *str
) {
383 len
= utf8_encoded_expected_len(str
);
391 /* check if expected encoded chars are available */
392 for (i
= 0; i
< len
; i
++)
393 if ((str
[i
] & 0x80) != 0x80)
396 r
= utf8_encoded_to_unichar(str
, &unichar
);
400 /* check if encoded length matches encoded value */
401 if (utf8_unichar_to_encoded_len(unichar
) != len
)
404 /* check if value has valid range */
405 if (!unichar_is_valid(unichar
))