summaryrefslogtreecommitdiff
path: root/utilities/hammington2416.py
blob: 8e60a330d014ebf001e38e8354594d565d77e340 (plain)
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
#!/usr/bin/python

import sys

_hammingCalculateParityLowNibble = [0,  3,  5,  6,  6,  5,  3,  0,  7,  4,  2,  1,  1,  2,  4,  7]
_hammingCalculateParityHighNibble = [0,  9, 10,  3, 11,  2,  1,  8, 12,  5,  6, 15,  7, 14, 13,  4]

def HammingCalculateParity128(byte):
	return _hammingCalculateParityLowNibble[byte&0x0F] ^ _hammingCalculateParityHighNibble[byte >> 4]

def HammingCalculateParity2416(first, second):
	return HammingCalculateParity128(second) << 4 | HammingCalculateParity128(first)

output = ""
bytepos = 0
oldbyte = None

# Read bytewise
f = open(sys.argv[1])
for byte in f.read():
	output += byte
	oldbyte = byte
	bytepos += 1
	if bytepos == 2:
		bytepos = 0
		output += str(HammingCalculateParity2416(ord(oldbyte), ord(byte)))
if bytepos == 1:
	output += "\x00"
	output += str(HammingCalculateParity2416(ord(oldbyte), ord("\x00")))

print output