Пример #1
0
		public void decodeTest16()
		{
			RsDecode16 target = new RsDecode16(4);
			byte[] data = new byte[1024];
			byte[] parity = new byte[] { 193, 3, 222, 151, 9, 2, 128, 246 };
			byte[] dataplus = new byte[data.Length + parity.Length];
			Random rnd = new Random(2314);
			rnd.NextBytes(data);
			Array.Copy(data, 0, dataplus, 0, data.Length);
			Array.Copy(parity, 0, dataplus, data.Length, parity.Length);

			for (int i = 0; i < 1000; i++)
			{
				int pos1 = (int)(rnd.NextDouble() * 1023);
				int pos2 = (int)(rnd.NextDouble() * 1023);
				if (Math.Abs(pos1 - pos2) < 4)
					pos2 = (pos1 + 4) % 1024;
				dataplus[pos1] = (byte)(rnd.NextDouble() * 255);
				dataplus[pos2] = (byte)(rnd.NextDouble() * 255);
				if (data[pos1] == dataplus[pos1]) dataplus[pos1] ^= 1;
				if (data[pos2] == dataplus[pos2]) dataplus[pos2] ^= 8;
				
				//dataplus[(int)(rnd.NextDouble() * 1023)] = (byte)(rnd.NextDouble() * 255);

				int errors;
				bool ok = target.decode(dataplus, 0, dataplus.Length, false, out errors);
				byte[] fixed_data = new byte[data.Length];
				Array.Copy(dataplus, fixed_data, data.Length);

				Assert.AreEqual(true, ok, "Fail");
				Assert.AreEqual(2, errors, "Errors");
				CollectionAssert.AreEqual(data, fixed_data);
			}
		}
Пример #2
0
        public unsafe CDRepairFix VerifyParity(ushort[,] syn2, uint crc, int actualOffset)
		{
            int npar2 = syn2.GetLength(1);
            int npar = Math.Min(AccurateRipVerify.maxNpar, npar2);
            var erroff = new int[stride * npar / 2];
            var forney = new ushort[stride * npar / 2];
            var syn1 = ar.GetSyndrome(npar, -1, -actualOffset);
            var rs = new RsDecode16(npar, this.galois);
            CDRepairFix fix = new CDRepairFix(this, npar);

            fix.actualOffset = actualOffset;
            fix.correctableErrors = 0;
            fix.hasErrors = false;
            fix.canRecover = true;

			fixed (ushort *psyn2 = syn2, psyn1 = syn1)
			{
                int sfLen = npar / 2 + 2;
                int ofLen = npar / 2 + 1;
                int efLen = npar / 2;
                int* _sigma = stackalloc int[npar / 2 + 2];
                int* _omega = stackalloc int[npar / 2 + 1];
                int* _errpos = stackalloc int[npar / 2];
                int* syn = stackalloc int[npar];
				int offset = fix.actualOffset;

				for (int part2 = 0; part2 < stride; part2++)
				{
					ushort* syn1part = psyn1 + part2 * npar;
                    ushort* syn2part = psyn2 + part2 * npar;
					int err = 0;

					for (int i = 0; i < npar; i++)
					{
                        var synI = syn1part[i] ^ syn2part[i];
                        syn[i] = synI;
                        err |= synI;
					}

                    if (err != 0)
                    {
                        int errcount = rs.calcSigmaMBM(_sigma, syn);
                        fix.hasErrors = true;
                        if (errcount <= 0 || errcount > efLen || !rs.chienSearch(_errpos, stridecount, errcount, _sigma))
                        {
                            fix.canRecover = false;
                            return fix;
                        }

                        galois.mulPoly(_omega, _sigma, syn, ofLen, sfLen, npar);

                        for (int i = 0; i < errcount; i++)
                        {
                            int pos = galois.toPos(stridecount, _errpos[i]) * stride + part2;
                            int erroffi = stride + pos + pregap * 2 - actualOffset * 2;
                            ushort diff = (ushort)this.galois.doForney(errcount, _errpos[i], _sigma, _omega);
                            if (erroffi < pregap * 2 || erroffi >= finalSampleCount * 2)
                            {
                                fix.canRecover = false;
                                return fix;
                            }
                            crc ^= Crc32.Combine(Crc32.ComputeChecksum(Crc32.ComputeChecksum(0, (byte)diff), (byte)(diff >> 8)), 0, (stridecount * stride - pos - 1) * 2);
                            erroff[fix.correctableErrors] = erroffi;
                            forney[fix.correctableErrors] = diff;
                            fix.correctableErrors++;
                        }
                    }
				}

                crc ^= ar.CTDBCRC(-actualOffset);
                if (crc != 0)
                {
                    fix.canRecover = false;
                    return fix;
                }
			}

            fix.erroffsorted = new int[fix.correctableErrors];
            fix.forneysorted = new ushort[fix.correctableErrors];
            for (int i = 0; i < fix.correctableErrors; i++)
            {
                fix.erroffsorted[i] = erroff[i];
                fix.forneysorted[i] = forney[i];
            }
            Array.Sort<int, ushort>(fix.erroffsorted, fix.forneysorted, 0, fix.correctableErrors);
            return fix;
		}
Пример #3
0
        public unsafe bool FindOffset(ushort[,] syn2, uint expectedCRC, out int actualOffset, out bool hasErrors)
        {
            int npar2 = syn2.GetLength(1);
            int npar = Math.Min(AccurateRipVerify.maxNpar, npar2);

            if (npar2 != npar)
                throw new Exception("npar mismatch");
            if (ar.Position != ar.FinalSampleCount)
                throw new Exception("ar.Position != ar.FinalSampleCount");

            var rs = new RsDecode16(npar, this.galois);
            fixed (ushort* syn2part = syn2)
            {
                int* _sigma = stackalloc int[npar];
                int* _errpos = stackalloc int[npar];
                int* syn = stackalloc int[npar];
                int bestOffset = 0;
                int bestOffsetErrors = npar / 2;

                // fast search
                for (int offset = 1 - stride / 2; offset < stride / 2; offset++)
                {
                    var syn1 = ar.GetSyndrome(npar, 1, -offset);
                    int err = 0;
                    for (int i = 0; i < npar; i++)
                    {
                        int synI = syn1[0, i] ^ syn2part[i];
                        syn[i] = synI;
                        err |= synI;
                    }
                    if (err == 0)
                    {
                        actualOffset = offset;
                        hasErrors = ar.CTDBCRC(-offset) != expectedCRC;
                        return true;
                    }
                    int err_count = rs.calcSigmaMBM(_sigma, syn);
                    if (err_count > 0 && err_count < bestOffsetErrors && rs.chienSearch(_errpos, stridecount, err_count, _sigma))
                    {
                        bestOffset = offset;
                        bestOffsetErrors = err_count;
                    }
                }

                if (bestOffsetErrors < npar / 2)
                {
                    actualOffset = bestOffset;
                    hasErrors = true;
                    return true;
                }
            }
            actualOffset = 0;
            hasErrors = true;
            return false;
        }