diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-29 09:10:57 +0100 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-29 09:18:36 +0100 |
| commit | c2a70b7de0e15dc80d5b56d438a6d4ed47647b44 (patch) | |
| tree | 5c5281bb789f0ea9ab1d8766f5cea6e7182d431b /util/libreboot-utils/lib/rand.c | |
| parent | 45edcf33f7d4b2f405c0f59fccc71b6ac15f5c65 (diff) | |
libreboot-utils: simplify random tmpdir namegen
generalise it in rand.c because this logic will
be useful for other programs in the future.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/rand.c')
| -rw-r--r-- | util/libreboot-utils/lib/rand.c | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 2cb0f56d..2aa0de3a 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -72,6 +72,36 @@ * or your program dies. */ +/* random string generator, with + * rejection sampling. NOTE: only + * uses ASCII-safe characters, for + * printing on a unix terminal + * + * you still shouldn't use this for + * password generation; open diceware + * passphrases are better for that + * + * NOTE: the generated strings must + * ALSO be safe for file/directory names + * on unix-like os e.g. linux/bsd + */ +char * +rchars(size_t n) /* emulates spkmodem-decode */ +{ + static char ch[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + char *s = NULL; + size_t i; + + smalloc(&s, n + 1); + for (i = 0; i < n; i++) + s[i] = ch[rsize(sizeof(ch) - 1)]; + + *(s + n) = '\0'; + return s; +} + size_t rsize(size_t n) { @@ -85,30 +115,6 @@ rsize(size_t n) return rval % n; } -char * -mkrstr(size_t n) /* emulates spkmodem-decode */ -{ - char *s; - size_t i; - - if (n == 0) - err_exit(EPERM, "rmalloc: zero-byte request"); - - if (n >= SIZE_MAX - 1) - err_exit(EOVERFLOW, "rmalloc: overflow"); - - if (if_err((s = rmalloc(n + 1)) == NULL, EFAULT)) - err_exit(EFAULT, "mkrstr: null"); - - for (i = 0; i < n; i++) - while(*(s + i) == '\0') - rset(s + i, 1); - - *(s + n) = '\0'; - - return s; -} - void * rmalloc(size_t n) { |
