]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/utf8.c
Merge pull request #8863 from evelikov/shell-completion-fixes
[thirdparty/systemd.git] / src / basic / utf8.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2008-2011 Kay Sievers
6 Copyright 2012 Lennart Poettering
7 ***/
8
9 /* Parts of this file are based on the GLIB utf8 validation functions. The
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
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.
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
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
26 *
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
30 */
31
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "alloc-util.h"
38 #include "gunicode.h"
39 #include "hexdecoct.h"
40 #include "macro.h"
41 #include "utf8.h"
42
43 bool unichar_is_valid(char32_t ch) {
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
57 static bool unichar_is_control(char32_t ch) {
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
65 return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
66 (0x7F <= ch && ch <= 0x9F);
67 }
68
69 /* count of characters used to encode one unicode char */
70 static int utf8_encoded_expected_len(const char *str) {
71 unsigned char c;
72
73 assert(str);
74
75 c = (unsigned char) str[0];
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;
88
89 return 0;
90 }
91
92 /* decode one unicode char */
93 int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
94 char32_t unichar;
95 int len, i;
96
97 assert(str);
98
99 len = utf8_encoded_expected_len(str);
100
101 switch (len) {
102 case 1:
103 *ret_unichar = (char32_t)str[0];
104 return 0;
105 case 2:
106 unichar = str[0] & 0x1f;
107 break;
108 case 3:
109 unichar = (char32_t)str[0] & 0x0f;
110 break;
111 case 4:
112 unichar = (char32_t)str[0] & 0x07;
113 break;
114 case 5:
115 unichar = (char32_t)str[0] & 0x03;
116 break;
117 case 6:
118 unichar = (char32_t)str[0] & 0x01;
119 break;
120 default:
121 return -EINVAL;
122 }
123
124 for (i = 1; i < len; i++) {
125 if (((char32_t)str[i] & 0xc0) != 0x80)
126 return -EINVAL;
127 unichar <<= 6;
128 unichar |= (char32_t)str[i] & 0x3f;
129 }
130
131 *ret_unichar = unichar;
132
133 return 0;
134 }
135
136 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
137 const char *p;
138
139 assert(str);
140
141 for (p = str; length;) {
142 int encoded_len, r;
143 char32_t val;
144
145 encoded_len = utf8_encoded_valid_unichar(p);
146 if (encoded_len < 0 ||
147 (size_t) encoded_len > length)
148 return false;
149
150 r = utf8_encoded_to_unichar(p, &val);
151 if (r < 0 ||
152 unichar_is_control(val) ||
153 (!newline && val == '\n'))
154 return false;
155
156 length -= encoded_len;
157 p += encoded_len;
158 }
159
160 return true;
161 }
162
163 const char *utf8_is_valid(const char *str) {
164 const uint8_t *p;
165
166 assert(str);
167
168 for (p = (const uint8_t*) str; *p; ) {
169 int len;
170
171 len = utf8_encoded_valid_unichar((const char *)p);
172 if (len < 0)
173 return NULL;
174
175 p += len;
176 }
177
178 return str;
179 }
180
181 char *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 {
198 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
199 str += 1;
200 }
201 }
202
203 *s = '\0';
204
205 return p;
206 }
207
208 char *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 {
226 while (len > 0) {
227 *(s++) = '\\';
228 *(s++) = 'x';
229 *(s++) = hexchar((int) *str >> 4);
230 *(s++) = hexchar((int) *str);
231
232 str += 1;
233 len--;
234 }
235 }
236 } else {
237 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
238 str += 1;
239 }
240 }
241
242 *s = '\0';
243
244 return p;
245 }
246
247 char *ascii_is_valid(const char *str) {
248 const char *p;
249
250 /* Check whether the string consists of valid ASCII bytes,
251 * i.e values between 0 and 127, inclusive. */
252
253 assert(str);
254
255 for (p = str; *p; p++)
256 if ((unsigned char) *p >= 128)
257 return NULL;
258
259 return (char*) str;
260 }
261
262 char *ascii_is_valid_n(const char *str, size_t len) {
263 size_t i;
264
265 /* Very similar to ascii_is_valid(), but checks exactly len
266 * bytes and rejects any NULs in that range. */
267
268 assert(str);
269
270 for (i = 0; i < len; i++)
271 if ((unsigned char) str[i] >= 128 || str[i] == 0)
272 return NULL;
273
274 return (char*) str;
275 }
276
277 /**
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
281 *
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.
285 *
286 * Returns: The length in bytes that the UTF-8 representation does or would
287 * occupy.
288 */
289 size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
290
291 if (g < (1 << 7)) {
292 if (out_utf8)
293 out_utf8[0] = g & 0x7f;
294 return 1;
295 } else if (g < (1 << 11)) {
296 if (out_utf8) {
297 out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
298 out_utf8[1] = 0x80 | (g & 0x3f);
299 }
300 return 2;
301 } else if (g < (1 << 16)) {
302 if (out_utf8) {
303 out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
304 out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
305 out_utf8[2] = 0x80 | (g & 0x3f);
306 }
307 return 3;
308 } else if (g < (1 << 21)) {
309 if (out_utf8) {
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);
314 }
315 return 4;
316 }
317
318 return 0;
319 }
320
321 char *utf16_to_utf8(const void *s, size_t length) {
322 const uint8_t *f;
323 char *r, *t;
324
325 r = new(char, (length * 4 + 1) / 2 + 1);
326 if (!r)
327 return NULL;
328
329 f = s;
330 t = r;
331
332 while (f < (const uint8_t*) s + length) {
333 char16_t w1, w2;
334
335 /* see RFC 2781 section 2.2 */
336
337 w1 = f[1] << 8 | f[0];
338 f += 2;
339
340 if (!utf16_is_surrogate(w1)) {
341 t += utf8_encode_unichar(t, w1);
342
343 continue;
344 }
345
346 if (utf16_is_trailing_surrogate(w1))
347 continue;
348 else if (f >= (const uint8_t*) s + length)
349 break;
350
351 w2 = f[1] << 8 | f[0];
352 f += 2;
353
354 if (!utf16_is_trailing_surrogate(w2)) {
355 f -= 2;
356 continue;
357 }
358
359 t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
360 }
361
362 *t = 0;
363 return r;
364 }
365
366 /* expected size used to encode one unicode char */
367 static int utf8_unichar_to_encoded_len(char32_t unichar) {
368
369 if (unichar < 0x80)
370 return 1;
371 if (unichar < 0x800)
372 return 2;
373 if (unichar < 0x10000)
374 return 3;
375 if (unichar < 0x200000)
376 return 4;
377 if (unichar < 0x4000000)
378 return 5;
379
380 return 6;
381 }
382
383 /* validate one encoded unicode char and return its length */
384 int utf8_encoded_valid_unichar(const char *str) {
385 int len, i, r;
386 char32_t unichar;
387
388 assert(str);
389
390 len = utf8_encoded_expected_len(str);
391 if (len == 0)
392 return -EINVAL;
393
394 /* ascii is valid */
395 if (len == 1)
396 return 1;
397
398 /* check if expected encoded chars are available */
399 for (i = 0; i < len; i++)
400 if ((str[i] & 0x80) != 0x80)
401 return -EINVAL;
402
403 r = utf8_encoded_to_unichar(str, &unichar);
404 if (r < 0)
405 return r;
406
407 /* check if encoded length matches encoded value */
408 if (utf8_unichar_to_encoded_len(unichar) != len)
409 return -EINVAL;
410
411 /* check if value has valid range */
412 if (!unichar_is_valid(unichar))
413 return -EINVAL;
414
415 return len;
416 }
417
418 size_t utf8_n_codepoints(const char *str) {
419 size_t n = 0;
420
421 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
422
423 while (*str != 0) {
424 int k;
425
426 k = utf8_encoded_valid_unichar(str);
427 if (k < 0)
428 return (size_t) -1;
429
430 str += k;
431 n++;
432 }
433
434 return n;
435 }
436
437 size_t utf8_console_width(const char *str) {
438 size_t n = 0;
439
440 /* Returns the approximate width a string will take on screen when printed on a character cell
441 * terminal/console. */
442
443 while (*str != 0) {
444 char32_t c;
445
446 if (utf8_encoded_to_unichar(str, &c) < 0)
447 return (size_t) -1;
448
449 str = utf8_next_char(str);
450
451 n += unichar_iswide(c) ? 2 : 1;
452 }
453
454 return n;
455 }