Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ReedSolomonEncoder"/> class.
 /// </summary>
 /// <param name="field">A <see cref="GenericGF"/> that represents the Galois field to use</param>
 public ReedSolomonEncoder(GenericGF field)
 {
     this.field            = field;
     this.cachedGenerators = new List <GenericGFPoly>();
     cachedGenerators.Add(new GenericGFPoly(field, new int[] { 1 }, true));
 }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ReedSolomonDecoder"/> class.
 /// </summary>
 /// <param name="field">A <see cref="GenericGF"/> that represents the Galois field to use</param>
 public ReedSolomonDecoder(GenericGF field)
 {
     this.field = field;
 }