Work in progress
This commit is contained in:
parent
d363e25cd5
commit
03e514ecff
64
RS129.py
64
RS129.py
@ -1,6 +1,17 @@
|
|||||||
|
'''
|
||||||
|
This file is a python translation of:
|
||||||
|
https://github.com/g4klx/MMDVMHost/blob/master/RS129.cpp
|
||||||
|
Copyright (C) 2015 by Jonathan Naylor G4KLX
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
'''
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
MASK = [0x96, 0x96, 0x96]
|
MASK = [0x96, 0x96, 0x96]
|
||||||
|
NUM_BYTES = 9
|
||||||
PARITY_BYTES = 3;
|
PARITY_BYTES = 3;
|
||||||
MAXDEG = PARITY_BYTES * 2;
|
MAXDEG = PARITY_BYTES * 2;
|
||||||
POLY= [64, 56, 14, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
POLY= [64, 56, 14, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
@ -59,39 +70,50 @@ LOG_TABLE = [
|
|||||||
0x4F, 0xAE, 0xD5, 0xE9, 0xE6, 0xE7, 0xAD, 0xE8, 0x74, 0xD6, 0xF4, 0xEA, 0xA8, 0x50, 0x58, 0xAF
|
0x4F, 0xAE, 0xD5, 0xE9, 0xE6, 0xE7, 0xAD, 0xE8, 0x74, 0xD6, 0xF4, 0xEA, 0xA8, 0x50, 0x58, 0xAF
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# For testing the code
|
||||||
|
def print_hex(_list):
|
||||||
|
print('[{}]'.format(', '.join(hex(x) for x in _list)))
|
||||||
|
|
||||||
# multiplication using logarithms
|
# multiplication using logarithms
|
||||||
def log_mult(a, b):
|
def log_mult(a, b):
|
||||||
if a == 0 or b == 0:
|
if a == 0 or b == 0:
|
||||||
return 0
|
return 0
|
||||||
return EXP_TABLE[LOG_TABLE[a] + LOG_TABLE[b]];
|
return EXP_TABLE[LOG_TABLE[a] + LOG_TABLE[b]];
|
||||||
|
|
||||||
def RS129_encode(msg):
|
# Reed-Solomon (12,9) encoder
|
||||||
nbytes = 9
|
def RS129_encode(_msg):
|
||||||
parity = [0x00,0x00,0x00]
|
assert len(_msg) == 9, 'RS129_encode error: Message not 9 bytes: %s' % print_hex(_msg)
|
||||||
|
|
||||||
for i in range(nbytes):
|
parity = [0x00, 0x00, 0x00]
|
||||||
dbyte = msg[i] ^ parity[PARITY_BYTES - 1]
|
|
||||||
|
|
||||||
for j in range(PARITY_BYTES-1,0,-1):
|
for i in range(NUM_BYTES):
|
||||||
parity[j] = parity[j - 1] ^ log_mult(POLY[j], dbyte)
|
dbyte = _msg[i] ^ parity[PARITY_BYTES - 1]
|
||||||
|
|
||||||
parity[0] = log_mult(POLY[0], dbyte)
|
for j in range(PARITY_BYTES-1, 0, -1):
|
||||||
|
parity[j] = parity[j - 1] ^ log_mult(POLY[j], dbyte)
|
||||||
|
parity[0] = log_mult(POLY[0], dbyte)
|
||||||
return parity
|
return parity
|
||||||
|
|
||||||
'''
|
|
||||||
// Reed-Solomon (12,9) check
|
|
||||||
bool CRS129::check(const unsigned char* in)
|
|
||||||
{
|
|
||||||
assert(in != NULL);
|
|
||||||
|
|
||||||
unsigned char parity[4U];
|
# Reed-Solomon (12,9) check
|
||||||
encode(in, 9U, parity);
|
def RS129_check(_msg):
|
||||||
|
assert len(_msg) == 12, 'RS129_check error: Message not 12 bytes: %s' % print_hex(_msg)
|
||||||
|
|
||||||
return in[9U] == parity[2U] && in[10U] == parity[1U] && in[11U] == parity[0U];
|
parity = RS129_encode(_msg[:9])
|
||||||
}
|
return _msg[9] == parity[2] and msg[10] == parity[1] and msg[11] == parity[0];
|
||||||
'''
|
|
||||||
|
|
||||||
|
# For testing the code
|
||||||
|
def print_hex(_list):
|
||||||
|
print('[{}]'.format(', '.join(hex(x) for x in _list)))
|
||||||
|
|
||||||
# 00 00 00 00.00.01 00.00.02 82.65.6d
|
# 00 00 00 00.00.01 00.00.02 82.65.6d
|
||||||
parity = RS129_encode((0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x02))
|
|
||||||
for i in range(3):
|
message = [0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x02]
|
||||||
print(hex(parity[i]|MASK[i]))
|
parity = RS129_encode(message)
|
||||||
|
|
||||||
|
print_hex(message+parity)
|
||||||
|
|
||||||
|
print(RS129_check(message+parity))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user