diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-04-21 13:39:29 +0100 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-04-21 13:39:29 +0100 |
| commit | 7f4f07fc4039353ee499d76bb53b6c742adb6428 (patch) | |
| tree | 69da1a8915bd9e28535eed9d6875c41002134987 /util/nvmutil/lib/string.c | |
| parent | 4b5aca8ff89bf46538a59bb55512dedeedbf1cbb (diff) | |
use old nvmutil for now, in lbmk
i'm trying to make nvmutil work on openbsd. the new code
in lbutils is a bit buggy, likely somewhere in mkhtemp.
i'm still debugging it.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/nvmutil/lib/string.c')
| -rw-r--r-- | util/nvmutil/lib/string.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/util/nvmutil/lib/string.c b/util/nvmutil/lib/string.c new file mode 100644 index 00000000..b1a5c3e2 --- /dev/null +++ b/util/nvmutil/lib/string.c @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: MIT + * Copyright (c) 2026 Leah Rowe <leah@libreboot.org> + * + * String functions + */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <errno.h> +#include <stddef.h> +#include <unistd.h> + +#include "../include/common.h" + +/* Portable strncmp() that blocks + * NULL/empty/unterminated strings + */ + +int +xstrxcmp(const char *a, const char *b, unsigned long maxlen) +{ + unsigned long i; + + if (a == NULL || b == NULL) + err(EINVAL, "NULL input to xstrxcmp"); + + if (*a == '\0' || *b == '\0') + err(EINVAL, "Empty string in xstrxcmp"); + + for (i = 0; i < maxlen; i++) { + + unsigned char ac = (unsigned char)a[i]; + unsigned char bc = (unsigned char)b[i]; + + if (ac == '\0' || bc == '\0') { + if (ac == bc) + return 0; + return ac - bc; + } + + if (ac != bc) + return ac - bc; + } + + err(EINVAL, "Unterminated string in xstrxcmp"); + + errno = EINVAL; + return -1; +} + +/* Portable strncmp() that blocks + * NULL/empty/unterminated strings + */ + +unsigned long +xstrxlen(const char *scmp, unsigned long maxlen) +{ + unsigned long xstr_index; + + if (scmp == NULL) + err(EINVAL, "NULL input to xstrxlen"); + + if (*scmp == '\0') + err(EINVAL, "Empty string in xstrxlen"); + + for (xstr_index = 0; + xstr_index < maxlen && scmp[xstr_index] != '\0'; + xstr_index++); + + if (xstr_index == maxlen) + err(EINVAL, "Unterminated string in xstrxlen"); + + return xstr_index; +} |
