tomerfiliba-org / reedsolomon

ā³šŸ›” Pythonic universal errors-and-erasures Reed-Solomon codec to protect your data from errors and bitrot. Includes a future-proof zero-dependencies pure-python implementation šŸ”® and an optional speed-optimized Cython/C extension šŸš€
http://pypi.python.org/pypi/reedsolo
Other
352 stars 86 forks source link

interface to deal with fixed erasures #27

Open Sec42 opened 3 years ago

Sec42 commented 3 years ago

This is more a feature request. I have a RS encoding that is a bit strange. It uses 8 bytes of error correction, but instead of the expected encoding:

rs8a=reedsolo.RSCodec(nsym=8, fcr=0, prim=0x11d, c_exp=8)

it instead encodes for 16 bytes, but omits the last 8 bytes from the message.

Due to the way that reed-solomon can deal with known erasures it has the same correcting capabilities as the "standard" one.

I have written a small helper function to deal with this case:

# data: 31B, checksum: 8B, erasure: 8B - RS(47,31) - transmission omits last 8B
rs8=reedsolo.RSCodec(nsym=(8+8), fcr=0, prim=0x11d, c_exp=8)
rs8.elen=8

def decode_with_fixed_erasure(self, data, nsym=None, erase_pos=None, only_erasures=False):
    if nsym is None: nsym=self.nsym

    if self.elen > 0:
        data=data+([0]*self.elen)
        r=range(len(data)-self.elen,len(data))
        if erase_pos is None:
            erase_pos=r
        else:
            erase_pos=list(set(r)|set(erase_pos))
        (cmsg,crs,ep)=self.decode(data, nsym, erase_pos=erase_pos, only_erasures=only_erasures)
        return (cmsg, crs, bytearray(set(ep)-set(r)))
    else:
        return self.decode(data, nsym, erase_pos, only_erasures)

I wonder if this is something you would consider adding support for.

lrq3000 commented 1 year ago

Thank you for your suggestion and for sharing your code. I won't be the one implementing it, but I leave your issue open if someone else want to give it a try or has similar needs.