Skip to content

Commit

Permalink
Fix multiple instances of RSCodec with different parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen L. <lrq3000@gmail.com>
  • Loading branch information
lrq3000 committed Jan 3, 2016
1 parent 4254e43 commit 7e8fa4e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,16 @@ This library is also thoroughly unit tested so that any encoding/decoding case s
However if you want to manually check if the repaired message is correct, you can do so:
>> rs.rs_check(rmes + recc, nsym)

Note: if you want to use multiple reedsolomon with different parameters, you need to backup the globals and restore them before calling reedsolo functions:
>> rs.init_tables()
>> global gf_log, gf_exp, field_charac
>> bak_gf_log, bak_gf_exp, bak_field_charac = gf_log, gf_exp, field_charac
Then at anytime, you can do:
>> global gf_log, gf_exp, field_charac
>> gf_log, gf_exp, field_charac = bak_gf_log, bak_gf_exp, bak_field_charac
>> mesecc = rs.rs_encode_msg(mes, nsym)
>> rmes, recc = rs.rs_correct_msg(mesecc, nsym)
The globals backup is not necessary if you use RSCodec, it will be automatically managed.

Read the sourcecode's comments for more infos about how it works, and for the various parameters you can setup if
you need to interface with other RS codecs.
13 changes: 11 additions & 2 deletions reedsolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _bytearray(obj = 0, encoding = "latin-1"):
for i in xrange(field_charac, field_charac * 2):
gf_exp[i] = gf_exp[i - field_charac]

return [gf_log, gf_exp]
return [gf_log, gf_exp, field_charac]

def gf_add(x, y):
return x ^ y
Expand Down Expand Up @@ -844,7 +844,7 @@ def __init__(self, nsym=10, nsize=255, fcr=0, prim=0x11d, generator=2, c_exp=8,
self.c_exp = c_exp # exponent of the field's characteristic. This both defines the maximum value per symbol and the maximum length of one chunk. By default it's GF(2^8), do not change if you're not sure what it means.

# Initialize the look-up tables for easy and quick multiplication/division
init_tables(prim, generator, c_exp)
self.gf_log, self.gf_exp, self.field_charac = init_tables(prim, generator, c_exp)
# Precompute the generator polynomials
if single_gen:
self.gen = {}
Expand All @@ -861,6 +861,10 @@ def chunk(self, data, chunksize):

def encode(self, data, nsym=None):
'''Encode a message (ie, add the ecc symbols) using Reed-Solomon, whatever the length of the message because we use chunking'''
# Restore precomputed tables (allow to use multiple RSCodec in one script)
global gf_log, gf_exp, field_charac
gf_log, gf_exp, field_charac = self.gf_log, self.gf_exp, self.field_charac

if not nsym:
nsym = self.nsym

Expand All @@ -877,6 +881,11 @@ def decode(self, data, nsym=None, erase_pos=None, only_erasures=False):
Usage: rmes, rmesecc = RSCodec.decode(data).
'''
# erase_pos is a list of positions where you know (or greatly suspect at least) there is an erasure (ie, wrong character but you know it's at this position). Just input the list of all positions you know there are errors, and this method will automatically split the erasures positions to attach to the corresponding data chunk.

# Restore precomputed tables (allow to use multiple RSCodec in one script)
global gf_log, gf_exp, field_charac
gf_log, gf_exp, field_charac = self.gf_log, self.gf_exp, self.field_charac

if not nsym:
nsym = self.nsym

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if USE_CYTHON: extensions = cythonize(extensions)

setup(name = "reedsolo",
version = "1.2.0",
version = "1.3.0",
description = "Pure-Python Reed Solomon encoder/decoder",
author = "Tomer Filiba",
author_email = "tomerfiliba@gmail.com",
Expand Down

0 comments on commit 7e8fa4e

Please sign in to comment.