// Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123) // added missing "if (denominator == 0)" check private int[] findErrorMagnitudes(GenericGFPoly errorEvaluator, int[] errorLocations) { // This is directly applying Forney's Formula int s = errorLocations.Length; int[] result = new int[s]; for (int i = 0; i < s; i++) { int xiInverse = field.inverse(errorLocations[i]); int denominator = 1; for (int j = 0; j < s; j++) { if (i != j) { denominator = field.multiply(denominator, GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); } } if (denominator == 0) { return(null); } result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); if (field.GeneratorBase != 0) { result[i] = field.multiply(result[i], xiInverse); } } return(result); }
private int[] findErrorLocations(GenericGFPoly errorLocator) { // This is a direct application of Chien's search int numErrors = errorLocator.Degree; if (numErrors == 1) { // shortcut return(new int[] { errorLocator.getCoefficient(1) }); } int[] result = new int[numErrors]; int e = 0; for (int i = 1; i < field.Size && e < numErrors; i++) { if (errorLocator.evaluateAt(i) == 0) { result[e] = field.inverse(i); e++; } } if (e != numErrors) { // throw new ReedSolomonException("Error locator degree does not match number of roots"); return(null); } return(result); }
/// <summary> /// <p>Decodes given set of received codewords, which include both data and error-correction /// codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, /// in the input.</p> /// </summary> /// <param name="received">data and error-correction codewords</param> /// <param name="twoS">number of error-correction codewords available</param> /// <param name="erasurePos">array of zero-based erasure indices</param> /// <returns>false: decoding fails</returns> public bool Decode(int[] received, int twoS, int[] erasurePos) { // Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123) // to add support for erasure and errata correction // most code ported to C# from the python code at http://en.wikiversity.org/wiki/Reed–Solomon_codes_for_coders if (received.Length >= field.Size) { throw new ArgumentException("Message is too long for this field", "received"); } if (twoS <= 0) { throw new ArgumentException("No error correction bytes provided", "twoS"); } var dataBytes = received.Length - twoS; if (dataBytes <= 0) { throw new ArgumentException("No data bytes provided", "twoS"); } var syndromeCoefficients = new int[twoS]; var noError = true; if (erasurePos == null) { erasurePos = new int[] { }; } else { for (var i = 0; i < erasurePos.Length; i++) { received[erasurePos[i]] = 0; } } if (erasurePos.Length > twoS) { return(false); } var poly = new GenericGFPoly(field, received, false); for (var i = 0; i < twoS; i++) { var eval = poly.evaluateAt(field.exp(i + field.GeneratorBase)); syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval; if (eval != 0) { noError = false; } } if (noError) { return(true); } var syndrome = new GenericGFPoly(field, syndromeCoefficients, false); var forneySyndrome = calculateForneySyndromes(syndrome, erasurePos, received.Length); var sigma = runBerlekampMasseyAlgorithm(forneySyndrome, erasurePos.Length); if (sigma == null) { return(false); } var errorLocations = findErrorLocations(sigma); if (errorLocations == null) { return(false); } // Prepare errors int[] errorPositions = new int[errorLocations.Length]; for (int i = 0; i < errorLocations.Length; i++) { errorPositions[i] = field.log(errorLocations[i]); } // Prepare erasures int[] erasurePositions = new int[erasurePos.Length]; for (int i = 0; i < erasurePos.Length; i++) { erasurePositions[i] = received.Length - 1 - erasurePos[i]; } // Combine errors and erasures int[] errataPositions = new int[errorPositions.Length + erasurePositions.Length]; Array.Copy(errorPositions, 0, errataPositions, 0, errorPositions.Length); Array.Copy(erasurePositions, 0, errataPositions, errorPositions.Length, erasurePositions.Length); var errataLocator = findErrataLocator(errataPositions); var omega = findErrorEvaluator(syndrome, errataLocator); if (omega == null) { return(false); } int[] errata = new int[errataPositions.Length]; for (int i = 0; i < errataPositions.Length; i++) { errata[i] = field.exp(errataPositions[i]); } var errorMagnitudes = findErrorMagnitudes(omega, errata); if (errorMagnitudes == null) { return(false); } for (var i = 0; i < errata.Length; i++) { var position = received.Length - 1 - field.log(errata[i]); if (position < 0) { // throw new ReedSolomonException("Bad error location"); return(false); } received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]); } var checkPoly = new GenericGFPoly(field, received, false); var error = false; for (var i = 0; i < twoS; i++) { var eval = checkPoly.evaluateAt(field.exp(i + field.GeneratorBase)); if (eval != 0) { error = true; } } if (error) { return(false); } return(true); }