]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/utf8.c
utf8: let's update utf16_to_utf8() a bit
[thirdparty/systemd.git] / src / basic / utf8.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 /* Parts of this file are based on the GLIB utf8 validation functions. The
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
12 * modify it under the terms of the GNU Library General Public
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
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
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
24 */
25
26 #include <errno.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "alloc-util.h"
32 #include "gunicode.h"
33 #include "hexdecoct.h"
34 #include "macro.h"
35 #include "utf8.h"
36
37 bool unichar_is_valid(char32_t ch) {
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
51 static bool unichar_is_control(char32_t ch) {
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
59 return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
60 (0x7F <= ch && ch <= 0x9F);
61 }
62
63 /* count of characters used to encode one unicode char */
64 static size_t utf8_encoded_expected_len(const char *str) {
65 uint8_t c;
66
67 assert(str);
68
69 c = (uint8_t) str[0];
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;
82
83 return 0;
84 }
85
86 /* decode one unicode char */
87 int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
88 char32_t unichar;
89 size_t len, i;
90
91 assert(str);
92
93 len = utf8_encoded_expected_len(str);
94
95 switch (len) {
96 case 1:
97 *ret_unichar = (char32_t)str[0];
98 return 0;
99 case 2:
100 unichar = str[0] & 0x1f;
101 break;
102 case 3:
103 unichar = (char32_t)str[0] & 0x0f;
104 break;
105 case 4:
106 unichar = (char32_t)str[0] & 0x07;
107 break;
108 case 5:
109 unichar = (char32_t)str[0] & 0x03;
110 break;
111 case 6:
112 unichar = (char32_t)str[0] & 0x01;
113 break;
114 default:
115 return -EINVAL;
116 }
117
118 for (i = 1; i < len; i++) {
119 if (((char32_t)str[i] & 0xc0) != 0x80)
120 return -EINVAL;
121
122 unichar <<= 6;
123 unichar |= (char32_t)str[i] & 0x3f;
124 }
125
126 *ret_unichar = unichar;
127
128 return 0;
129 }
130
131 bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
132 const char *p;
133
134 assert(str);
135
136 for (p = str; length;) {
137 int encoded_len, r;
138 char32_t val;
139
140 encoded_len = utf8_encoded_valid_unichar(p);
141 if (encoded_len < 0 ||
142 (size_t) encoded_len > length)
143 return false;
144
145 r = utf8_encoded_to_unichar(p, &val);
146 if (r < 0 ||
147 unichar_is_control(val) ||
148 (!newline && val == '\n'))
149 return false;
150
151 length -= encoded_len;
152 p += encoded_len;
153 }
154
155 return true;
156 }
157
158 char *utf8_is_valid(const char *str) {
159 const char *p;
160
161 assert(str);
162
163 p = str;
164 while (*p) {
165 int len;
166
167 len = utf8_encoded_valid_unichar(p);
168 if (len < 0)
169 return NULL;
170
171 p += len;
172 }
173
174 return (char*) str;
175 }
176
177 char *utf8_escape_invalid(const char *str) {
178 char *p, *s;
179
180 assert(str);
181
182 p = s = malloc(strlen(str) * 4 + 1);
183 if (!p)
184 return NULL;
185
186 while (*str) {
187 int len;
188
189 len = utf8_encoded_valid_unichar(str);
190 if (len > 0) {
191 s = mempcpy(s, str, len);
192 str += len;
193 } else {
194 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
195 str += 1;
196 }
197 }
198
199 *s = '\0';
200
201 return p;
202 }
203
204 char *utf8_escape_non_printable(const char *str) {
205 char *p, *s;
206
207 assert(str);
208
209 p = s = malloc(strlen(str) * 4 + 1);
210 if (!p)
211 return NULL;
212
213 while (*str) {
214 int len;
215
216 len = utf8_encoded_valid_unichar(str);
217 if (len > 0) {
218 if (utf8_is_printable(str, len)) {
219 s = mempcpy(s, str, len);
220 str += len;
221 } else {
222 while (len > 0) {
223 *(s++) = '\\';
224 *(s++) = 'x';
225 *(s++) = hexchar((int) *str >> 4);
226 *(s++) = hexchar((int) *str);
227
228 str += 1;
229 len--;
230 }
231 }
232 } else {
233 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
234 str += 1;
235 }
236 }
237
238 *s = '\0';
239
240 return p;
241 }
242
243 char *ascii_is_valid(const char *str) {
244 const char *p;
245
246 /* Check whether the string consists of valid ASCII bytes,
247 * i.e values between 0 and 127, inclusive. */
248
249 assert(str);
250
251 for (p = str; *p; p++)
252 if ((unsigned char) *p >= 128)
253 return NULL;
254
255 return (char*) str;
256 }
257
258 char *ascii_is_valid_n(const char *str, size_t len) {
259 size_t i;
260
261 /* Very similar to ascii_is_valid(), but checks exactly len
262 * bytes and rejects any NULs in that range. */
263
264 assert(str);
265
266 for (i = 0; i < len; i++)
267 if ((unsigned char) str[i] >= 128 || str[i] == 0)
268 return NULL;
269
270 return (char*) str;
271 }
272
273 /**
274 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
275 * @out_utf8: output buffer of at least 4 bytes or NULL
276 * @g: UCS-4 character to encode
277 *
278 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
279 * The length of the character is returned. It is not zero-terminated! If the
280 * output buffer is NULL, only the length is returned.
281 *
282 * Returns: The length in bytes that the UTF-8 representation does or would
283 * occupy.
284 */
285 size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
286
287 if (g < (1 << 7)) {
288 if (out_utf8)
289 out_utf8[0] = g & 0x7f;
290 return 1;
291 } else if (g < (1 << 11)) {
292 if (out_utf8) {
293 out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
294 out_utf8[1] = 0x80 | (g & 0x3f);
295 }
296 return 2;
297 } else if (g < (1 << 16)) {
298 if (out_utf8) {
299 out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
300 out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
301 out_utf8[2] = 0x80 | (g & 0x3f);
302 }
303 return 3;
304 } else if (g < (1 << 21)) {
305 if (out_utf8) {
306 out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
307 out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
308 out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
309 out_utf8[3] = 0x80 | (g & 0x3f);
310 }
311 return 4;
312 }
313
314 return 0;
315 }
316
317 char *utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) {
318 const uint8_t *f;
319 char *r, *t;
320
321 assert(s);
322
323 /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may
324 * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */
325 if (length * 2 < length)
326 return NULL; /* overflow */
327
328 r = new(char, length * 2 + 1);
329 if (!r)
330 return NULL;
331
332 f = (const uint8_t*) s;
333 t = r;
334
335 while (f + 1 < (const uint8_t*) s + length) {
336 char16_t w1, w2;
337
338 /* see RFC 2781 section 2.2 */
339
340 w1 = f[1] << 8 | f[0];
341 f += 2;
342
343 if (!utf16_is_surrogate(w1)) {
344 t += utf8_encode_unichar(t, w1);
345 continue;
346 }
347
348 if (utf16_is_trailing_surrogate(w1))
349 continue; /* spurious trailing surrogate, ignore */
350
351 if (f + 1 >= (const uint8_t*) s + length)
352 break;
353
354 w2 = f[1] << 8 | f[0];
355 f += 2;
356
357 if (!utf16_is_trailing_surrogate(w2)) {
358 f -= 2;
359 continue; /* surrogate missing its trailing surrogate, ignore */
360 }
361
362 t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
363 }
364
365 *t = 0;
366 return r;
367 }
368
369 /* expected size used to encode one unicode char */
370 static int utf8_unichar_to_encoded_len(char32_t unichar) {
371
372 if (unichar < 0x80)
373 return 1;
374 if (unichar < 0x800)
375 return 2;
376 if (unichar < 0x10000)
377 return 3;
378 if (unichar < 0x200000)
379 return 4;
380 if (unichar < 0x4000000)
381 return 5;
382
383 return 6;
384 }
385
386 /* validate one encoded unicode char and return its length */
387 int utf8_encoded_valid_unichar(const char *str) {
388 char32_t unichar;
389 size_t len, i;
390 int r;
391
392 assert(str);
393
394 len = utf8_encoded_expected_len(str);
395 if (len == 0)
396 return -EINVAL;
397
398 /* ascii is valid */
399 if (len == 1)
400 return 1;
401
402 /* check if expected encoded chars are available */
403 for (i = 0; i < len; i++)
404 if ((str[i] & 0x80) != 0x80)
405 return -EINVAL;
406
407 r = utf8_encoded_to_unichar(str, &unichar);
408 if (r < 0)
409 return r;
410
411 /* check if encoded length matches encoded value */
412 if (utf8_unichar_to_encoded_len(unichar) != (int) len)
413 return -EINVAL;
414
415 /* check if value has valid range */
416 if (!unichar_is_valid(unichar))
417 return -EINVAL;
418
419 return (int) len;
420 }
421
422 size_t utf8_n_codepoints(const char *str) {
423 size_t n = 0;
424
425 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
426
427 while (*str != 0) {
428 int k;
429
430 k = utf8_encoded_valid_unichar(str);
431 if (k < 0)
432 return (size_t) -1;
433
434 str += k;
435 n++;
436 }
437
438 return n;
439 }
440
441 size_t utf8_console_width(const char *str) {
442 size_t n = 0;
443
444 /* Returns the approximate width a string will take on screen when printed on a character cell
445 * terminal/console. */
446
447 while (*str != 0) {
448 char32_t c;
449
450 if (utf8_encoded_to_unichar(str, &c) < 0)
451 return (size_t) -1;
452
453 str = utf8_next_char(str);
454
455 n += unichar_iswide(c) ? 2 : 1;
456 }
457
458 return n;
459 }