internal GenericGFPoly addOrSubtract(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (isZero) { return(other); } if (other.isZero) { return(this); } int[] smallerCoefficients = this.coefficients; int[] largerCoefficients = other.coefficients; if (smallerCoefficients.Length > largerCoefficients.Length) { int[] temp = smallerCoefficients; smallerCoefficients = largerCoefficients; largerCoefficients = temp; } int[] sumDiff = new int[largerCoefficients.Length]; int lengthDiff = largerCoefficients.Length - smallerCoefficients.Length; // Copy high-order terms only found in higher-degree polynomial's coefficients Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff); for (int i = lengthDiff; i < largerCoefficients.Length; i++) { sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return(new GenericGFPoly(field, sumDiff, encoding)); }
/// <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); }