Exemplo n.º 1
0
 internal static Polynomial GetErrorCorrectPolynomial(int errorCorrectLength)
 {
     Polynomial a = new Polynomial(new int[] {1});
     for (int i = 0; i < errorCorrectLength; i++) {
     a = a.Multiply(new Polynomial(new int[] { 1, QRMath.Gexp(i) }));
     }
     return a;
 }
Exemplo n.º 2
0
        public Polynomial Mod(Polynomial e)
        {
            if (GetLength() - e.GetLength() < 0) {
            return this;
            }

            int ratio = QRMath.Glog(Get(0)) - QRMath.Glog(e.Get(0));
            int[] num = new int[GetLength()];
            for (int i = 0; i < GetLength(); i++) {
            num[i] = Get(i);
            }

            for (int i = 0; i < e.GetLength(); i++) {
            num[i] ^= QRMath.Gexp(QRMath.Glog(e.Get(i)) + ratio);
            }

            return new Polynomial(num).Mod(e);
        }
Exemplo n.º 3
0
        public Polynomial Multiply(Polynomial e)
        {
            int[] num = new int[GetLength() + e.GetLength() - 1];
            for (int i = 0; i < GetLength(); i++) {
            for (int j = 0; j < e.GetLength(); j++) {
                num[i + j] ^= QRMath.Gexp(QRMath.Glog(Get(i)) + QRMath.Glog(e.Get(j)));
            }
            }

            return new Polynomial(num);
        }
Exemplo n.º 4
0
        private static byte[] CreateBytes(BitBuffer buffer, RSBlock[] rsBlocks)
        {
            int offset = 0;
            int maxDcCount = 0;
            int maxEcCount = 0;

            int[][] dcdata = new int[rsBlocks.Length][];
            int[][] ecdata = new int[rsBlocks.Length][];

            for (int r = 0; r < rsBlocks.Length; r++) {
            int dcCount = rsBlocks[r].GetDataCount();
            int ecCount = rsBlocks[r].GetTotalCount() - dcCount;

            maxDcCount = Math.Max(maxDcCount, dcCount);
            maxEcCount = Math.Max(maxEcCount, ecCount);

            dcdata[r] = new int[dcCount];
            for (int i = 0; i < dcdata[r].Length; i++) {
                dcdata[r][i] = 0xff & buffer.GetBuffer()[i + offset];
            }
            offset += dcCount;

            Polynomial rsPoly = QRUtil.GetErrorCorrectPolynomial(ecCount);
            Polynomial rawPoly = new Polynomial(dcdata[r], rsPoly.GetLength() - 1);

            Polynomial modPoly = rawPoly.Mod(rsPoly);
            ecdata[r] = new int[rsPoly.GetLength() - 1];
            for (int i = 0; i < ecdata[r].Length; i++) {
                int modIndex = i + modPoly.GetLength() - ecdata[r].Length;
                ecdata[r][i] = (modIndex >= 0)? modPoly.Get(modIndex) : 0;
            }
            }

            int totalCodeCount = 0;
            for (int i = 0; i < rsBlocks.Length; i++) {
            totalCodeCount += rsBlocks[i].GetTotalCount();
            }

            byte[] data = new byte[totalCodeCount];
            int index = 0;
            for (int i = 0; i < maxDcCount; i++) {
            for (int r = 0; r < rsBlocks.Length; r++) {
                if (i < dcdata[r].Length) {
                    data[index++] = (byte) dcdata[r][i];
                }
            }
            }

            for (int i = 0; i < maxEcCount; i++) {
            for (int r = 0; r < rsBlocks.Length; r++) {
                if (i < ecdata[r].Length) {
                    data[index++] = (byte) ecdata[r][i];
                }
            }
            }

            return data;
        }