Documentation ¶
Overview ¶
Package infectious implements Reed-Solomon forward error correction [1]. It uses the Berlekamp-Welch [2] error correction algorithm to achieve the ability to actually correct errors.
Caution: this package API leans toward providing the user more power and performance at the expense of having some really sharp edges! Read the documentation about memory lifecycles carefully!
We wrote a blog post about how this library works! https://innovation.vivint.com/introduction-to-reed-solomon-bc264d0794f8
[1] https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction [2] https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Welch_algorithm
Index ¶
- Variables
- type FEC
- func (fc *FEC) Correct(shares []Share) error
- func (f *FEC) Decode(dst []byte, shares []Share) ([]byte, error)
- func (f *FEC) Encode(input []byte, output func(Share)) error
- func (f *FEC) EncodeSingle(input, output []byte, num int) error
- func (f *FEC) Rebuild(shares []Share, output func(Share)) error
- func (f *FEC) Required() int
- func (f *FEC) Total() int
- type Share
Constants ¶
This section is empty.
Variables ¶
var ( TooManyErrors = errors.New("too many errors to reconstruct") )
Functions ¶
This section is empty.
Types ¶
type FEC ¶
type FEC struct {
// contains filtered or unexported fields
}
FEC represents operations performed on a Reed-Solomon-based forward error correction code. Make sure to construct using NewFEC.
func NewFEC ¶
NewFEC creates a *FEC using k required pieces and n total pieces. Encoding data with this *FEC will generate n pieces, and decoding data requires k uncorrupted pieces. If during decode more than k pieces exist, corrupted data can be detected and recovered from.
func (*FEC) Correct ¶
Correct implements the Berlekamp-Welch algorithm for correcting errors in given FEC encoded data. It will correct the supplied shares, mutating the underlying byte slices and reordering the shares
func (*FEC) Decode ¶
Decode will take a destination buffer (can be nil) and a list of shares (pieces). It will return the data passed in to the corresponding Encode call or return an error.
It will first correct the shares using Correct, mutating and reordering the passed-in shares arguments. Then it will rebuild the data using Rebuild. Finally it will concatenate the data into the given output buffer dst if it has capacity, growing it otherwise.
If you already know your data does not contain errors, Rebuild will be faster.
If you only want to identify which pieces are bad, you may be interested in Correct.
If you don't want the data concatenated for you, you can use Correct and then Rebuild individually.
func (*FEC) Encode ¶
Encode will take input data and encode to the total number of pieces n this *FEC is configured for. It will call the callback output n times.
The input data must be a multiple of the required number of pieces k. Padding to this multiple is up to the caller.
Note that the byte slices in Shares passed to output may be reused when output returns.
func (*FEC) EncodeSingle ¶
EncodeSingle will take input data and encode it to output only for the num piece.
The input data must be a multiple of the required number of pieces k. Padding to this multiple is up to the caller.
The output must be exactly len(input) / k bytes.
The num must be 0 <= num < n.
func (*FEC) Rebuild ¶
Rebuild will take a list of corrected shares (pieces) and a callback output. output will be called k times ((*FEC).Required() times) with 1/k of the original data each time and the index of that data piece. Decode is usually preferred.
Note that the data is not necessarily sent to output ordered by the piece number.
Note that the byte slices in Shares passed to output may be reused when output returns.
Rebuild assumes that you have already called Correct or did not need to.