|
|
|
@ -30,16 +30,24 @@ import java.util.Objects;
|
|
|
|
// Computes Reed-Solomon error correction codewords for given data codewords.
|
|
|
|
// Computes Reed-Solomon error correction codewords for given data codewords.
|
|
|
|
final class ReedSolomonGenerator {
|
|
|
|
final class ReedSolomonGenerator {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Constant ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// Use this memoizer to get instances of this class.
|
|
|
|
// Use this memoizer to get instances of this class.
|
|
|
|
public static final Memoizer<Integer,ReedSolomonGenerator> MEMOIZER
|
|
|
|
public static final Memoizer<Integer,ReedSolomonGenerator> MEMOIZER
|
|
|
|
= new Memoizer<>(ReedSolomonGenerator::new);
|
|
|
|
= new Memoizer<>(ReedSolomonGenerator::new);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Field ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// A table of size 256 * degree, where polynomialMultiply[i][j] = multiply(i, coefficients[j]).
|
|
|
|
// A table of size 256 * degree, where polynomialMultiply[i][j] = multiply(i, coefficients[j]).
|
|
|
|
// 'coefficients' is the temporary array computed in the constructor.
|
|
|
|
// 'coefficients' is the temporary array computed in the constructor.
|
|
|
|
private byte[][] polynomialMultiply;
|
|
|
|
private byte[][] polynomialMultiply;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Constructor ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a Reed-Solomon ECC generator polynomial for the given degree.
|
|
|
|
// Creates a Reed-Solomon ECC generator polynomial for the given degree.
|
|
|
|
private ReedSolomonGenerator(int degree) {
|
|
|
|
private ReedSolomonGenerator(int degree) {
|
|
|
|
if (degree < 1 || degree > 255)
|
|
|
|
if (degree < 1 || degree > 255)
|
|
|
|
@ -72,6 +80,9 @@ final class ReedSolomonGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Method ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the error correction codeword for the given data polynomial and this divisor polynomial.
|
|
|
|
// Returns the error correction codeword for the given data polynomial and this divisor polynomial.
|
|
|
|
public void getRemainder(byte[] data, int dataOff, int dataLen, byte[] result) {
|
|
|
|
public void getRemainder(byte[] data, int dataOff, int dataLen, byte[] result) {
|
|
|
|
Objects.requireNonNull(data);
|
|
|
|
Objects.requireNonNull(data);
|
|
|
|
@ -89,6 +100,9 @@ final class ReedSolomonGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---- Utility function ----*/
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
|
|
|
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
|
|
|
private static int multiply(int x, int y) {
|
|
|
|
private static int multiply(int x, int y) {
|
|
|
|
|