-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCRC.cpp
More file actions
50 lines (37 loc) · 1.06 KB
/
CRC.cpp
File metadata and controls
50 lines (37 loc) · 1.06 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
// CRC.h
/*
Shamelessly borrowed from the more general version found at http://n64dev.org/n64crc.html
*/
#include "CRC.h"
#define ROL(i, b) (((i) << (b)) | ((i) >> (32 - (b))))
#define BYTES2LONG(b) ( (b)[0] << 24 | \
(b)[1] << 16 | \
(b)[2] << 8 | \
(b)[3] )
#define N64_HEADER_SIZE 0x40
#define CHECKSUM_CIC6105 0xDF26F436
#define CHECKSUM_START 0x00001000
#define CHECKSUM_LENGTH 0x00100000
int N64CalcCRC(unsigned int *crc, unsigned char *data) {
int bootcode, i;
unsigned int t1, t2, t3;
unsigned int t4, t5, t6;
unsigned int r, d;
t1 = t2 = t3 = t4 = t5 = t6 = CHECKSUM_CIC6105;
i = CHECKSUM_START;
while (i < (CHECKSUM_START + CHECKSUM_LENGTH)) {
d = BYTES2LONG(&data[i]);
if ((t6 + d) < t6) t4++;
t6 += d;
t3 ^= d;
r = ROL(d, (d & 0x1F));
t5 += r;
if (t2 > d) t2 ^= r;
else t2 ^= t6 ^ d;
t1 += BYTES2LONG(&data[N64_HEADER_SIZE + 0x0710 + (i & 0xFF)]) ^ d;
i += 4;
}
crc[0] = t6 ^ t4 ^ t3;
crc[1] = t5 ^ t2 ^ t1;
return 0;
}