]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/utf8.c
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / utf8.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7f110ff9
LP
2/***
3 This file is part of systemd.
4
036ae95a 5 Copyright 2008-2011 Kay Sievers
7f110ff9 6 Copyright 2012 Lennart Poettering
7f110ff9
LP
7***/
8
036ae95a 9/* Parts of this file are based on the GLIB utf8 validation functions. The
7f110ff9
LP
10 * original license text follows. */
11
12/* gutf8.c - Operations on UTF-8 strings.
13 *
14 * Copyright (C) 1999 Tom Tromey
15 * Copyright (C) 2000 Red Hat, Inc.
16 *
17 * This library is free software; you can redistribute it and/or
23757887 18 * modify it under the terms of the GNU Library General Public
7f110ff9
LP
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23757887
SK
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
7f110ff9 26 *
23757887
SK
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
7f110ff9
LP
30 */
31
32#include <errno.h>
7f110ff9 33#include <stdbool.h>
cf0fbc49
TA
34#include <stdlib.h>
35#include <string.h>
7f110ff9 36
b5efdb8a 37#include "alloc-util.h"
3f536d5b 38#include "gunicode.h"
e4e73a63 39#include "hexdecoct.h"
11c3a366 40#include "macro.h"
7f110ff9
LP
41#include "utf8.h"
42
c932fb71 43bool unichar_is_valid(char32_t ch) {
7f110ff9
LP
44
45 if (ch >= 0x110000) /* End of unicode space */
46 return false;
47 if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
48 return false;
49 if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
50 return false;
51 if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
52 return false;
53
54 return true;
55}
56
c932fb71 57static bool unichar_is_control(char32_t ch) {
ba961854
ZJS
58
59 /*
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.
63 */
64
4c701096 65 return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
ba961854
ZJS
66 (0x7F <= ch && ch <= 0x9F);
67}
68
7991ac34
DR
69/* count of characters used to encode one unicode char */
70static int utf8_encoded_expected_len(const char *str) {
7e8185ef 71 unsigned char c;
ba961854 72
7e8185ef
LP
73 assert(str);
74
75 c = (unsigned char) str[0];
7991ac34
DR
76 if (c < 0x80)
77 return 1;
78 if ((c & 0xe0) == 0xc0)
79 return 2;
80 if ((c & 0xf0) == 0xe0)
81 return 3;
82 if ((c & 0xf8) == 0xf0)
83 return 4;
84 if ((c & 0xfc) == 0xf8)
85 return 5;
86 if ((c & 0xfe) == 0xfc)
87 return 6;
7e8185ef 88
7991ac34
DR
89 return 0;
90}
ba961854 91
7991ac34 92/* decode one unicode char */
c932fb71
SL
93int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
94 char32_t unichar;
95 int len, i;
7e8185ef
LP
96
97 assert(str);
ba961854 98
7991ac34 99 len = utf8_encoded_expected_len(str);
7e8185ef 100
7991ac34
DR
101 switch (len) {
102 case 1:
c932fb71
SL
103 *ret_unichar = (char32_t)str[0];
104 return 0;
7991ac34
DR
105 case 2:
106 unichar = str[0] & 0x1f;
107 break;
108 case 3:
c932fb71 109 unichar = (char32_t)str[0] & 0x0f;
7991ac34
DR
110 break;
111 case 4:
c932fb71 112 unichar = (char32_t)str[0] & 0x07;
7991ac34
DR
113 break;
114 case 5:
c932fb71 115 unichar = (char32_t)str[0] & 0x03;
7991ac34
DR
116 break;
117 case 6:
c932fb71 118 unichar = (char32_t)str[0] & 0x01;
7991ac34
DR
119 break;
120 default:
7e8185ef 121 return -EINVAL;
ba961854
ZJS
122 }
123
7991ac34 124 for (i = 1; i < len; i++) {
c932fb71 125 if (((char32_t)str[i] & 0xc0) != 0x80)
7e8185ef 126 return -EINVAL;
7991ac34 127 unichar <<= 6;
c932fb71 128 unichar |= (char32_t)str[i] & 0x3f;
7991ac34
DR
129 }
130
c932fb71
SL
131 *ret_unichar = unichar;
132
133 return 0;
ba961854
ZJS
134}
135
0ade5ffe 136bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
6ed62be0 137 const char *p;
7f110ff9
LP
138
139 assert(str);
140
6ed62be0 141 for (p = str; length;) {
c932fb71
SL
142 int encoded_len, r;
143 char32_t val;
7e8185ef 144
6ed62be0 145 encoded_len = utf8_encoded_valid_unichar(p);
7e8185ef 146 if (encoded_len < 0 ||
144b3d9e
LP
147 (size_t) encoded_len > length)
148 return false;
149
c932fb71
SL
150 r = utf8_encoded_to_unichar(p, &val);
151 if (r < 0 ||
f3ee6297 152 unichar_is_control(val) ||
0ade5ffe 153 (!newline && val == '\n'))
7991ac34 154 return false;
7f110ff9 155
7991ac34 156 length -= encoded_len;
a7176505 157 p += encoded_len;
7f110ff9
LP
158 }
159
7991ac34 160 return true;
7f110ff9
LP
161}
162
7991ac34
DR
163const char *utf8_is_valid(const char *str) {
164 const uint8_t *p;
7f110ff9
LP
165
166 assert(str);
167
7991ac34 168 for (p = (const uint8_t*) str; *p; ) {
faaa5728
LP
169 int len;
170
171 len = utf8_encoded_valid_unichar((const char *)p);
7991ac34
DR
172 if (len < 0)
173 return NULL;
174
175 p += len;
176 }
7f110ff9 177
7991ac34 178 return str;
7f110ff9
LP
179}
180
550a40ec
ZJS
181char *utf8_escape_invalid(const char *str) {
182 char *p, *s;
183
184 assert(str);
185
186 p = s = malloc(strlen(str) * 4 + 1);
187 if (!p)
188 return NULL;
189
190 while (*str) {
191 int len;
192
193 len = utf8_encoded_valid_unichar(str);
194 if (len > 0) {
195 s = mempcpy(s, str, len);
196 str += len;
197 } else {
3c6d3052 198 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
550a40ec
ZJS
199 str += 1;
200 }
201 }
7e8185ef 202
550a40ec
ZJS
203 *s = '\0';
204
205 return p;
206}
207
fec84576
WC
208char *utf8_escape_non_printable(const char *str) {
209 char *p, *s;
210
211 assert(str);
212
213 p = s = malloc(strlen(str) * 4 + 1);
214 if (!p)
215 return NULL;
216
217 while (*str) {
218 int len;
219
220 len = utf8_encoded_valid_unichar(str);
221 if (len > 0) {
222 if (utf8_is_printable(str, len)) {
223 s = mempcpy(s, str, len);
224 str += len;
225 } else {
3c6d3052 226 while (len > 0) {
fec84576
WC
227 *(s++) = '\\';
228 *(s++) = 'x';
229 *(s++) = hexchar((int) *str >> 4);
230 *(s++) = hexchar((int) *str);
fec84576 231
3c6d3052 232 str += 1;
313cefa1 233 len--;
3c6d3052 234 }
fec84576
WC
235 }
236 } else {
3c6d3052 237 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
fec84576
WC
238 str += 1;
239 }
240 }
241
242 *s = '\0';
243
244 return p;
245}
246
7f110ff9
LP
247char *ascii_is_valid(const char *str) {
248 const char *p;
249
250 assert(str);
251
252 for (p = str; *p; p++)
253 if ((unsigned char) *p >= 128)
254 return NULL;
255
256 return (char*) str;
257}
258
2bb4c7e3
TG
259/**
260 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
261 * @out_utf8: output buffer of at least 4 bytes or NULL
262 * @g: UCS-4 character to encode
263 *
264 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
265 * The length of the character is returned. It is not zero-terminated! If the
266 * output buffer is NULL, only the length is returned.
267 *
268 * Returns: The length in bytes that the UTF-8 representation does or would
269 * occupy.
270 */
c932fb71 271size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
f3ee6297 272
2bb4c7e3
TG
273 if (g < (1 << 7)) {
274 if (out_utf8)
275 out_utf8[0] = g & 0x7f;
e7eebcfc 276 return 1;
2bb4c7e3
TG
277 } else if (g < (1 << 11)) {
278 if (out_utf8) {
279 out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
280 out_utf8[1] = 0x80 | (g & 0x3f);
281 }
e7eebcfc 282 return 2;
2bb4c7e3
TG
283 } else if (g < (1 << 16)) {
284 if (out_utf8) {
285 out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
286 out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
287 out_utf8[2] = 0x80 | (g & 0x3f);
288 }
e7eebcfc 289 return 3;
2bb4c7e3
TG
290 } else if (g < (1 << 21)) {
291 if (out_utf8) {
292 out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
293 out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
294 out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
295 out_utf8[3] = 0x80 | (g & 0x3f);
296 }
297 return 4;
e7eebcfc 298 }
f3ee6297
LP
299
300 return 0;
e7eebcfc
LP
301}
302
2e3d0692 303char *utf16_to_utf8(const void *s, size_t length) {
2e3d0692 304 const uint8_t *f;
e7eebcfc 305 char *r, *t;
2e3d0692 306
04166cb7 307 r = new(char, (length * 4 + 1) / 2 + 1);
2e3d0692
LP
308 if (!r)
309 return NULL;
310
04166cb7
TG
311 f = s;
312 t = r;
313
314 while (f < (const uint8_t*) s + length) {
c932fb71 315 char16_t w1, w2;
04166cb7
TG
316
317 /* see RFC 2781 section 2.2 */
318
319 w1 = f[1] << 8 | f[0];
320 f += 2;
321
322 if (!utf16_is_surrogate(w1)) {
dcd12626 323 t += utf8_encode_unichar(t, w1);
04166cb7
TG
324
325 continue;
326 }
327
328 if (utf16_is_trailing_surrogate(w1))
329 continue;
330 else if (f >= (const uint8_t*) s + length)
331 break;
332
333 w2 = f[1] << 8 | f[0];
334 f += 2;
335
336 if (!utf16_is_trailing_surrogate(w2)) {
337 f -= 2;
338 continue;
339 }
340
341 t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
342 }
2e3d0692
LP
343
344 *t = 0;
7b4d7cc0 345 return r;
2e3d0692 346}
02a36bc9 347
02a36bc9 348/* expected size used to encode one unicode char */
c932fb71 349static int utf8_unichar_to_encoded_len(char32_t unichar) {
7e8185ef 350
02a36bc9
DR
351 if (unichar < 0x80)
352 return 1;
353 if (unichar < 0x800)
354 return 2;
355 if (unichar < 0x10000)
356 return 3;
357 if (unichar < 0x200000)
358 return 4;
359 if (unichar < 0x4000000)
360 return 5;
7e8185ef 361
02a36bc9
DR
362 return 6;
363}
364
365/* validate one encoded unicode char and return its length */
366int utf8_encoded_valid_unichar(const char *str) {
c932fb71
SL
367 int len, i, r;
368 char32_t unichar;
7e8185ef
LP
369
370 assert(str);
02a36bc9
DR
371
372 len = utf8_encoded_expected_len(str);
373 if (len == 0)
7e8185ef 374 return -EINVAL;
02a36bc9
DR
375
376 /* ascii is valid */
377 if (len == 1)
378 return 1;
379
380 /* check if expected encoded chars are available */
381 for (i = 0; i < len; i++)
382 if ((str[i] & 0x80) != 0x80)
7e8185ef 383 return -EINVAL;
02a36bc9 384
c932fb71
SL
385 r = utf8_encoded_to_unichar(str, &unichar);
386 if (r < 0)
387 return r;
02a36bc9
DR
388
389 /* check if encoded length matches encoded value */
390 if (utf8_unichar_to_encoded_len(unichar) != len)
7e8185ef 391 return -EINVAL;
02a36bc9
DR
392
393 /* check if value has valid range */
f3ee6297 394 if (!unichar_is_valid(unichar))
7e8185ef 395 return -EINVAL;
02a36bc9
DR
396
397 return len;
398}
65ee8660
LP
399
400size_t utf8_n_codepoints(const char *str) {
401 size_t n = 0;
402
403 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
404
405 while (*str != 0) {
406 int k;
407
408 k = utf8_encoded_valid_unichar(str);
409 if (k < 0)
410 return (size_t) -1;
411
412 str += k;
413 n++;
414 }
415
416 return n;
417}
3f536d5b
LP
418
419size_t utf8_console_width(const char *str) {
420 size_t n = 0;
421
422 /* Returns the approximate width a string will take on screen when printed on a character cell
423 * terminal/console. */
424
425 while (*str != 0) {
426 char32_t c;
427
428 if (utf8_encoded_to_unichar(str, &c) < 0)
429 return (size_t) -1;
430
431 str = utf8_next_char(str);
432
433 n += unichar_iswide(c) ? 2 : 1;
434 }
435
436 return n;
437}