-
Notifications
You must be signed in to change notification settings - Fork 2
/
rare-n64-chksm.c
106 lines (99 loc) · 6.81 KB
/
rare-n64-chksm.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
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
#include <stdio.h>
// Goldeneye 007 test vector
unsigned char GE_example[] = {
0x1A, 0x00, 0xE5, 0xFF, 0x00, 0x3A, 0xFF, 0xFF, 0x0F, 0x00, 0xFF, 0xFF, 0xF2, 0x43, 0xFF, 0xFF, 0xC8, 0x1F,
0xFF, 0xFF, 0x06, 0x3F, 0xFF, 0xFC, 0x69, 0xFF, 0xFF, 0xF1, 0x83, 0xFF, 0xFF, 0xC4, 0xAF, 0xFF, 0xFF, 0x13,
0x7F, 0xFF, 0xFC, 0x51, 0xFF, 0xFF, 0xF1, 0x27, 0xFF, 0xFF, 0xC0, 0x9F, 0xFF, 0xFF, 0x09, 0x3F, 0xFF, 0xFC,
0xC6, 0xFF, 0xFF, 0xF4, 0xC7, 0xFF, 0x13, 0xC4, 0xE0, 0x9C, 0x5F, 0x19, 0xC6, 0xC0, 0x2C, 0x5D, 0x05, 0x80,
0x60, 0x34, 0x6B, 0x08, 0x06, 0x23, 0x60, 0x73, 0x24, 0x04, 0xA2, 0x8C, 0x4F, 0x00, 0x00, 0x00
};
// Banjo-Kazooie test vector
unsigned char BK_example[] = {
0x11, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1E, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF7, 0xFD, 0xFF, 0xFB, 0x6F, 0xD9, 0xFF, 0xFF, 0x00, 0x00, 0x64, 0xC9,
0x93, 0x26, 0x4C, 0x99, 0x32, 0x64, 0x04, 0xC7, 0x10, 0x53, 0x08, 0x4E, 0x0A, 0xCE, 0x0F, 0xD7, 0x32, 0x62,
0x0E, 0xC2, 0x2B, 0xDE, 0x23, 0x40, 0x12, 0xFA, 0x03, 0x1D, 0xFB, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF,
0xFF, 0x7F, 0xFE, 0xBF, 0x3D, 0xA6, 0xF2, 0x67, 0x5E, 0xFF, 0xDF, 0xFF, 0xE7, 0xFF, 0xFF, 0xEB, 0xFF, 0xBF,
0xD6, 0xF2, 0xFD, 0x61, 0xF9, 0x17, 0x00, 0x00, 0x00, 0x00, 0x08, 0x63, 0xC8, 0x64, 0x14, 0x02, 0x00, 0x0F,
0xFF, 0xFF, 0x00, 0x00, 0x1F, 0xFF, 0x00, 0x00
};
// Perfect Dark test vector
unsigned char PD_example[] = {
0x53, 0x68, 0x61, 0x64, 0x6F, 0x77, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x09, 0xB2, 0x4C, 0x0A, 0x28, 0x40,
0x3B, 0xAE, 0xFF, 0xF1, 0x10, 0x00, 0x00, 0x66, 0x1E, 0x0D, 0x00, 0x00, 0x03, 0xA0, 0x4F, 0x10, 0x90, 0x84,
0x22, 0x51, 0x61, 0x07, 0x91, 0x78, 0x28, 0xC0, 0xA4, 0x09, 0x31, 0xAF, 0x04, 0x50, 0x8E, 0x06, 0xD0, 0x56,
0x13, 0x71, 0x35, 0x0A, 0x11, 0x16, 0x3B, 0x60, 0xD9, 0x1A, 0xC1, 0x6B, 0x0E, 0x61, 0xA5, 0x18, 0xD0, 0xBF,
0x0B, 0xF1, 0x1F, 0x08, 0x10, 0xFD, 0x0E, 0x10, 0x9D, 0x0F, 0xE1, 0x4E, 0x0A, 0x61, 0x4C, 0x1B, 0x01, 0x57,
0x1C, 0xE1, 0x62, 0x06, 0x40, 0xD4, 0x11, 0x60, 0xE9, 0x12, 0xE1, 0x7F, 0x11, 0xE1, 0x67, 0x13, 0xA0, 0x8A,
0x0A, 0x80, 0xFE, 0x0C, 0x20, 0xF6, 0x1C, 0xB0, 0x47, 0x0B, 0xE0, 0xFF, 0x00, 0x40, 0x0C, 0x00, 0xE8, 0x88,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x80, 0x03, 0xFF, 0x80, 0x00,
0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xF7, 0xFF, 0xFF, 0xF8
};
// Banjo-Tooie test vector
unsigned char BT_example[] = {
0x4B, 0x48, 0x4A, 0x43, 0x01, 0x02, 0x1E, 0x00, 0x02, 0x01, 0x01, 0x03, 0x1C, 0x00, 0x00, 0x0F, 0x92, 0x0F,
0x1B, 0x00, 0x00, 0x14, 0x60, 0x05, 0x15, 0x13, 0x70, 0x1B, 0x31, 0x2A, 0x61, 0x25, 0xF7, 0x22, 0xAB, 0x07,
0x77, 0x12, 0xAB, 0x00, 0x00, 0x04, 0x2A, 0x00, 0x34, 0x00, 0x64, 0x00, 0x64, 0x00, 0x32, 0x00, 0x10, 0x00,
0x00, 0x00, 0xC8, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x2E, 0x00, 0x3B, 0x00,
0x41, 0x00, 0x00, 0x00, 0x34, 0x00, 0x64, 0x00, 0x72, 0x00, 0x40, 0x00, 0x3E, 0x00, 0x3E, 0x00, 0x29, 0x00,
0x52, 0x00, 0x4E, 0x00, 0x39, 0x02, 0x27, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x03, 0x3E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xFC, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFA, 0x3F, 0xC8, 0xF5,
0x7F, 0xC6, 0xEB, 0xFE, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned long long rarechksm(unsigned char data[], int size, int type) {
unsigned long long cksm0 = 0, cksm1 = 0, sum = 0x13108B3C1;
int bp, sd = 0;
for(bp = 0; bp < size; bp++, sd += 7, cksm0 ^= sum) {
sum += data[bp] << (sd & 0xF);
sum = ((sum >> 1 | sum << 32) ^ (sum << 44) >> 32) & 0x1FFFFFFFF;
sum ^= (sum >> 20) & 0xFFF;
}
for(bp--; bp >= 0; bp--, sd += 3, cksm1 ^= sum) {
sum += data[bp] << (sd & 0xF);
sum = ((sum >> 1 | sum << 32) ^ (sum << 44) >> 32) & 0x1FFFFFFFF;
sum ^= (sum >> 20) & 0xFFF;
}
switch(type) {
case 0: sum = (cksm0 << 32) | (cksm1 & 0xFFFFFFFF); break;
case 1: sum = (unsigned int) (cksm0 ^ cksm1); break;
case 2: sum = (unsigned int) (cksm0 << 16) | (cksm1 & 0xFFFF); break;
}
return sum;
}
int main(void) {
unsigned long long output;
// Calculate checksum for BT test vector
output = rarechksm(BT_example, sizeof(BT_example), 0);
if(output == 0x004A49F0955CF65E) printf("Good: "); else printf(" Bad: ");
printf("BT Checksum: %016llX\n", output);
// Calculate checksum for GE test vector
output = rarechksm(GE_example, sizeof(GE_example), 0);
if(output == 0xF5095E5959EF5946) printf("Good: "); else printf(" Bad: ");
printf("GE Checksum: %016llX\n", output);
// Calculate checksum for BK test vector
output = rarechksm(BK_example, sizeof(BK_example), 1);
if(output == 0x5970ECA5) printf("Good: "); else printf(" Bad: ");
printf("BK Checksum: %08llX\n", output);
// Calculate checksum for PD test vector
output = rarechksm(PD_example, sizeof(PD_example), 2);
if(output == 0xAE9B2CB0) printf("Good: "); else printf(" Bad: ");
printf("PD Checksum: %08llX\n", output);
return 0;
}