diff options
Diffstat (limited to 'util/libreboot-utils/lottery.c')
| -rw-r--r-- | util/libreboot-utils/lottery.c | 134 |
1 files changed, 72 insertions, 62 deletions
diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c index f7c467e2..cbe8a871 100644 --- a/util/libreboot-utils/lottery.c +++ b/util/libreboot-utils/lottery.c @@ -1,76 +1,86 @@ /* SPDX-License-Identifier: MIT ( >:3 ) * Copyright (c) 2026 Leah Rowe <leah@libreboot.org> /| |\ - Presenting: a love song about non-determinism. / \ */ + Something something non-determinism / \ */ +#include <ctype.h> +#include <stddef.h> #include <stdio.h> #include <stdint.h> -#include <stdlib.h> /* (^.>) - are u lucky? */ -#include <string.h> /* \| /= */ -#include "include/common.h" /* l \ */ +#include <string.h> +#include <stdlib.h> +#include "include/common.h" -#define len_hell(x, y) ((((x) - (y)) < BUFSIZ) ? ((x) - (y)) : BUFSIZ) +static void +exit_cleanup(void); -static int rigged(void) +static void +spew_buf(const void *data, size_t len); + +int +main(int argc, char **argv) { - size_t size[5] = { 0, 0, 0, SIZE_MAX, SIZE_MAX}; - char *b1 = NULL, *b2 = NULL; - char *s = NULL; - size_t e = 0; - - int rval = 1; - -for (e = 0; e < SIZE_MAX; e++) { - if (rsize(e | 1) == 666) - goto heaven; - - size[3] = SIZE_MAX; - size[4] = SIZE_MAX; -hell: - if (!size[3]) { - if (!size[4]) - continue; - else --size[4]; - } else --size[3]; - - free_and_set_null(&b1); - free_and_set_null(&b2); - - size[0] = rsize(SIZE_MAX); /* \( ^o^)/ - then come play! */ - size[1] = rsize(SIZE_MAX); /* | | */ -/* / \ */ - if (!(size[0] && (size[0] == size[1]) && (size[0] <= SIZE_MAX)) || - ((size[0] & 1) && (*(b1 = mkrstr(1)) != *(b2 = mkrstr(1))))) - goto out; /* \(^-^)/ - it could be you! */ - if (!(size[0] &= ~(size_t)1)) /* \ / */ - goto hell; /* / \ */ - - if (s == NULL) { - if ((s = malloc(BUFSIZ)) == NULL) - goto out; - } + int same = 0; + char *buf; + + (void) argc, (void) argv; + xpledgex("stdio", NULL); + + (void) errhook(exit_cleanup); + + buf = mkrbuf(BUFSIZ + 1); + if (!memcmp(buf, buf + (BUFSIZ >> 1), BUFSIZ >> 1)) + same = 1; - for (size[1] = 0; size[1] < size[0]; size[1] += BUFSIZ) { - rset(s, size[2] = len_hell(size[0], size[1])); - if (!memcmp(s, s + (size[2] >> 1), size[2] >> 1)) - goto out; - } + if (argc < 2) /* no spew */ + spew_buf(buf, BUFSIZ); + free(buf); - goto hell; + fprintf(stderr, "\n%s\n", same ? "You win!" : "You lose!"); + return same ^ 1; } -heaven: - rval = 0; /* <-- WINNER!!! */ -out: - free_and_set_null(&b1); - free_and_set_null(&b2); - free_and_set_null(&s); - - return rval; + +static void +spew_buf(const void *data, size_t len) +{ + const unsigned char *buf = data; + unsigned char c; + size_t i, j; + + if (buf == NULL || + len == 0) + return; + + for (i = 0; i < len; i += 16) { + + printf("%08zx ", i); + + for (j = 0; j < 16; j++) { + + if (i + j < len) + printf("%02x ", buf[i + j]); + else + printf(" "); + + if (j == 7) + printf(" "); + } + + printf(" |"); + + for (j = 0; j < 16 && i + j < len; j++) { + + c = buf[i + j]; + printf("%c", isprint(c) ? c : '.'); + } + + printf("|\n"); + } + + printf("%08zx\n", len); } -/* \(^o^)/ - come back soon! */ -int main(int argc, char **argv) /* \ / */ -{ /* / \ */ - xpledgex("stdio", NULL); /* */ - printf("%s\n", (argc = rigged()) ? "You lose!" : "You win!"); - return argc; +static void +exit_cleanup(void) +{ + return; } |
