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