]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/utf8.c
tree-wide: drop license boilerplate
[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"
e4e73a63 38#include "hexdecoct.h"
11c3a366 39#include "macro.h"
7f110ff9
LP
40#include "utf8.h"
41
c932fb71 42bool unichar_is_valid(char32_t ch) {
7f110ff9
LP
43
44 if (ch >= 0x110000) /* End of unicode space */
45 return false;
46 if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
47 return false;
48 if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
49 return false;
50 if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
51 return false;
52
53 return true;
54}
55
c932fb71 56static bool unichar_is_control(char32_t ch) {
ba961854
ZJS
57
58 /*
59 0 to ' '-1 is the C0 range.
60 DEL=0x7F, and DEL+1 to 0x9F is C1 range.
61 '\t' is in C0 range, but more or less harmless and commonly used.
62 */
63
4c701096 64 return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
ba961854
ZJS
65 (0x7F <= ch && ch <= 0x9F);
66}
67
7991ac34
DR
68/* count of characters used to encode one unicode char */
69static int utf8_encoded_expected_len(const char *str) {
7e8185ef 70 unsigned char c;
ba961854 71
7e8185ef
LP
72 assert(str);
73
74 c = (unsigned char) str[0];
7991ac34
DR
75 if (c < 0x80)
76 return 1;
77 if ((c & 0xe0) == 0xc0)
78 return 2;
79 if ((c & 0xf0) == 0xe0)
80 return 3;
81 if ((c & 0xf8) == 0xf0)
82 return 4;
83 if ((c & 0xfc) == 0xf8)
84 return 5;
85 if ((c & 0xfe) == 0xfc)
86 return 6;
7e8185ef 87
7991ac34
DR
88 return 0;
89}
ba961854 90
7991ac34 91/* decode one unicode char */
c932fb71
SL
92int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
93 char32_t unichar;
94 int len, i;
7e8185ef
LP
95
96 assert(str);
ba961854 97
7991ac34 98 len = utf8_encoded_expected_len(str);
7e8185ef 99
7991ac34
DR
100 switch (len) {
101 case 1:
c932fb71
SL
102 *ret_unichar = (char32_t)str[0];
103 return 0;
7991ac34
DR
104 case 2:
105 unichar = str[0] & 0x1f;
106 break;
107 case 3:
c932fb71 108 unichar = (char32_t)str[0] & 0x0f;
7991ac34
DR
109 break;
110 case 4:
c932fb71 111 unichar = (char32_t)str[0] & 0x07;
7991ac34
DR
112 break;
113 case 5:
c932fb71 114 unichar = (char32_t)str[0] & 0x03;
7991ac34
DR
115 break;
116 case 6:
c932fb71 117 unichar = (char32_t)str[0] & 0x01;
7991ac34
DR
118 break;
119 default:
7e8185ef 120 return -EINVAL;
ba961854
ZJS
121 }
122
7991ac34 123 for (i = 1; i < len; i++) {
c932fb71 124 if (((char32_t)str[i] & 0xc0) != 0x80)
7e8185ef 125 return -EINVAL;
7991ac34 126 unichar <<= 6;
c932fb71 127 unichar |= (char32_t)str[i] & 0x3f;
7991ac34
DR
128 }
129
c932fb71
SL
130 *ret_unichar = unichar;
131
132 return 0;
ba961854
ZJS
133}
134
0ade5ffe 135bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
6ed62be0 136 const char *p;
7f110ff9
LP
137
138 assert(str);
139
6ed62be0 140 for (p = str; length;) {
c932fb71
SL
141 int encoded_len, r;
142 char32_t val;
7e8185ef 143
6ed62be0 144 encoded_len = utf8_encoded_valid_unichar(p);
7e8185ef 145 if (encoded_len < 0 ||
144b3d9e
LP
146 (size_t) encoded_len > length)
147 return false;
148
c932fb71
SL
149 r = utf8_encoded_to_unichar(p, &val);
150 if (r < 0 ||
f3ee6297 151 unichar_is_control(val) ||
0ade5ffe 152 (!newline && val == '\n'))
7991ac34 153 return false;
7f110ff9 154
7991ac34 155 length -= encoded_len;
a7176505 156 p += encoded_len;
7f110ff9
LP
157 }
158
7991ac34 159 return true;
7f110ff9
LP
160}
161
7991ac34
DR
162const char *utf8_is_valid(const char *str) {
163 const uint8_t *p;
7f110ff9
LP
164
165 assert(str);
166
7991ac34 167 for (p = (const uint8_t*) str; *p; ) {
faaa5728
LP
168 int len;
169
170 len = utf8_encoded_valid_unichar((const char *)p);
7991ac34
DR
171 if (len < 0)
172 return NULL;
173
174 p += len;
175 }
7f110ff9 176
7991ac34 177 return str;
7f110ff9
LP
178}
179
550a40ec
ZJS
180char *utf8_escape_invalid(const char *str) {
181 char *p, *s;
182
183 assert(str);
184
185 p = s = malloc(strlen(str) * 4 + 1);
186 if (!p)
187 return NULL;
188
189 while (*str) {
190 int len;
191
192 len = utf8_encoded_valid_unichar(str);
193 if (len > 0) {
194 s = mempcpy(s, str, len);
195 str += len;
196 } else {
3c6d3052 197 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
550a40ec
ZJS
198 str += 1;
199 }
200 }
7e8185ef 201
550a40ec
ZJS
202 *s = '\0';
203
204 return p;
205}
206
fec84576
WC
207char *utf8_escape_non_printable(const char *str) {
208 char *p, *s;
209
210 assert(str);
211
212 p = s = malloc(strlen(str) * 4 + 1);
213 if (!p)
214 return NULL;
215
216 while (*str) {
217 int len;
218
219 len = utf8_encoded_valid_unichar(str);
220 if (len > 0) {
221 if (utf8_is_printable(str, len)) {
222 s = mempcpy(s, str, len);
223 str += len;
224 } else {
3c6d3052 225 while (len > 0) {
fec84576
WC
226 *(s++) = '\\';
227 *(s++) = 'x';
228 *(s++) = hexchar((int) *str >> 4);
229 *(s++) = hexchar((int) *str);
fec84576 230
3c6d3052 231 str += 1;
313cefa1 232 len--;
3c6d3052 233 }
fec84576
WC
234 }
235 } else {
3c6d3052 236 s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
fec84576
WC
237 str += 1;
238 }
239 }
240
241 *s = '\0';
242
243 return p;
244}
245
7f110ff9
LP
246char *ascii_is_valid(const char *str) {
247 const char *p;
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
2bb4c7e3
TG
258/**
259 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
260 * @out_utf8: output buffer of at least 4 bytes or NULL
261 * @g: UCS-4 character to encode
262 *
263 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
264 * The length of the character is returned. It is not zero-terminated! If the
265 * output buffer is NULL, only the length is returned.
266 *
267 * Returns: The length in bytes that the UTF-8 representation does or would
268 * occupy.
269 */
c932fb71 270size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
f3ee6297 271
2bb4c7e3
TG
272 if (g < (1 << 7)) {
273 if (out_utf8)
274 out_utf8[0] = g & 0x7f;
e7eebcfc 275 return 1;
2bb4c7e3
TG
276 } else if (g < (1 << 11)) {
277 if (out_utf8) {
278 out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
279 out_utf8[1] = 0x80 | (g & 0x3f);
280 }
e7eebcfc 281 return 2;
2bb4c7e3
TG
282 } else if (g < (1 << 16)) {
283 if (out_utf8) {
284 out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
285 out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
286 out_utf8[2] = 0x80 | (g & 0x3f);
287 }
e7eebcfc 288 return 3;
2bb4c7e3
TG
289 } else if (g < (1 << 21)) {
290 if (out_utf8) {
291 out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
292 out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
293 out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
294 out_utf8[3] = 0x80 | (g & 0x3f);
295 }
296 return 4;
e7eebcfc 297 }
f3ee6297
LP
298
299 return 0;
e7eebcfc
LP
300}
301
2e3d0692 302char *utf16_to_utf8(const void *s, size_t length) {
2e3d0692 303 const uint8_t *f;
e7eebcfc 304 char *r, *t;
2e3d0692 305
04166cb7 306 r = new(char, (length * 4 + 1) / 2 + 1);
2e3d0692
LP
307 if (!r)
308 return NULL;
309
04166cb7
TG
310 f = s;
311 t = r;
312
313 while (f < (const uint8_t*) s + length) {
c932fb71 314 char16_t w1, w2;
04166cb7
TG
315
316 /* see RFC 2781 section 2.2 */
317
318 w1 = f[1] << 8 | f[0];
319 f += 2;
320
321 if (!utf16_is_surrogate(w1)) {
dcd12626 322 t += utf8_encode_unichar(t, w1);
04166cb7
TG
323
324 continue;
325 }
326
327 if (utf16_is_trailing_surrogate(w1))
328 continue;
329 else if (f >= (const uint8_t*) s + length)
330 break;
331
332 w2 = f[1] << 8 | f[0];
333 f += 2;
334
335 if (!utf16_is_trailing_surrogate(w2)) {
336 f -= 2;
337 continue;
338 }
339
340 t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
341 }
2e3d0692
LP
342
343 *t = 0;
7b4d7cc0 344 return r;
2e3d0692 345}
02a36bc9 346
02a36bc9 347/* expected size used to encode one unicode char */
c932fb71 348static int utf8_unichar_to_encoded_len(char32_t unichar) {
7e8185ef 349
02a36bc9
DR
350 if (unichar < 0x80)
351 return 1;
352 if (unichar < 0x800)
353 return 2;
354 if (unichar < 0x10000)
355 return 3;
356 if (unichar < 0x200000)
357 return 4;
358 if (unichar < 0x4000000)
359 return 5;
7e8185ef 360
02a36bc9
DR
361 return 6;
362}
363
364/* validate one encoded unicode char and return its length */
365int utf8_encoded_valid_unichar(const char *str) {
c932fb71
SL
366 int len, i, r;
367 char32_t unichar;
7e8185ef
LP
368
369 assert(str);
02a36bc9
DR
370
371 len = utf8_encoded_expected_len(str);
372 if (len == 0)
7e8185ef 373 return -EINVAL;
02a36bc9
DR
374
375 /* ascii is valid */
376 if (len == 1)
377 return 1;
378
379 /* check if expected encoded chars are available */
380 for (i = 0; i < len; i++)
381 if ((str[i] & 0x80) != 0x80)
7e8185ef 382 return -EINVAL;
02a36bc9 383
c932fb71
SL
384 r = utf8_encoded_to_unichar(str, &unichar);
385 if (r < 0)
386 return r;
02a36bc9
DR
387
388 /* check if encoded length matches encoded value */
389 if (utf8_unichar_to_encoded_len(unichar) != len)
7e8185ef 390 return -EINVAL;
02a36bc9
DR
391
392 /* check if value has valid range */
f3ee6297 393 if (!unichar_is_valid(unichar))
7e8185ef 394 return -EINVAL;
02a36bc9
DR
395
396 return len;
397}
65ee8660
LP
398
399size_t utf8_n_codepoints(const char *str) {
400 size_t n = 0;
401
402 /* Returns the number of UTF-8 codepoints in this string, or (size_t) -1 if the string is not valid UTF-8. */
403
404 while (*str != 0) {
405 int k;
406
407 k = utf8_encoded_valid_unichar(str);
408 if (k < 0)
409 return (size_t) -1;
410
411 str += k;
412 n++;
413 }
414
415 return n;
416}