internal GenericGFPoly[] divide(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (other.isZero) { throw new ArgumentException("Divide by 0", "other"); } GenericGFPoly quotient = new GenericGFPoly(field, new int[] { 0 }, encoding); GenericGFPoly remainder = this; int denominatorLeadingTerm = other.getCoefficient(other.Degree); int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm); while (remainder.Degree >= other.Degree && !remainder.isZero) { int degreeDifference = remainder.Degree - other.Degree; int scale = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm); GenericGFPoly term = other.multiplyByMonomial(degreeDifference, scale); GenericGFPoly iterationQuotient = buildMonomial(degreeDifference, scale); quotient = quotient.addOrSubtract(iterationQuotient); remainder = remainder.addOrSubtract(term); } return(new GenericGFPoly[] { quotient, remainder }); }
/// <summary> /// Encodes given set of data codewords with Reed-Solomon. /// </summary> /// <param name="toEncode">data codewords and padding, the amount of padding should match /// the number of error-correction codewords to generate. After encoding, the padding is /// replaced with the error-correction codewords</param> /// <param name="ecBytes">number of error-correction codewords to generate</param> public void Encode(int[] toEncode, int ecBytes) { // Method modified by Sonic-The-Hedgehog-LNK1123 (github.com/Sonic-The-Hedgehog-LNK1123) // added check for messages that are too long for the used Galois field if (toEncode.Length >= field.Size) { throw new ArgumentException("Message is too long for this field", "toEncode"); } if (ecBytes <= 0) { throw new ArgumentException("No error correction bytes provided", "ecBytes"); } var dataBytes = toEncode.Length - ecBytes; if (dataBytes <= 0) { throw new ArgumentException("No data bytes provided", "ecBytes"); } var generator = buildGenerator(ecBytes); var infoCoefficients = new int[dataBytes]; Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes); var info = new GenericGFPoly(field, infoCoefficients, true); info = info.multiplyByMonomial(ecBytes, 1); var remainder = info.divide(generator)[1]; var coefficients = remainder.Coefficients; var numZeroCoefficients = ecBytes - coefficients.Length; for (var i = 0; i < numZeroCoefficients; i++) { toEncode[dataBytes + i] = 0; } Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length); }