-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChecksum.py
executable file
·51 lines (38 loc) · 1.02 KB
/
Checksum.py
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
#!/usr/bin/python
import sys
def checksum(bs):
ckA = 0
ckB = 0
for b in bs:
ckA = (ckA + ord(b)) & 0xff
ckB = (ckB + ckA) & 0xff
return (ckA, ckB)
def hexByte(b):
if (b >= 16):
return hex(b)[2:4]
else:
return ("0" + hex(b)[2:3])
syncByteLen = 2
if len(sys.argv) != 2:
sys.stderr.write("USAGE: %s <INPUT>\n" % sys.argv[0])
sys.exit(2)
f = open(sys.argv[1], "rb")
packet = f.read()
f.close()
sys.stderr.write("Read packet of %d bytes, not checksumming the first %d\n" % (len(packet), syncByteLen))
(checkA, checkB) = checksum(packet[syncByteLen:])
n = 0
for b in packet:
sys.stdout.write(hexByte(ord(b)))
if ((n % 8) == 7):
sys.stdout.write("\n")
else:
sys.stdout.write(" ")
sys.stdout.write("\nChecksum: %s %s\n" % (hexByte(checkA), hexByte(checkB)))
outname = sys.argv[1] + "-chk"
g = open(outname, "wb")
g.write(packet)
g.write(chr(checkA))
g.write(chr(checkB))
g.close()
sys.stderr.write("Wrote checksummed packet to " + outname + "\n")