• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 27728979565

17 Jun 2026 12:11PM UTC coverage: 72.586% (-0.4%) from 73.001%
27728979565

push

github

web-flow
mkosi: fix suse build (#42637)

No longer exists since latest spec changes, as the binaries have moved
to another package so it doesn't get autogenerated anymore

Follow-up for db9d7265f

335884 of 462738 relevant lines covered (72.59%)

1397781.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.58
/src/basic/utf8.c
1
/* SPDX-License-Identifier: LGPL-2.0-or-later */
2

3
/* Parts of this file are based on the GLIB utf8 validation functions. The original copyright follows.
4
 *
5
 * gutf8.c - Operations on UTF-8 strings.
6
 * Copyright (C) 1999 Tom Tromey
7
 * Copyright (C) 2000 Red Hat, Inc.
8
 */
9

10
#include "alloc-util.h"
11
#include "gunicode.h"
12
#include "hexdecoct.h"
13
#include "string-util.h"
14
#include "utf8.h"
15

16
bool unichar_is_valid(char32_t ch) {
97,357✔
17

18
        if (ch >= 0x110000) /* End of unicode space */
97,357✔
19
                return false;
20
        if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
97,357✔
21
                return false;
22
        if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
97,357✔
23
                return false;
24
        if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
97,357✔
25
                return false;
26✔
26

27
        return true;
28
}
29

30
static bool unichar_is_control(char32_t ch) {
120,744,702✔
31

32
        /*
33
          0 to ' '-1 is the C0 range.
34
          DEL=0x7F, and DEL+1 to 0x9F is C1 range.
35
          '\t' is in C0 range, but more or less harmless and commonly used.
36
        */
37

38
        return (ch < ' ' && !IN_SET(ch, '\t', '\n')) ||
120,744,702✔
39
                (0x7F <= ch && ch <= 0x9F);
120,744,521✔
40
}
41

42
/* count of characters used to encode one unicode char */
43
static size_t utf8_encoded_expected_len(uint8_t c) {
1,231,570,602✔
44
        if (c < 0x80)
1,231,570,602✔
45
                return 1;
46
        if ((c & 0xe0) == 0xc0)
311,994✔
47
                return 2;
48
        if ((c & 0xf0) == 0xe0)
284,720✔
49
                return 3;
50
        if ((c & 0xf8) == 0xf0)
751✔
51
                return 4;
52
        if ((c & 0xfc) == 0xf8)
89✔
53
                return 5;
54
        if ((c & 0xfe) == 0xfc)
89✔
55
                return 6;
×
56

57
        return 0;
58
}
59

60
/* decode one unicode char */
61
int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
133,640,148✔
62
        char32_t unichar;
133,640,148✔
63
        size_t len;
133,640,148✔
64

65
        assert(str);
133,640,148✔
66
        assert(ret_unichar);
133,640,148✔
67

68
        len = utf8_encoded_expected_len(str[0]);
133,640,148✔
69

70
        switch (len) {
133,640,148✔
71
        case 1:
133,426,796✔
72
                *ret_unichar = (char32_t)str[0];
133,426,796✔
73
                return 1;
133,426,796✔
74
        case 2:
22,456✔
75
                unichar = str[0] & 0x1f;
22,456✔
76
                break;
22,456✔
77
        case 3:
190,461✔
78
                unichar = (char32_t)str[0] & 0x0f;
190,461✔
79
                break;
190,461✔
80
        case 4:
435✔
81
                unichar = (char32_t)str[0] & 0x07;
435✔
82
                break;
435✔
83
        case 5:
×
84
                unichar = (char32_t)str[0] & 0x03;
×
85
                break;
×
86
        case 6:
×
87
                unichar = (char32_t)str[0] & 0x01;
×
88
                break;
×
89
        default:
90
                return -EINVAL;
91
        }
92

93
        for (size_t i = 1; i < len; i++) {
618,027✔
94
                if (((char32_t)str[i] & 0xc0) != 0x80)
404,681✔
95
                        return -EINVAL;
96

97
                unichar <<= 6;
404,675✔
98
                unichar |= (char32_t)str[i] & 0x3f;
404,675✔
99
        }
100

101
        *ret_unichar = unichar;
213,346✔
102
        return len;
213,346✔
103
}
104

105
bool utf8_is_printable_newline(const char* str, size_t length, bool allow_newline) {
4,337,352✔
106
        assert(str);
4,337,352✔
107

108
        for (const char *p = str; length > 0;) {
125,081,873✔
109
                int encoded_len;
120,744,703✔
110
                char32_t val;
120,744,703✔
111

112
                encoded_len = utf8_encoded_valid_unichar(p, length);
120,744,703✔
113
                if (encoded_len < 0)
120,744,703✔
114
                        return false;
182✔
115
                assert(encoded_len > 0 && (size_t) encoded_len <= length);
120,744,702✔
116

117
                if (utf8_encoded_to_unichar(p, &val) < 0 ||
120,744,702✔
118
                    unichar_is_control(val) ||
120,744,702✔
119
                    (!allow_newline && val == '\n'))
22,006,007✔
120
                        return false;
121

122
                length -= encoded_len;
120,744,521✔
123
                p += encoded_len;
120,744,521✔
124
        }
125

126
        return true;
127
}
128

129
char* utf8_is_valid_n(const char *str, size_t len_bytes) {
55,451,055✔
130
        /* Check if the string is composed of valid utf8 characters. If length len_bytes is given, stop after
131
         * len_bytes. Otherwise, stop at NUL. */
132

133
        assert(str);
55,451,055✔
134

135
        for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) {
982,223,724✔
136
                int len;
926,773,807✔
137

138
                if (_unlikely_(str[i] == '\0'))
926,773,807✔
139
                        return NULL; /* embedded NUL */
140

141
                len = utf8_encoded_valid_unichar(str + i,
926,773,804✔
142
                                                 len_bytes != SIZE_MAX ? len_bytes - i : SIZE_MAX);
143
                if (_unlikely_(len < 0))
926,773,805✔
144
                        return NULL; /* invalid character */
145

146
                i += len;
926,772,669✔
147
        }
148

149
        return (char*) str;
150
}
151

152
char* utf8_escape_invalid(const char *str) {
15,836✔
153
        char *p, *s;
15,836✔
154

155
        assert(str);
15,836✔
156

157
        p = s = malloc(strlen(str) * 4 + 1);
15,836✔
158
        if (!p)
15,836✔
159
                return NULL;
160

161
        while (*str) {
348,172✔
162
                int len;
332,336✔
163

164
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
332,336✔
165
                if (len > 0) {
332,336✔
166
                        s = mempcpy(s, str, len);
332,284✔
167
                        str += len;
332,284✔
168
                } else {
169
                        s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
52✔
170
                        str += 1;
52✔
171
                }
172
        }
173

174
        *s = '\0';
15,836✔
175
        return str_realloc(p);
15,836✔
176
}
177

178
int utf8_char_console_width(const char *str) {
10,245,007✔
179
        char32_t c;
10,245,007✔
180
        int r;
10,245,007✔
181

182
        r = utf8_encoded_to_unichar(str, &c);
10,245,007✔
183
        if (r < 0)
10,245,007✔
184
                return r;
10,245,007✔
185

186
        if (c == '\t')
10,245,006✔
187
                return 8; /* Assume a tab width of 8 */
188

189
        /* TODO: we should detect combining characters */
190

191
        return unichar_iswide(c) ? 2 : 1;
10,191,361✔
192
}
193

194
char* utf8_escape_non_printable_full(const char *str, size_t console_width, bool force_ellipsis) {
4,083✔
195
        char *p, *s, *prev_s;
4,083✔
196
        size_t n = 0; /* estimated print width */
4,083✔
197

198
        assert(str);
4,083✔
199

200
        if (console_width == 0)
4,083✔
201
                return strdup("");
9✔
202

203
        size_t body = strlen(str) * 4;
4,074✔
204
        p = s = prev_s = malloc(body + STRLEN("…") + 1);
4,074✔
205
        if (!p)
4,074✔
206
                return NULL;
207

208
        for (;;) {
395,028✔
209
                int len;
199,551✔
210
                char *saved_s = s;
199,551✔
211

212
                if (!*str) { /* done! */
199,551✔
213
                        if (force_ellipsis)
4,000✔
214
                                goto truncation;
68✔
215
                        else
216
                                goto finish;
3,932✔
217
                }
218

219
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
195,551✔
220
                if (len > 0) {
195,551✔
221
                        if (utf8_is_printable(str, len)) {
195,455✔
222
                                int w;
195,289✔
223

224
                                w = utf8_char_console_width(str);
195,289✔
225
                                assert(w >= 0);
195,289✔
226
                                if (n + w > console_width)
195,289✔
227
                                        goto truncation;
34✔
228

229
                                s = mempcpy(s, str, len);
195,255✔
230
                                str += len;
195,255✔
231
                                n += w;
195,255✔
232

233
                        } else {
234
                                for (; len > 0; len--) {
294✔
235
                                        if (n + 4 > console_width)
166✔
236
                                                goto truncation;
38✔
237

238
                                        *(s++) = '\\';
128✔
239
                                        *(s++) = 'x';
128✔
240
                                        *(s++) = hexchar((int) *str >> 4);
128✔
241
                                        *(s++) = hexchar((int) *str);
128✔
242

243
                                        str += 1;
128✔
244
                                        n += 4;
128✔
245
                                }
246
                        }
247
                } else {
248
                        if (n + 1 > console_width)
96✔
249
                                goto truncation;
2✔
250

251
                        s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
94✔
252
                        str += 1;
94✔
253
                        n += 1;
94✔
254
                }
255

256
                prev_s = saved_s;
195,477✔
257
        }
258

259
 truncation:
142✔
260
        /* Try to go back one if we don't have enough space for the ellipsis */
261
        if (n + 1 > console_width)
142✔
262
                s = prev_s;
82✔
263

264
        s = mempcpy(s, "…", strlen("…"));
142✔
265

266
 finish:
4,074✔
267
        *s = '\0';
4,074✔
268
        return str_realloc(p);
4,074✔
269
}
270

271
char* ascii_is_valid_n(const char *str, size_t len) {
98,846✔
272
        /* Check whether the string consists of valid ASCII bytes, i.e values between 1 and 127, inclusive.
273
         * Stops at len, or NUL byte if len is SIZE_MAX. */
274

275
        assert(str);
98,846✔
276

277
        for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++)
2,445,020✔
278
                if ((unsigned char) str[i] >= 128 || str[i] == '\0')
2,349,580✔
279
                        return NULL;
280

281
        return (char*) str;
282
}
283

284
int utf8_to_ascii(const char *str, char replacement_char, char **ret) {
50✔
285
        /* Convert to a string that has only ASCII chars, replacing anything that is not ASCII
286
         * by replacement_char. */
287

288
        assert(str);
50✔
289
        assert(ret);
50✔
290

291
        _cleanup_free_ char *ans = new(char, strlen(str) + 1);
100✔
292
        if (!ans)
50✔
293
                return -ENOMEM;
294

295
        char *q = ans;
296

297
        for (const char *p = str; *p; q++) {
389✔
298
                int l;
341✔
299

300
                l = utf8_encoded_valid_unichar(p, SIZE_MAX);
341✔
301
                if (l < 0)  /* Non-UTF-8, let's not even try to propagate the garbage */
341✔
302
                        return l;
303

304
                if (l == 1)
339✔
305
                        *q = *p;
322✔
306
                else
307
                        /* non-ASCII, we need to replace it */
308
                        *q = replacement_char;
17✔
309

310
                p += l;
339✔
311
        }
312
        *q = '\0';
48✔
313

314
        *ret = TAKE_PTR(ans);
48✔
315
        return 0;
48✔
316
}
317

318
/**
319
 * utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
320
 * @out_utf8: output buffer of at least 4 bytes or NULL
321
 * @g: UCS-4 character to encode
322
 *
323
 * This encodes a single UCS-4 character as UTF-8 and writes it into @out_utf8.
324
 * The length of the character is returned. It is not zero-terminated! If the
325
 * output buffer is NULL, only the length is returned.
326
 *
327
 * Returns: The length in bytes that the UTF-8 representation does or would
328
 *          occupy.
329
 */
330
size_t utf8_encode_unichar(char *out_utf8, char32_t g) {
81,950✔
331

332
        if (g < (1 << 7)) {
81,950✔
333
                if (out_utf8)
81,826✔
334
                        out_utf8[0] = g & 0x7f;
81,826✔
335
                return 1;
336
        } else if (g < (1 << 11)) {
124✔
337
                if (out_utf8) {
17✔
338
                        out_utf8[0] = 0xc0 | ((g >> 6) & 0x1f);
17✔
339
                        out_utf8[1] = 0x80 | (g & 0x3f);
17✔
340
                }
341
                return 2;
342
        } else if (g < (1 << 16)) {
107✔
343
                if (out_utf8) {
96✔
344
                        out_utf8[0] = 0xe0 | ((g >> 12) & 0x0f);
96✔
345
                        out_utf8[1] = 0x80 | ((g >> 6) & 0x3f);
96✔
346
                        out_utf8[2] = 0x80 | (g & 0x3f);
96✔
347
                }
348
                return 3;
349
        } else if (g < (1 << 21)) {
11✔
350
                if (out_utf8) {
11✔
351
                        out_utf8[0] = 0xf0 | ((g >> 18) & 0x07);
11✔
352
                        out_utf8[1] = 0x80 | ((g >> 12) & 0x3f);
11✔
353
                        out_utf8[2] = 0x80 | ((g >> 6) & 0x3f);
11✔
354
                        out_utf8[3] = 0x80 | (g & 0x3f);
11✔
355
                }
356
                return 4;
357
        }
358

359
        return 0;
360
}
361

362
char* utf16_to_utf8(const char16_t *s, size_t length /* bytes! */) {
2,510✔
363
        const uint8_t *f;
2,510✔
364
        char *r, *t;
2,510✔
365

366
        if (length == 0)
2,510✔
367
                return new0(char, 1);
×
368

369
        assert(s);
2,510✔
370

371
        if (length == SIZE_MAX) {
2,510✔
372
                length = char16_strlen(s);
6✔
373

374
                if (length > SIZE_MAX/2)
6✔
375
                        return NULL; /* overflow */
376

377
                length *= 2;
6✔
378
        }
379

380
        /* Input length is in bytes, i.e. the shortest possible character takes 2 bytes. Each unicode character may
381
         * take up to 4 bytes in UTF-8. Let's also account for a trailing NUL byte. */
382
        if (length > (SIZE_MAX - 1) / 2)
2,510✔
383
                return NULL; /* overflow */
384

385
        r = new(char, length * 2 + 1);
2,510✔
386
        if (!r)
2,510✔
387
                return NULL;
388

389
        f = (const uint8_t*) s;
390
        t = r;
391

392
        while (f + 1 < (const uint8_t*) s + length) {
84,230✔
393
                char16_t w1, w2;
81,720✔
394

395
                /* see RFC 2781 section 2.2 */
396

397
                w1 = f[1] << 8 | f[0];
81,720✔
398
                f += 2;
81,720✔
399

400
                if (!utf16_is_surrogate(w1)) {
81,720✔
401
                        t += utf8_encode_unichar(t, w1);
81,713✔
402
                        continue;
81,713✔
403
                }
404

405
                if (utf16_is_trailing_surrogate(w1))
7✔
406
                        continue; /* spurious trailing surrogate, ignore */
1✔
407

408
                if (f + 1 >= (const uint8_t*) s + length)
6✔
409
                        break;
410

411
                w2 = f[1] << 8 | f[0];
6✔
412
                f += 2;
6✔
413

414
                if (!utf16_is_trailing_surrogate(w2)) {
6✔
415
                        f -= 2;
1✔
416
                        continue; /* surrogate missing its trailing surrogate, ignore */
1✔
417
                }
418

419
                t += utf8_encode_unichar(t, utf16_surrogate_pair_to_unichar(w1, w2));
5✔
420
        }
421

422
        *t = 0;
2,510✔
423
        return r;
2,510✔
424
}
425

426
size_t utf16_encode_unichar(char16_t *out, char32_t c) {
16✔
427

428
        /* Note that this encodes as little-endian. */
429

430
        switch (c) {
16✔
431

432
        case 0 ... 0xd7ffU:
12✔
433
        case 0xe000U ... 0xffffU:
434
                out[0] = htole16(c);
12✔
435
                return 1;
12✔
436

437
        case 0x10000U ... 0x10ffffU:
4✔
438
                c -= 0x10000U;
4✔
439
                out[0] = htole16((c >> 10) + 0xd800U);
4✔
440
                out[1] = htole16((c & 0x3ffU) + 0xdc00U);
4✔
441
                return 2;
4✔
442

443
        default: /* A surrogate (invalid) */
444
                return 0;
445
        }
446
}
447

448
char16_t *utf8_to_utf16(const char *s, size_t length) {
413✔
449
        char16_t *n, *p;
413✔
450
        int r;
413✔
451

452
        if (length == 0)
413✔
453
                return new0(char16_t, 1);
×
454

455
        assert(s);
413✔
456

457
        if (length == SIZE_MAX)
413✔
458
                length = strlen(s);
412✔
459

460
        if (length > SIZE_MAX - 1)
412✔
461
                return NULL; /* overflow */
462

463
        n = new(char16_t, length + 1);
413✔
464
        if (!n)
413✔
465
                return NULL;
466

467
        p = n;
468

469
        for (size_t i = 0; i < length;) {
6,832✔
470
                char32_t unichar;
6,419✔
471
                size_t e;
6,419✔
472

473
                e = utf8_encoded_expected_len(s[i]);
6,419✔
474
                if (e <= 1) /* Invalid and single byte characters are copied as they are */
6,419✔
475
                        goto copy;
6,403✔
476

477
                if (i + e > length) /* sequence longer than input buffer, then copy as-is */
16✔
478
                        goto copy;
×
479

480
                r = utf8_encoded_to_unichar(s + i, &unichar);
16✔
481
                if (r < 0) /* sequence invalid, then copy as-is */
16✔
482
                        goto copy;
×
483

484
                p += utf16_encode_unichar(p, unichar);
16✔
485
                i += e;
16✔
486
                continue;
16✔
487

488
        copy:
6,403✔
489
                *(p++) = htole16(s[i++]);
6,403✔
490
        }
491

492
        *p = 0;
413✔
493
        return n;
413✔
494
}
495

496
size_t char16_strlen(const char16_t *s) {
677✔
497
        size_t n = 0;
677✔
498

499
        assert(s);
677✔
500

501
        while (*s != 0)
10,277✔
502
                n++, s++;
9,600✔
503

504
        return n;
677✔
505
}
506

507
size_t char16_strsize(const char16_t *s) {
3✔
508
        POINTER_MAY_BE_NULL(s);
3✔
509

510
        return s ? (char16_strlen(s) + 1) * sizeof(*s) : 0;
3✔
511
}
512

513
/* expected size used to encode one unicode char */
514
static int utf8_unichar_to_encoded_len(char32_t unichar) {
97,347✔
515

516
        if (unichar < 0x80)
97,347✔
517
                return 1;
518
        if (unichar < 0x800)
97,347✔
519
                return 2;
520
        if (unichar < 0x10000)
92,857✔
521
                return 3;
522
        if (unichar < 0x200000)
221✔
523
                return 4;
524
        if (unichar < 0x4000000)
×
525
                return 5;
×
526

527
        return 6;
528
}
529

530
/* validate one encoded unicode char and return its length */
531
int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
1,097,924,033✔
532
        char32_t unichar;
1,097,924,033✔
533
        size_t len;
1,097,924,033✔
534
        int r;
1,097,924,033✔
535

536
        assert(str);
1,097,924,033✔
537
        assert(length > 0);
1,097,924,033✔
538

539
        /* We read until NUL, at most length bytes. SIZE_MAX may be used to disable the length check. */
540

541
        len = utf8_encoded_expected_len(str[0]);
1,097,924,033✔
542
        if (len == 0)
1,097,924,033✔
543
                return -EINVAL;
1,097,924,033✔
544

545
        /* Do we have a truncated multi-byte character? */
546
        if (len > length)
1,097,923,944✔
547
                return -EINVAL;
548

549
        /* ascii is valid */
550
        if (len == 1)
1,097,923,936✔
551
                return 1;
552

553
        /* check if expected encoded chars are available */
554
        for (size_t i = 0; i < len; i++)
387,947✔
555
                if ((str[i] & 0x80) != 0x80)
290,595✔
556
                        return -EINVAL;
557

558
        r = utf8_encoded_to_unichar(str, &unichar);
97,352✔
559
        if (r < 0)
97,352✔
560
                return r;
561

562
        /* check if encoded length matches encoded value */
563
        if (utf8_unichar_to_encoded_len(unichar) != (int) len)
97,347✔
564
                return -EINVAL;
565

566
        /* check if value has valid range */
567
        if (!unichar_is_valid(unichar))
97,347✔
568
                return -EINVAL;
26✔
569

570
        return (int) len;
571
}
572

573
size_t utf8_n_codepoints(const char *str) {
6✔
574
        size_t n = 0;
6✔
575

576
        assert(str);
6✔
577

578
        /* Returns the number of UTF-8 codepoints in this string, or SIZE_MAX if the string is not valid UTF-8. */
579

580
        while (*str != 0) {
34✔
581
                int k;
29✔
582

583
                k = utf8_encoded_valid_unichar(str, SIZE_MAX);
29✔
584
                if (k < 0)
29✔
585
                        return SIZE_MAX;
586

587
                str += k;
28✔
588
                n++;
28✔
589
        }
590

591
        return n;
592
}
593

594
size_t utf8_console_width(const char *str) {
540,011✔
595
        POINTER_MAY_BE_NULL(str);
540,011✔
596

597
        if (isempty(str))
540,011✔
598
                return 0;
599

600
        /* Returns the approximate width a string will take on screen when printed on a character cell
601
         * terminal/console. */
602

603
        size_t n = 0;
604
        while (*str) {
7,487,176✔
605
                int w;
6,968,778✔
606

607
                w = utf8_char_console_width(str);
6,968,778✔
608
                if (w < 0)
6,968,778✔
609
                        return SIZE_MAX;
610

611
                n += w;
6,968,777✔
612
                str = utf8_next_char(str);
6,968,777✔
613
        }
614

615
        return n;
616
}
617

618
size_t utf8_last_length(const char *s, size_t n) {
9✔
619
        int r;
9✔
620

621
        assert(s);
9✔
622

623
        if (n == SIZE_MAX)
9✔
624
                n = strlen(s);
7✔
625

626
        /* Determines length in bytes of last UTF-8 codepoint in string. If the string is empty, returns
627
         * zero. Treats invalid UTF-8 codepoints as 1 sized ones. */
628

629
        for (size_t last = 0;;) {
9✔
630
                if (n == 0)
28✔
631
                        return last;
9✔
632

633
                r = utf8_encoded_valid_unichar(s, n);
19✔
634
                if (r <= 0)
19✔
635
                        r = 1; /* treat invalid UTF-8 as byte-wide */
×
636

637
                s += r;
19✔
638
                n -= r;
19✔
639
                last = r;
19✔
640
        }
641
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc