-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchecksum.cs
117 lines (100 loc) · 3.64 KB
/
checksum.cs
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
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//////////////////////////////////////////////////////////////////////
namespace KP184
{
//////////////////////////////////////////////////////////////////////
public class ChecksumException: ApplicationException
{
public ChecksumException(string reason) : base(reason)
{
}
}
public static class checksum
{
//////////////////////////////////////////////////////////////////////
public static void dump_array(string header, byte[] message, int length)
{
Console.Error.Write(header);
string sep = "";
for(int i = 0; i < length; ++i)
{
Console.Error.Write($"{sep}{message[i]:X2}");
sep = ",";
}
Console.Error.WriteLine();
}
//////////////////////////////////////////////////////////////////////
// get the crc of a message
public static ushort compute(byte[] message, int length)
{
ushort crc = 0xffff;
for(int i = 0; i < length; ++i)
{
crc ^= message[i];
for(int j = 0; j < 8; ++j)
{
ushort xor = 0;
if((crc & 1) != 0)
{
xor = 0xa001;
}
crc = (ushort)((crc >> 1) ^ xor);
}
}
// Flip upper and lower CRC bytes for Kunkin FW Date > 2020
if (Globals.crc_order == "new")
{
crc = (ushort)((crc & 0xFFU) << 8 | (crc & 0xFF00U) >> 8);
}
return crc;
}
//////////////////////////////////////////////////////////////////////
// insert a ushort into the last 2 bytes
public static void insert(byte[] message, int length, ushort crc)
{
message[length - 1] = (byte)(crc & 0xff);
message[length - 2] = (byte)(crc >> 8);
}
//////////////////////////////////////////////////////////////////////
// get the last 2 bytes as a ushort
public static ushort extract(byte[] message, int length)
{
return (ushort)((message[length - 1] & 0xff) | (message[length - 2] << 8));
}
//////////////////////////////////////////////////////////////////////
// overwrite the last 2 bytes with the crc
public static void set(byte[] message, int length)
{
checksum.insert(message, length, checksum.compute(message, length - 2));
}
//////////////////////////////////////////////////////////////////////
// compare existing crc with computed crc
public static void verify(byte[] message, int length, bool checksum_check)
{
ushort crc_got = checksum.extract(message, length);
ushort crc_computed = checksum.compute(message, length - 2);
if(crc_computed != crc_got)
{
string err = $"Checksum error, got 0x{crc_got:X4}. expected 0x{crc_computed:X4}";
if(checksum_check)
{
throw new ChecksumException(err);
}
else
{
Log.Error(err);
}
dump_array("Message: ", message, length);
}
else
{
Log.Debug($"Checksum OK, got 0x{crc_got:X4}");
}
}
}
}