summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lottery.c
blob: 4b6071171395c8cb50de23475a5ace0c041de0d4 (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
/* SPDX-License-Identifier: MIT                                        ( >:3 )
 * Copyright (c) 2026 Leah Rowe <leah@libreboot.org>                    /| |\
                                                                         / \ */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> /*                           (^.>)  - are u lucky?       */
#include <string.h> /*                           \| /=                       */
#include "include/common.h" /*                    l \                        */

#define len_hell(x, y) ((((x) - (y)) < BUFSIZ) ? ((x) - (y)) : BUFSIZ)

static int rigged(void)
{
	size_t size[5] = { 0, 0, 0, SIZE_MAX, SIZE_MAX};
	char *b1 = NULL, *b2 = NULL;
	char *s = NULL;
again:
	if (!size[3]) {
		if (!size[4])
			return 0; /* <--- WINNER!!! */
		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 again; /*                  / \                          */

	if (s == NULL) {
		if ((s = malloc(BUFSIZ)) == NULL)
			goto out;
	}

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

	goto again;
out:
	free_and_set_null(&b1);
	free_and_set_null(&b2);
	free_and_set_null(&s);

	return 1;
}
/*                                              \(^o^)/ - come again soon!   */
int main(int argc, char **argv) /*               \ /                         */
{ /*                                             / \                         */
	xpledgex("stdio", NULL); /*                                          */
	printf("%s", (argc = rigged()) ? "You lose!\n" : "");

	return argc;
}