Created
February 5, 2018 21:18
-
-
Save lambdafu/7100593ddfda99e53e284b491177dd3e to your computer and use it in GitHub Desktop.
modified rabin mueller decoder for NFC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2 | |
import sys | |
class mrm_decoder(): | |
def __init__(self): | |
self.last = "0" | |
# Take one symbol and decode it. | |
# Symbol can be: 1 (kurz = short), 2 (mittel = medium), 3 (lang = long) | |
def decode(self, sym): | |
result = None | |
if sym == "1": | |
if self.last == "0": | |
result = "0" | |
else: | |
result = "1" | |
elif sym == "2": | |
if self.last == "0": | |
result = "1" | |
else: | |
result = "00" | |
elif sym == "3": | |
result = "01" | |
else: | |
raise Exception("Unknown symbol: %s" % sym) | |
self.last = result[-1] | |
return result | |
mrm = mrm_decoder() | |
bitstream = "0" | |
line = sys.argv[1].strip() | |
for symbol in line: | |
bitstream += mrm.decode(symbol) | |
print "bits: ", bitstream | |
for idx in range(0, len(bitstream), 9): | |
byte = bitstream[idx+8:idx:-1] | |
if len(byte) < 8: | |
print byte | |
else: | |
print byte, bitstream[idx+8], "=", '0x%02x' % int(byte, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment