Reed-Solomon Encoder/Decoder

1.7.0 · active · verified Sat Apr 11

reedsolo is a pure-Python library providing a universal errors-and-erasures Reed-Solomon codec for data protection against errors and bitrot. It includes a fallback pure-Python implementation and an optional speed-optimized Cython/C extension. The library primarily focuses on burst-type errors, making it well-suited for data storage protection. The current stable version is 1.7.0, with a major 2.x branch actively in beta (2.1.1b1) introducing significant changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode a message using a specified number of ECC symbols and then decode it after introducing simulated errors. The `RSCodec` class handles the core encoding and decoding operations. The output type (bytearray or bytes) is matched to the input type.

from reedsolo import RSCodec, ReedSolomonError

# Initialize RSCodec with the number of error correction (ECC) symbols
# 10 ECC symbols allow correction of up to 5 byte-level errors (nsym/2)
rsc = RSCodec(10)

original_message = b'hello world'
print(f"Original: {original_message}")

# Encode the message
encoded_message = rsc.encode(original_message)
print(f"Encoded: {encoded_message}")

# Simulate some errors (e.g., change 'o' to 'X' in 'world')
tampered_message = bytearray(encoded_message)
tampered_message[4] = ord(b'X') # 'o' in 'hello'
tampered_message[8] = ord(b'X') # 'o' in 'world'
tampered_message[12] = ord(b'X') # ECC part
print(f"Tampered: {tampered_message}")

# Decode and correct errors
try:
    # decode returns (decoded_message, corrected_ecc_symbols, errata_positions)
    decoded_message, _, _ = rsc.decode(tampered_message)
    print(f"Decoded: {decoded_message}")

    if original_message == decoded_message:
        print("Decoding successful, message recovered.")
    else:
        print("Decoding failed or original message not fully recovered.")

except ReedSolomonError as e:
    print(f"Decoding failed: {e}")

view raw JSON →