| 1 | /* Hardware capability support for run-time dynamic loader. String splitting. |
| 2 | Copyright (C) 2020-2023 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <dl-hwcaps.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | _Bool |
| 24 | _dl_hwcaps_split (struct dl_hwcaps_split *s) |
| 25 | { |
| 26 | if (s->segment == NULL) |
| 27 | return false; |
| 28 | |
| 29 | /* Skip over the previous segment. */ |
| 30 | s->segment += s->length; |
| 31 | |
| 32 | /* Consume delimiters. This also avoids returning an empty |
| 33 | segment. */ |
| 34 | while (*s->segment == ':') |
| 35 | ++s->segment; |
| 36 | if (*s->segment == '\0') |
| 37 | return false; |
| 38 | |
| 39 | /* This could use strchrnul, but we would have to link the function |
| 40 | into ld.so for that. */ |
| 41 | const char *colon = strchr (s->segment, ':'); |
| 42 | if (colon == NULL) |
| 43 | s->length = strlen (s->segment); |
| 44 | else |
| 45 | s->length = colon - s->segment; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | _Bool |
| 50 | _dl_hwcaps_split_masked (struct dl_hwcaps_split_masked *s) |
| 51 | { |
| 52 | while (true) |
| 53 | { |
| 54 | if (!_dl_hwcaps_split (&s->split)) |
| 55 | return false; |
| 56 | bool active = s->bitmask & 1; |
| 57 | s->bitmask >>= 1; |
| 58 | if (active && _dl_hwcaps_contains (s->mask, |
| 59 | s->split.segment, s->split.length)) |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | _Bool |
| 65 | _dl_hwcaps_contains (const char *hwcaps, const char *name, size_t name_length) |
| 66 | { |
| 67 | if (hwcaps == NULL) |
| 68 | return true; |
| 69 | |
| 70 | struct dl_hwcaps_split split; |
| 71 | _dl_hwcaps_split_init (&split, hwcaps); |
| 72 | while (_dl_hwcaps_split (&split)) |
| 73 | if (split.length == name_length |
| 74 | && memcmp (split.segment, name, name_length) == 0) |
| 75 | return true; |
| 76 | return false; |
| 77 | } |
| 78 | |