From c0e21b06f657135f3ffe1d647130419a70e530db Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 17 Oct 2014 19:39:09 +0200 Subject: Fix EDI analysis RS check --- edi/edidebug.py | 28 +++++++++++++++++++++++----- edi/reedsolo.py | 16 +++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) (limited to 'edi') diff --git a/edi/edidebug.py b/edi/edidebug.py index c27a374..ab61ab7 100755 --- a/edi/edidebug.py +++ b/edi/edidebug.py @@ -234,23 +234,38 @@ def get_rs_decoder(chunk_size, zeropad): # p.hexpr(" ZE CHUNK DATA", c[0]); # p.hexpr(" ZE CHUNK PROT", c[1]); - rs_codec = RSCodec(48) + rs_codec = RSCodec(48, fcr=1) + protection_ok = True for chunk, protection in rs_chunks: - p.pr(" Protection") + #p.pr(" Protection") #p.hexpr(" OF ZE CHUNK DATA", chunk); - recalc_protection = rs_codec.encode(bytearray(chunk))[-48:] - if (protection != recalc_protection): + + bchunk = bytearray(chunk) + padbytes = 255-(48 + len(chunk)) + bchunk = bchunk + bytearray(0 for i in range(padbytes)) + recalc_protection = rs_codec.encode(bchunk)[-48:] + if protection != recalc_protection: p.pr(" PROTECTION ERROR") p.hexpr(" orig", protection) p.hexpr(" calc", recalc_protection) + protection_ok = False + else: + p.pr(" PROTECTION OK") + + if protection_ok: + p.pr("Protection check: OK") + afpacket = "".join(data for (data, protection) in rs_chunks) #p.hexpr(" ZE AF PACKET", afpacket) - return decode_af(afpacket[0:-zeropad]) + if zeropad: + return decode_af(afpacket[0:-zeropad]) + else: + return decode_af(afpacket) return decode_rs @@ -268,6 +283,9 @@ def decode_af(in_data, is_stream=False): else: headerdata = in_data[:10] + if len(headerdata) != 10: + p.hexpr("AF Header", headerdata) + sync, plen, seq, ar, pt = struct.unpack(af_head_struct, headerdata) if sync != "AF": diff --git a/edi/reedsolo.py b/edi/reedsolo.py index c310d22..1ba0243 100644 --- a/edi/reedsolo.py +++ b/edi/reedsolo.py @@ -65,7 +65,8 @@ class ReedSolomonError(Exception): pass -gf_exp = [1] * 512 +gf_exp = [0] * 512 +gf_exp[0] = 1 gf_log = [0] * 256 x = 1 for i in range(1, 255): @@ -113,16 +114,16 @@ def gf_poly_eval(p, x): y = gf_mul(y, x) ^ p[i] return y -def rs_generator_poly(nsym): +def rs_generator_poly(nsym, fcr=0): g = [1] - for i in range(0, nsym): + for i in range(fcr, nsym+fcr): g = gf_poly_mul(g, [1, gf_exp[i]]) return g -def rs_encode_msg(msg_in, nsym): +def rs_encode_msg(msg_in, nsym, fcr=0): if len(msg_in) + nsym > 255: raise ValueError("message too long") - gen = rs_generator_poly(nsym) + gen = rs_generator_poly(nsym, fcr) msg_out = bytearray(len(msg_in) + nsym) msg_out[:len(msg_in)] = msg_in for i in range(0, len(msg_in)): @@ -229,8 +230,9 @@ class RSCodec(object): The ``nsym`` argument is the length of the correction code, and it determines the number of error bytes (if I understand this correctly, half of ``nsym`` is correctable) """ - def __init__(self, nsym=10): + def __init__(self, nsym=10, fcr=0): self.nsym = nsym + self.fcr = fcr def encode(self, data): if isinstance(data, str): @@ -239,7 +241,7 @@ class RSCodec(object): enc = bytearray() for i in range(0, len(data), chunk_size): chunk = data[i:i+chunk_size] - enc.extend(rs_encode_msg(chunk, self.nsym)) + enc.extend(rs_encode_msg(chunk, self.nsym, self.fcr)) return enc def decode(self, data): -- cgit v1.2.3