summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lottery.c
blob: cbe8a871a662484d9d076f48cb94356f5c21243a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* SPDX-License-Identifier: MIT                                        ( >:3 )
 * Copyright (c) 2026 Leah Rowe <leah@libreboot.org>                    /| |\
   Something something non-determinism                                   / \ */

#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "include/common.h"

static void
exit_cleanup(void);

static void
spew_buf(const void *data, size_t len);

int
main(int argc, char **argv)
{
	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;

	if (argc < 2) /* no spew */
		spew_buf(buf, BUFSIZ);
	free(buf);

	fprintf(stderr, "\n%s\n", same ? "You win!" : "You lose!");
	return same ^ 1;
}

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);
}

static void
exit_cleanup(void)
{
	return;
}