Skip to content
Snippets Groups Projects
Unverified Commit 3ab773d8 authored by Jakob Haufe's avatar Jakob Haufe
Browse files

Autodetect python crc16 implementation

There are several common CRC implementations for python with the currently
used one (crc16) not being available in e.g. Debian.

crcmod and/or crcelk are readily available on several distributions.
parent cef43dde
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import sys
try:
import crc16
crcfun = crc16.crc16xmodem
except ImportError:
try:
import crcmod
crcfun = crcmod.predefined.mkCrcFun('xmodem')
except ImportError:
try:
import crcelk
crcfun = crcelk.CRC_XMODEM.calc_bytes
except ImportError:
raise Exception('Could not find a CRC implementation. Tried: crc16, crcmod, crcelk.')
def main():
data = open(sys.argv[1], 'rb').read()
crc = crc16.crc16xmodem(data)
crc = crcfun(data)
# print(crc)
padded = data + bytes([crc >> 8, crc & 0xFF])
crc = crc16.crc16xmodem(padded)
crc = crcfun(padded)
# print(crc)
open(sys.argv[1], 'wb').write(padded)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment