-
Notifications
You must be signed in to change notification settings - Fork 4
/
zelda-cksum.c
68 lines (52 loc) · 1.17 KB
/
zelda-cksum.c
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
// Copyright 2007,2008 Segher Boessenkool <[email protected]>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include <stdio.h>
#include "tools.h"
static u8 buf[0x4000];
static void slot_checksum(u8 *x)
{
u32 i;
u32 sum, nsum;
sum = 0;
for (i = 0; i < 0x0a8c; i++)
sum += x[i];
nsum = -(sum + 0x0a8c);
wbe32(x + 0x0a8c, sum);
wbe32(x + 0x0a90, nsum);
}
static void save_checksum(u8 *x)
{
u32 i;
u16 sum, nsum;
slot_checksum(x + 8);
slot_checksum(x + 8 + 0x0a94);
slot_checksum(x + 8 + 2*0x0a94);
sum = 0;
for (i = 0; i < 0x0ffe; i++)
sum += be16(x + 2*i);
nsum = -(sum + 0x0ffe);
wbe16(x + 0x1ffc, sum);
wbe16(x + 0x1ffe, nsum);
}
int main(int argc, char **argv)
{
FILE *fp;
if (argc != 2) {
fprintf(stderr, "Usage: %s <zeldaTp.dat>\n", argv[0]);
return 1;
}
fp = fopen(argv[1], "rb+");
if (!fp)
fatal("open %s", argv[1]);
if (fread(buf, 0x4000, 1, fp) != 1)
fatal("read %s", argv[1]);
save_checksum(buf);
save_checksum(buf + 0x2000);
if (fseek(fp, 0, SEEK_SET))
fatal("seek");
if (fwrite(buf, 0x4000, 1, fp) != 1)
fatal("write %s", argv[1]);
fclose(fp);
return 0;
}