-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcheat.c
More file actions
144 lines (129 loc) · 3.6 KB
/
cheat.c
File metadata and controls
144 lines (129 loc) · 3.6 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <string.h>
#include "cheat.h"
static int hex_digit(char c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
bool cheat_parse_one(const char *code,
uint32_t *addr_out,
uint32_t *val_out,
uint8_t *size_out)
{
char buf[32];
size_t n = 0;
size_t addr_len, val_len, i;
uint32_t addr = 0, val = 0;
if (!code || !addr_out || !val_out || !size_out)
return false;
for (; *code && n < sizeof(buf) - 1; code++)
{
if (hex_digit((char)*code) >= 0)
buf[n++] = *code;
else if (*code != ' ' && *code != '\t' && *code != ':' &&
*code != '-' && *code != '.')
return false;
}
buf[n] = '\0';
/* Layout: address_hex + value_hex (concatenated after separator strip).
* Accepted pairings cover byte/word/long values under 24- or 32-bit
* nominal addresses; the address is always masked to 24 bits below. */
switch (n)
{
case 8: addr_len = 6; val_len = 2; break; /* 6 + byte */
case 10: addr_len = 6; val_len = 4; break; /* 6 + word */
case 12: addr_len = 8; val_len = 4; break; /* 8 + word (PAR) */
case 14: addr_len = 6; val_len = 8; break; /* 6 + long */
case 16: addr_len = 8; val_len = 8; break; /* 8 + long */
default: return false;
}
for (i = 0; i < addr_len; i++)
addr = (addr << 4) | (uint32_t)hex_digit(buf[i]);
for (i = 0; i < val_len; i++)
val = (val << 4) | (uint32_t)hex_digit(buf[addr_len + i]);
*addr_out = addr & 0x00FFFFFFu;
*val_out = val;
*size_out = (uint8_t)(val_len / 2);
return true;
}
void cheat_list_remove_index(cheat_list_t *list, unsigned index)
{
unsigned i, j = 0;
if (!list)
return;
for (i = 0; i < list->count; i++)
{
if (list->entries[i].tag == (uint8_t)(index & 0xFF))
continue;
if (j != i)
list->entries[j] = list->entries[i];
j++;
}
list->count = j;
}
void cheat_list_reset(cheat_list_t *list)
{
if (!list)
return;
memset(list, 0, sizeof(*list));
}
void cheat_list_set(cheat_list_t *list,
unsigned index,
bool enabled,
const char *code)
{
const char *p;
const char *start;
if (!list || !code)
return;
cheat_list_remove_index(list, index);
if (!enabled)
return;
p = code;
start = p;
for (;;)
{
if (*p == '+' || *p == '\n' || *p == '\r' || *p == '\0')
{
size_t len = (size_t)(p - start);
if (len > 0 && list->count < CHEAT_MAX_ENTRIES)
{
char tmp[64];
cheat_entry_t c;
if (len >= sizeof(tmp))
len = sizeof(tmp) - 1;
memcpy(tmp, start, len);
tmp[len] = '\0';
if (cheat_parse_one(tmp, &c.address, &c.value, &c.size))
{
c.tag = (uint8_t)(index & 0xFF);
c.enabled = true;
list->entries[list->count++] = c;
}
}
if (*p == '\0')
break;
start = p + 1;
}
p++;
}
}
void cheat_list_apply(const cheat_list_t *list,
cheat_write_fn write,
void *user)
{
unsigned i;
if (!list || !write)
return;
for (i = 0; i < list->count; i++)
{
if (!list->entries[i].enabled)
continue;
write(list->entries[i].address,
list->entries[i].value,
list->entries[i].size,
user);
}
}