public bool Decode(int[] received, int ecBytes)
        {
            if (ecBytes == 0)
            {
                throw new ArgumentException("No error correction bytes");
            }

            var dataBytes = received.Length - ecBytes;
            if (dataBytes <= 0)
            {
                throw new ArgumentException("No data bytes provided");
            }

            var errorsCorrected = false;
            var generator = BuildGenerator(ecBytes);
            var count = 0;
            GenericGFPoly remainder = null;
            var iteratorTemp = new int[received.Length];
            Array.Copy(received, iteratorTemp, received.Length);
            var tempInfo = new GenericGFPoly(_field, iteratorTemp);

            while (count <= dataBytes)
            {
                remainder = tempInfo.divide(generator)[1];

                if (remainder.Coefficients.Count(coeff => coeff != 0) <= ecBytes / 2)
                {
                    errorsCorrected = true;
                    break;
                }

                count++;
                Array.Copy(received, 0, iteratorTemp, count, received.Length - count);
                Array.Copy(received, received.Length - count, iteratorTemp, 0, count);
                tempInfo = new GenericGFPoly(_field, iteratorTemp);
            }

            if (!errorsCorrected)
            {
                return false;
            }
            if (count == 0)
            {
                return true;
            }

            var correctedInfo = tempInfo.addOrSubtract(remainder);

            var missingZerosCorrection = received.Length - correctedInfo.Coefficients.Length;
            correctedInfo = correctedInfo.multiplyByMonomial(missingZerosCorrection, 1);
            correctedInfo = correctedInfo.shiftLeft(count - missingZerosCorrection);

            missingZerosCorrection = received.Length - correctedInfo.Coefficients.Length;
            correctedInfo = correctedInfo.multiplyByMonomial(missingZerosCorrection, 1);

            Array.Copy(correctedInfo.Coefficients, 0, received, missingZerosCorrection, correctedInfo.Coefficients.Length - missingZerosCorrection);
            Array.Copy(correctedInfo.Coefficients, correctedInfo.Coefficients.Length - missingZerosCorrection, received, 0, missingZerosCorrection);

            return true;
        }
Пример #2
0
        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");
            }

            GenericGFPoly quotient  = field.Zero;
            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 = field.buildMonomial(degreeDifference, scale);
                quotient  = quotient.addOrSubtract(iterationQuotient);
                remainder = remainder.addOrSubtract(term);
            }

            return(new GenericGFPoly[] { quotient, remainder });
        }
        public bool Decode(int[] received, int ecBytes)
        {
            if (ecBytes == 0)
            {
                throw new ArgumentException("No error correction bytes");
            }

            var dataBytes = received.Length - ecBytes;

            if (dataBytes <= 0)
            {
                throw new ArgumentException("No data bytes provided");
            }

            var           errorsCorrected = false;
            var           generator       = BuildGenerator(ecBytes);
            var           count           = 0;
            GenericGFPoly remainder       = null;
            var           iteratorTemp    = new int[received.Length];

            Array.Copy(received, iteratorTemp, received.Length);
            var tempInfo = new GenericGFPoly(_field, iteratorTemp);

            while (count <= dataBytes)
            {
                remainder = tempInfo.divide(generator)[1];

                if (remainder.Coefficients.Count(coeff => coeff != 0) <= ecBytes / 2)
                {
                    errorsCorrected = true;
                    break;
                }

                count++;
                Array.Copy(received, 0, iteratorTemp, count, received.Length - count);
                Array.Copy(received, received.Length - count, iteratorTemp, 0, count);
                tempInfo = new GenericGFPoly(_field, iteratorTemp);
            }

            if (!errorsCorrected)
            {
                return(false);
            }
            if (count == 0)
            {
                return(true);
            }

            var correctedInfo = tempInfo.addOrSubtract(remainder);

            var missingZerosCorrection = received.Length - correctedInfo.Coefficients.Length;

            correctedInfo = correctedInfo.multiplyByMonomial(missingZerosCorrection, 1);
            correctedInfo = correctedInfo.shiftLeft(count - missingZerosCorrection);

            missingZerosCorrection = received.Length - correctedInfo.Coefficients.Length;
            correctedInfo          = correctedInfo.multiplyByMonomial(missingZerosCorrection, 1);

            Array.Copy(correctedInfo.Coefficients, 0, received, missingZerosCorrection, correctedInfo.Coefficients.Length - missingZerosCorrection);
            Array.Copy(correctedInfo.Coefficients, correctedInfo.Coefficients.Length - missingZerosCorrection, received, 0, missingZerosCorrection);

            return(true);
        }