Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        private byte[] CreateData(int errorCorrectLevel)
        {
            RSBlock[] rsBlocks = RSBlock.GetRSBlocks(errorCorrectLevel);

            BitBuffer buffer = new BitBuffer();
            buffer.Put(4, 4);
            buffer.Put(qrData.Length, 8);
            for (int i = 0; i < qrData.Length; i++) {
            buffer.Put(qrData[i], 8);
            }

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

            if (buffer.GetLengthInBits() > totalDataCount * 8) {
            throw new ArgumentException("String length overflow. ("
                + buffer.GetLengthInBits()
                + ">"
                +  totalDataCount * 8
                + ")");
            }

            if (buffer.GetLengthInBits() + 4 <= totalDataCount * 8) {
            buffer.Put(0, 4);
            }

            // padding
            while (buffer.GetLengthInBits() % 8 != 0) {
            buffer.Put(false);
            }

            // padding
            while (true) {
            if (buffer.GetLengthInBits() >= totalDataCount * 8) {
                break;
            }
            buffer.Put(PAD0, 8);

            if (buffer.GetLengthInBits() >= totalDataCount * 8) {
                break;
            }
            buffer.Put(PAD1, 8);
            }

            return CreateBytes(buffer, rsBlocks);
        }
Ejemplo n.º 3
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);
        }