-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc.ts
More file actions
45 lines (37 loc) · 720 Bytes
/
crc.ts
File metadata and controls
45 lines (37 loc) · 720 Bytes
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
import { BinaryReader } from "./deps.ts";
export function calculateCrc(
io: BinaryReader,
start: number,
end: number,
): number {
const crcTable = [
0x0000,
0xcc01,
0xd801,
0x1400,
0xf001,
0x3c00,
0x2800,
0xe401,
0xa001,
0x6c00,
0x7800,
0xb401,
0x5000,
0x9c01,
0x8801,
0x4400,
];
let crc = 0;
// Copied from the FIT SDK
for (let i = start; i < end; i++) {
const byte = io.readUint8();
let tmp = crcTable[crc & 0xf];
crc = (crc >> 4) & 0x0fff;
crc = crc ^ tmp ^ crcTable[byte & 0xf];
tmp = crcTable[crc & 0xf];
crc = (crc >> 4) & 0x0fff;
crc = crc ^ tmp ^ crcTable[(byte >> 4) & 0xf];
}
return crc;
}