pub struct ReedSolomon { /* private fields */ }Expand description
A Reed-Solomon codec over GF(256) for codewords of at most 255 bytes.
The codec is parameterized by its error-correction capability: the
number of byte errors it can correct, each costing two parity bytes.
Codewords are laid out as parity || data.
Implementations§
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn apply_corrections(target: &mut [u8], corrections: impl AsRef<[u8]>)
pub fn apply_corrections(target: &mut [u8], corrections: impl AsRef<[u8]>)
XORs corrections onto target, element-wise.
The two slices are zipped, so the tail of the longer one is ignored; a zero correction byte leaves its target byte unchanged.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn apply_corrections_detached(
parity: &mut [u8],
data: &mut [u8],
corrections: impl AsRef<[u8]>,
)
pub fn apply_corrections_detached( parity: &mut [u8], data: &mut [u8], corrections: impl AsRef<[u8]>, )
Applies XOR corrections to a detached (parity, data) pair.
The corrections slice is laid out parity-first, matching the
parity || data codeword layout: its first parity.len() bytes are
XORed onto parity, and the remaining bytes onto data.
Like ReedSolomon::apply_corrections, this method uses truncating
zip semantics. If corrections holds fewer bytes than parity and
data combined, the uncovered tail is left unmodified; excess
correction bytes are ignored.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn compute_errors(
&self,
length: u8,
syndromes: &Polynomial,
) -> Result<Option<Polynomial>, RSComputeErrorsError>
pub fn compute_errors( &self, length: u8, syndromes: &Polynomial, ) -> Result<Option<Polynomial>, RSComputeErrorsError>
Computes errors in a received codeword.
§Parameters
length: full length of the codeword, including parity bytessyndromes: syndrome polynomial
§Errors
RSComputeErrorsError::GFErrorif an arithmetic operation failsRSComputeErrorsError::EuclideanErrorif the Euclidean algorithm failsRSComputeErrorsError::TooManyErrorsif the input is unrecoverableRSComputeErrorsError::ZeroErrorLocatorDerivativeif the error locator derivative evaluates to zero
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn compute_syndromes(num_parity_bytes: u8, received: &[u8]) -> Polynomial
pub fn compute_syndromes(num_parity_bytes: u8, received: &[u8]) -> Polynomial
Computes the syndromes of a given codeword.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn compute_syndromes_detached(
parity: &[u8],
data: &[u8],
) -> Result<Polynomial, RSConstructorError>
pub fn compute_syndromes_detached( parity: &[u8], data: &[u8], ) -> Result<Polynomial, RSConstructorError>
Computes the syndromes of a given detached codeword.
An empty parity slice yields zero syndromes, so a pair carrying no
parity always validates as pristine.
§Errors
RSConstructorError::ParityTooHighis returned ifparityholds more thanMAX_PARITY_BYTESbytes.RSConstructorError::OddParityLengthis returned ifparityholds an odd number of bytes; parity always comprises two bytes per correctable error.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn correct<'lt>(
&self,
received: &'lt [u8],
) -> Result<Cow<'lt>, RSDecodeError>
pub fn correct<'lt>( &self, received: &'lt [u8], ) -> Result<Cow<'lt>, RSDecodeError>
Corrects a received codeword, returning the corrected codeword.
§Errors
RSDecodeError::InsufficientLengthis returned ifreceivedholds fewer bytes thanReedSolomon::parity_bytes.std::num::TryFromIntErroris returned ifreceivedholds more than 255 bytes.ps_buffer::BufferErroris returned if memory allocation fails.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the corrected bytes fail validation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn correct_detached<'lt>(
parity: &[u8],
data: &'lt [u8],
) -> Result<Codeword<'lt>, RSDecodeError>
pub fn correct_detached<'lt>( parity: &[u8], data: &'lt [u8], ) -> Result<Codeword<'lt>, RSDecodeError>
Corrects a message based on detached parity bytes.
§Errors
RSConstructorErroris returned ifparityholds more thanMAX_PARITY_BYTESbytes, or an odd number of bytes.std::num::TryFromIntErroris returned ifparityanddatatogether hold more than 255 bytes.ps_buffer::BufferErroris returned if memory allocation fails.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the corrected bytes fail validation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn correct_detached_data_in_place(
parity: &[u8],
data: &mut [u8],
) -> Result<(), RSDecodeError>
pub fn correct_detached_data_in_place( parity: &[u8], data: &mut [u8], ) -> Result<(), RSDecodeError>
Corrects a message based on detached parity bytes.
§Errors
RSConstructorErroris returned ifparityholds more thanMAX_PARITY_BYTESbytes, or an odd number of bytes.std::num::TryFromIntErroris returned ifparityanddatatogether hold more than 255 bytes.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the corrected bytes fail validation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn correct_detached_in_place(
parity: &mut [u8],
data: &mut [u8],
) -> Result<(), RSDecodeError>
pub fn correct_detached_in_place( parity: &mut [u8], data: &mut [u8], ) -> Result<(), RSDecodeError>
Corrects a message based on detached parity bytes. Also corrects the parity bytes.
§Errors
RSConstructorErroris returned ifparityholds more thanMAX_PARITY_BYTESbytes, or an odd number of bytes.std::num::TryFromIntErroris returned ifparityanddatatogether hold more than 255 bytes.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the corrected bytes fail validation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn correct_in_place(&self, received: &mut [u8]) -> Result<(), RSDecodeError>
pub fn correct_in_place(&self, received: &mut [u8]) -> Result<(), RSDecodeError>
Corrects a received codeword in-place.
§Errors
RSDecodeError::InsufficientLengthis returned ifreceivedholds fewer bytes thanReedSolomon::parity_bytes.std::num::TryFromIntErroris returned ifreceivedholds more than 255 bytes.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the data is unrecoverable.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn decode<'lt>(
&self,
received: &'lt [u8],
) -> Result<Codeword<'lt>, RSDecodeError>
pub fn decode<'lt>( &self, received: &'lt [u8], ) -> Result<Codeword<'lt>, RSDecodeError>
Decodes a received codeword, correcting errors if possible.
§Errors
RSDecodeError::InsufficientLengthis returned ifreceivedholds fewer bytes thanReedSolomon::parity_bytes.std::num::TryFromIntErroris returned ifreceivedholds more than 255 bytes.ps_buffer::BufferErroris returned if memory allocation fails.RSComputeErrorsErroris propagated fromReedSolomon::compute_errors.RSDecodeError::TooManyErrorsis returned if the corrected bytes fail validation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn encode(&self, message: &[u8]) -> Result<Buffer, RSEncodeError>
pub fn encode(&self, message: &[u8]) -> Result<Buffer, RSEncodeError>
Encodes a message into a codeword.
§Errors
ps_buffer::BufferErroris returned if memory allocation fails.RSEncodeError::RSGenerateParityErrorif parity generation fails.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn generate_parity(
&self,
message: &[u8],
) -> Result<ParityBytes, RSGenerateParityError>
pub fn generate_parity( &self, message: &[u8], ) -> Result<ParityBytes, RSGenerateParityError>
Generates parity bytes.
§Errors
RSGenerateParityError::SetCoefficientsis returned if2 * parity + message.len()exceeds 255.RSGenerateParityError::Divisionis returned if the generator polynomial is zero (not expected to occur).
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub const fn new(parity: u8) -> Result<Self, RSConstructorError>
pub const fn new(parity: u8) -> Result<Self, RSConstructorError>
Creates a new Reed-Solomon codec with the given error-correction capability.
§Errors
RSConstructorError::ParityTooHighis returned ifparityexceedsMAX_PARITY.
Source§impl ReedSolomon
impl ReedSolomon
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub const fn parity_bytes(&self) -> u8
pub const fn parity_bytes(&self) -> u8
Returns the number of parity bytes per codeword: 2 * parity, two
per correctable error.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn validate(&self, received: &[u8]) -> Option<Polynomial>
pub fn validate(&self, received: &[u8]) -> Option<Polynomial>
Validates a received codeword.
Returns None if received is a valid codeword, or Some(syndromes)
if errors are detected.
An input holding fewer bytes than ReedSolomon::parity_bytes, or
more than Polynomial::MAX_COEFFICIENTS (255) bytes, can never be
a valid codeword and always yields Some; for such inputs the
contained syndrome polynomial may be zero and is not usable for
error computation.
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn validate_detached(
parity: &[u8],
data: &[u8],
) -> Result<Option<Polynomial>, RSConstructorError>
pub fn validate_detached( parity: &[u8], data: &[u8], ) -> Result<Option<Polynomial>, RSConstructorError>
Validates a segregated (parity, data) pair.
Returns Ok(None) if valid, or Ok(Some(syndromes)) if errors are
detected. An empty parity slice carries no parity information, so
a pair within the length bound validates trivially.
A pair whose combined length exceeds
Polynomial::MAX_COEFFICIENTS (255) bytes can never be a valid
codeword and always yields Ok(Some); for such inputs the contained
syndrome polynomial may be zero and is not usable for error
computation.
§Errors
RSConstructorError::ParityTooHighis returned ifparityholds more thanMAX_PARITY_BYTESbytes.RSConstructorError::OddParityLengthis returned ifparityholds an odd number of bytes.
Trait Implementations§
Source§impl Clone for ReedSolomon
impl Clone for ReedSolomon
Source§fn clone(&self) -> ReedSolomon
fn clone(&self) -> ReedSolomon
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more