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

systemd / systemd / 24373971178

14 Apr 2026 12:08AM UTC coverage: 72.291% (+0.2%) from 72.107%
24373971178

push

github

web-flow
hwdb: Add extended SteelSeries Arctis headset device support (#41628)

Add USB device IDs for additional SteelSeries Arctis headset models to
the sound card hardware database.

Newly added device IDs:

- Arctis Nova 7x v2 (22AD)
- Arctis Nova 7 Diablo IV (22A9)
- Arctis Nova 7X (22A4)
- Arctis Nova 7X (22A5)
- Arctis Nova 7P V2 (22A7)

321138 of 444232 relevant lines covered (72.29%)

1277257.29 hits per line

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

95.61
/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) {
89,433✔
17

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

27
        return true;
28
}
29

30
static bool unichar_is_control(char32_t ch) {
123,877,220✔
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')) ||
123,877,220✔
39
                (0x7F <= ch && ch <= 0x9F);
123,877,042✔
40
}
41

42
/* count of characters used to encode one unicode char */
43
static size_t utf8_encoded_expected_len(uint8_t c) {
1,076,722,222✔
44
        if (c < 0x80)
1,076,722,222✔
45
                return 1;
46
        if ((c & 0xe0) == 0xc0)
287,658✔
47
                return 2;
48
        if ((c & 0xf0) == 0xe0)
260,527✔
49
                return 3;
50
        if ((c & 0xf8) == 0xf0)
840✔
51
                return 4;
52
        if ((c & 0xfc) == 0xf8)
64✔
53
                return 5;
54
        if ((c & 0xfe) == 0xfc)
64✔
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) {
135,047,461✔
62
        char32_t unichar;
135,047,461✔
63
        size_t len;
135,047,461✔
64

65
        assert(str);
135,047,461✔
66
        assert(ret_unichar);
135,047,461✔
67

68
        len = utf8_encoded_expected_len(str[0]);
135,047,461✔
69

70
        switch (len) {
135,047,461✔
71
        case 1:
134,850,495✔
72
                *ret_unichar = (char32_t)str[0];
134,850,495✔
73
                return 1;
134,850,495✔
74
        case 2:
22,374✔
75
                unichar = str[0] & 0x1f;
22,374✔
76
                break;
22,374✔
77
        case 3:
174,095✔
78
                unichar = (char32_t)str[0] & 0x0f;
174,095✔
79
                break;
174,095✔
80
        case 4:
497✔
81
                unichar = (char32_t)str[0] & 0x07;
497✔
82
                break;
497✔
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++) {
569,013✔
94
                if (((char32_t)str[i] & 0xc0) != 0x80)
372,053✔
95
                        return -EINVAL;
96

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

101
        *ret_unichar = unichar;
196,960✔
102
        return len;
196,960✔
103
}
104

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

108
        for (const char *p = str; length > 0;) {
127,951,153✔
109
                int encoded_len;
123,877,221✔
110
                char32_t val;
123,877,221✔
111

112
                encoded_len = utf8_encoded_valid_unichar(p, length);
123,877,221✔
113
                if (encoded_len < 0)
123,877,221✔
114
                        return false;
179✔
115
                assert(encoded_len > 0 && (size_t) encoded_len <= length);
123,877,220✔
116

117
                if (utf8_encoded_to_unichar(p, &val) < 0 ||
123,877,220✔
118
                    unichar_is_control(val) ||
123,877,220✔
119
                    (!allow_newline && val == '\n'))
25,795,501✔
120
                        return false;
121

122
                length -= encoded_len;
123,877,042✔
123
                p += encoded_len;
123,877,042✔
124
        }
125

126
        return true;
127
}
128

129
char* utf8_is_valid_n(const char *str, size_t len_bytes) {
46,322,218✔
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);
46,322,218✔
134

135
        for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) {
829,740,706✔
136
                int len;
783,419,617✔
137

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

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

146
                i += len;
783,418,488✔
147
        }
148

149
        return (char*) str;
150
}
151

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

155
        assert(str);
12,396✔
156

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

161
        while (*str) {
287,409✔
162
                int len;
275,013✔
163

164
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
275,013✔
165
                if (len > 0) {
275,013✔
166
                        s = mempcpy(s, str, len);
274,997✔
167
                        str += len;
274,997✔
168
                } else {
169
                        s = stpcpy(s, UTF8_REPLACEMENT_CHARACTER);
16✔
170
                        str += 1;
16✔
171
                }
172
        }
173

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

178
int utf8_char_console_width(const char *str) {
8,758,010✔
179
        char32_t c;
8,758,010✔
180
        int r;
8,758,010✔
181

182
        r = utf8_encoded_to_unichar(str, &c);
8,758,010✔
183
        if (r < 0)
8,758,010✔
184
                return r;
8,758,010✔
185

186
        if (c == '\t')
8,758,009✔
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;
8,715,796✔
192
}
193

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

198
        assert(str);
1,321✔
199

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

203
        p = s = prev_s = malloc(strlen(str) * 4 + 1);
1,312✔
204
        if (!p)
1,312✔
205
                return NULL;
206

207
        for (;;) {
52,236✔
208
                int len;
52,236✔
209
                char *saved_s = s;
52,236✔
210

211
                if (!*str) { /* done! */
52,236✔
212
                        if (force_ellipsis)
1,238✔
213
                                goto truncation;
73✔
214
                        else
215
                                goto finish;
1,165✔
216
                }
217

218
                len = utf8_encoded_valid_unichar(str, SIZE_MAX);
50,998✔
219
                if (len > 0) {
50,998✔
220
                        if (utf8_is_printable(str, len)) {
50,902✔
221
                                int w;
50,739✔
222

223
                                w = utf8_char_console_width(str);
50,739✔
224
                                assert(w >= 0);
50,739✔
225
                                if (n + w > console_width)
50,739✔
226
                                        goto truncation;
34✔
227

228
                                s = mempcpy(s, str, len);
50,705✔
229
                                str += len;
50,705✔
230
                                n += w;
50,705✔
231

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

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

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

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

255
                prev_s = saved_s;
256
        }
257

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

263
        s = mempcpy(s, "…", strlen("…"));
147✔
264

265
 finish:
1,312✔
266
        *s = '\0';
1,312✔
267
        return str_realloc(p);
1,312✔
268
}
269

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

274
        assert(str);
65,741✔
275

276
        for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++)
1,683,113✔
277
                if ((unsigned char) str[i] >= 128 || str[i] == '\0')
1,620,778✔
278
                        return NULL;
279

280
        return (char*) str;
281
}
282

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

287
        assert(str);
49✔
288
        assert(ret);
49✔
289

290
        _cleanup_free_ char *ans = new(char, strlen(str) + 1);
98✔
291
        if (!ans)
49✔
292
                return -ENOMEM;
293

294
        char *q = ans;
295

296
        for (const char *p = str; *p; q++) {
385✔
297
                int l;
338✔
298

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

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

309
                p += l;
336✔
310
        }
311
        *q = '\0';
47✔
312

313
        *ret = TAKE_PTR(ans);
47✔
314
        return 0;
47✔
315
}
316

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

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

358
        return 0;
359
}
360

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

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

368
        assert(s);
2,380✔
369

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

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

376
                length *= 2;
6✔
377
        }
378

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

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

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

391
        while (f + 1 < (const uint8_t*) s + length) {
78,387✔
392
                char16_t w1, w2;
76,007✔
393

394
                /* see RFC 2781 section 2.2 */
395

396
                w1 = f[1] << 8 | f[0];
76,007✔
397
                f += 2;
76,007✔
398

399
                if (!utf16_is_surrogate(w1)) {
76,007✔
400
                        t += utf8_encode_unichar(t, w1);
76,000✔
401
                        continue;
76,000✔
402
                }
403

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

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

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

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

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

421
        *t = 0;
2,380✔
422
        return r;
2,380✔
423
}
424

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

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

429
        switch (c) {
16✔
430

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

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

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

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

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

454
        assert(s);
367✔
455

456
        if (length == SIZE_MAX)
367✔
457
                length = strlen(s);
366✔
458

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

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

466
        p = n;
467

468
        for (size_t i = 0; i < length;) {
5,963✔
469
                char32_t unichar;
5,596✔
470
                size_t e;
5,596✔
471

472
                e = utf8_encoded_expected_len(s[i]);
5,596✔
473
                if (e <= 1) /* Invalid and single byte characters are copied as they are */
5,596✔
474
                        goto copy;
5,580✔
475

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

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

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

487
        copy:
5,580✔
488
                *(p++) = htole16(s[i++]);
5,580✔
489
        }
490

491
        *p = 0;
367✔
492
        return n;
367✔
493
}
494

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

498
        assert(s);
631✔
499

500
        while (*s != 0)
9,408✔
501
                n++, s++;
8,777✔
502

503
        return n;
631✔
504
}
505

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

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

512
/* expected size used to encode one unicode char */
513
static int utf8_unichar_to_encoded_len(char32_t unichar) {
89,423✔
514

515
        if (unichar < 0x80)
89,423✔
516
                return 1;
517
        if (unichar < 0x800)
89,423✔
518
                return 2;
519
        if (unichar < 0x10000)
84,993✔
520
                return 3;
521
        if (unichar < 0x200000)
273✔
522
                return 4;
523
        if (unichar < 0x4000000)
×
524
                return 5;
×
525

526
        return 6;
527
}
528

529
/* validate one encoded unicode char and return its length */
530
int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
941,669,165✔
531
        char32_t unichar;
941,669,165✔
532
        size_t len;
941,669,165✔
533
        int r;
941,669,165✔
534

535
        assert(str);
941,669,165✔
536
        assert(length > 0);
941,669,165✔
537

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

540
        len = utf8_encoded_expected_len(str[0]);
941,669,165✔
541
        if (len == 0)
941,669,165✔
542
                return -EINVAL;
941,669,165✔
543

544
        /* Do we have a truncated multi-byte character? */
545
        if (len > length)
941,669,101✔
546
                return -EINVAL;
547

548
        /* ascii is valid */
549
        if (len == 1)
941,669,093✔
550
                return 1;
551

552
        /* check if expected encoded chars are available */
553
        for (size_t i = 0; i < len; i++)
356,361✔
554
                if ((str[i] & 0x80) != 0x80)
266,933✔
555
                        return -EINVAL;
556

557
        r = utf8_encoded_to_unichar(str, &unichar);
89,428✔
558
        if (r < 0)
89,428✔
559
                return r;
560

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

565
        /* check if value has valid range */
566
        if (!unichar_is_valid(unichar))
89,423✔
567
                return -EINVAL;
6✔
568

569
        return (int) len;
570
}
571

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

575
        assert(str);
6✔
576

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

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

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

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

590
        return n;
591
}
592

593
size_t utf8_console_width(const char *str) {
486,881✔
594
        POINTER_MAY_BE_NULL(str);
486,881✔
595

596
        if (isempty(str))
486,881✔
597
                return 0;
598

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

602
        size_t n = 0;
603
        while (*str) {
6,765,532✔
604
                int w;
6,298,420✔
605

606
                w = utf8_char_console_width(str);
6,298,420✔
607
                if (w < 0)
6,298,420✔
608
                        return SIZE_MAX;
609

610
                n += w;
6,298,419✔
611
                str = utf8_next_char(str);
6,298,419✔
612
        }
613

614
        return n;
615
}
616

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

620
        assert(s);
9✔
621

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

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

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

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

636
                s += r;
19✔
637
                n -= r;
19✔
638
                last = r;
19✔
639
        }
640
}
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