reed_solomon_32/
lib.rs

1//! Reed-Solomon BCH encoder and decoder suitable for `no_std` environment in GF(2^5).
2//!
3//! This is a fork of <https://github.com/mersinvald/reed-solomon-rs> - the primary changes
4//! being that it operates in GF(2^5) instead of GF(2^8) and some significant revisions to
5//! the user facing API.
6//!
7//! This library implements block encoder and decoder: error correction code is appended to original data.
8//!
9//! # Example
10//! ```rust
11//! use reed_solomon_32::encode;
12//! use reed_solomon_32::correct;
13//!
14//! fn main() {
15//!     let data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
16//!
17//!     // Length of error correction code
18//!     let ecc_len = 8;
19//!
20//!     // Encode data
21//!     let encoded = encode(&data, ecc_len).unwrap();
22//!
23//!     // Simulate some transmission errors
24//!     let mut corrupted = *encoded;
25//!     for i in 0..4 {
26//!         corrupted[i] = 0x0;
27//!     }
28//!
29//!     // Try to recover data
30//!     let known_erasures = [0];
31//!     let recovered = correct(&mut corrupted, ecc_len, Some(&known_erasures)).unwrap();
32//!
33//!     let orig_str = std::str::from_utf8(&data).unwrap();
34//!     let recv_str = std::str::from_utf8(recovered.data()).unwrap();
35//!
36//!     println!("message:               {:?}", orig_str);
37//!     println!("original data:         {:?}", data);
38//!     println!("error correction code: {:?}", encoded.ecc());
39//!     println!("corrupted:             {:?}", corrupted);
40//!     println!("repaired:              {:?}", recv_str);
41//! }
42//! ```
43
44#![cfg_attr(not(feature = "std"), no_std)]
45
46const POLYNOMIAL_MAX_LENGTH: usize = 31;
47
48#[macro_use]
49mod macros;
50mod gf;
51mod encoder_impl;
52mod decoder_impl;
53mod buffer;
54mod err;
55
56pub use encoder_impl::encode;
57pub use decoder_impl::{correct, correct_err_count, is_corrupted};
58pub use err::{UsageError, CorrectionError, UsageErrorMessage};
59pub use buffer::Buffer;
60
61pub mod encoder {
62    //! This is a specialized module and generally the [`encode`](crate::encode)
63    //! function should be preferred.
64    //!
65    //! The downside of the `encode` function is that it will cause around 500 bytes of
66    //! data tables to be included in the final binary - with the majority of that data
67    //! only being useful for larger ECC sizes that are unlikely to be used (eg,
68    //! the table for ECC size 30 takes up 31 bytes, the table for ECC size 29 takes
69    //! up 30 bytes - but neither makes much sense in practical use).
70    //!
71    //! By using the constants in this module, at LTO pass may be able to remove unused
72    //! tables from the final binary. Not all targets will be able to take practical
73    //! advantage of this, however.
74    pub use crate::encoder_impl::{
75        Encoder,
76        ENCODER_0,
77        ENCODER_1,
78        ENCODER_2,
79        ENCODER_3,
80        ENCODER_4,
81        ENCODER_5,
82        ENCODER_6,
83        ENCODER_7,
84        ENCODER_8,
85        ENCODER_9,
86        ENCODER_10,
87        ENCODER_11,
88        ENCODER_12,
89        ENCODER_13,
90        ENCODER_14,
91        ENCODER_15,
92        ENCODER_16,
93        ENCODER_17,
94        ENCODER_18,
95        ENCODER_19,
96        ENCODER_20,
97        ENCODER_21,
98        ENCODER_22,
99        ENCODER_23,
100        ENCODER_24,
101        ENCODER_25,
102        ENCODER_26,
103        ENCODER_27,
104        ENCODER_28,
105        ENCODER_29,
106        ENCODER_30,
107    };
108}
109
110pub mod decoder {
111    //! This is a specialized module and generally [`correct_err_count`](crate::correct_err_count),
112    //! [`correct`](crate::correct), and [`is_corrupted`](crate::is_corrupted) functions
113    //! should be preferred.
114    //!
115    //! As of the current version of this crate, this module exists for future proofing
116    //! and to mirror the encode APIs. In the future, using these functions _may_
117    //! help decrease binary size. However, currently they do not significantly do so.
118    pub use crate::decoder_impl::{
119        Decoder,
120        DECODER_0,
121        DECODER_1,
122        DECODER_2,
123        DECODER_3,
124        DECODER_4,
125        DECODER_5,
126        DECODER_6,
127        DECODER_7,
128        DECODER_8,
129        DECODER_9,
130        DECODER_10,
131        DECODER_11,
132        DECODER_12,
133        DECODER_13,
134        DECODER_14,
135        DECODER_15,
136        DECODER_16,
137        DECODER_17,
138        DECODER_18,
139        DECODER_19,
140        DECODER_20,
141        DECODER_21,
142        DECODER_22,
143        DECODER_23,
144        DECODER_24,
145        DECODER_25,
146        DECODER_26,
147        DECODER_27,
148        DECODER_28,
149        DECODER_29,
150        DECODER_30,
151    };
152}