Exemplo n.º 1
0
        /// <summary>
        /// Takes the error correction level and the mask pattern number and creates
        /// the format data which is then masked and can be put directly into the matrix
        /// </summary>
        /// <param name="errorCorrectionLevel">the error correction level (0-3)</param>
        /// <param name="maskPattern">the mask pattern (0-7)</param>
        /// <returns>the queue that is masked and can be directly placed into the matrix</returns>
        public Queue <bool> FormatInformation(int errorCorrectionLevel, int maskPattern)
        {
            //generator polynomial is a typo, it is really the "message" polynomial
            //gOfX is the generator polynomial xD oh well, my mistake

            Queue <bool> outQueue = new Queue <bool>();

            bool[] generatorPolynomial = new bool[15];
            //i really hate using a COUNTER, but for this case i will just have to deal with it
            int generatorBoolCounter = 0;

            //this is gOfX, this is what divides the generator polynomial
            bool[] gOfX = { true, false, true, false, false, true, true, false, true, true, true };
            //reversing it allows the actual numbers of expontents to be used
            Array.Reverse(gOfX);

            //add the first two bits to the out queue and the generator polynomial
            foreach (bool thisBool in qrAssistance.BitsWithPadding(errorCorrectionLevel, 2))
            {
                //add to the queue for later use
                outQueue.Enqueue(thisBool);
                //add to the generator polynomial for later use
                generatorPolynomial[generatorBoolCounter] = thisBool;
                generatorBoolCounter++;
            }

            //and now for the next 3 bits, the data that represents the masking data
            foreach (bool thisBool in qrAssistance.BitsWithPadding(maskPattern, 3))
            {
                //add to the queue for later use
                outQueue.Enqueue(thisBool);
                //add to the generator polynomial for later use
                generatorPolynomial[generatorBoolCounter] = thisBool;
                generatorBoolCounter++;
            }

            //effectively raising the polynomials by x^10 xD (it works..)
            Array.Reverse(generatorPolynomial);

            //and this is what i call murder, have fun mr. trink..
            //cycles through the elements of the generator polynomial (ten is added so it doesnt have to be added at every step later)
            //also starting at 14 alows me to use the same array for when it is exported into the queue
            for (int i = 14; i >= 10; i--)
            {
                //then if the polynomial that is currently being looked at ISNT zero
                if (generatorPolynomial[i] == true)
                {
                    int quotientExponent = i - 10;
                    for (int k = 10; k >= 0; k--)
                    {
                        //XOR is magical, it took me about 4 minutes exactly to realize the operation that was actually being done
                        //while getting a remained was XOR in this case
                        generatorPolynomial[k + quotientExponent] ^= gOfX[k];
                    }
                }
            }
            //after getting the above i wanted to cry, as it is only 9 LINES OF CODE and the amount of time i spent is pityfull........

            //add the new bits to the out stream
            for (int i = 9; i >= 0; i--)
            {
                outQueue.Enqueue(generatorPolynomial[i]);
            }

            //XOR stream
            bool[] XORMaskStream = { true, false, true, false, true, false, false, false, false, false, true, false, false, true, false };
            //all of the outgoing bits have to be XOR'd with a mask that apparently prevents the rare case of an ALL zero string from being outputted
            //im just going to leave the understanding of this one to the people with the PhD's (or just accept that this bit stream does what they say it does)
            for (int i = 0; i <= 14; i++)
            {
                //cycles through the whole queue XORIN' it all with the bit stream
                outQueue.Enqueue(outQueue.Dequeue() ^ XORMaskStream[i]);
            }

            outQueue = qrAssistance.ReverseQueue(outQueue);

            return(outQueue);
        }
Exemplo n.º 2
0
        private Queue <bool> AlphaNumericEncoding(string message)
        {
            //creates dictionary of characters
            SetUpDictionary();

            //stores the decoded numbers
            List <int> messageDecoded = new List <int>();
            //the queue to be output for data placement
            Queue <bool> outQueue = new Queue <bool>();

            //message length is needed here and there
            //also i need the starting length constantly
            int messageLength = message.Length;

            //must be all in caps to be decoded
            message = message.ToUpper();

            //add the mode indicator to the queue
            //requires total of 4 bits and the mode number is 2
            foreach (bool thisBool in qrAssistance.BitsWithPadding(2, 4))
            {
                outQueue.Enqueue(thisBool);
            }

            //determines padding for characters data block according to version
            int characterLengthIndex;

            if (version < 10)
            {
                characterLengthIndex = 9;
            }
            else if (version < 27)
            {
                characterLengthIndex = 11;
            }
            else
            {
                characterLengthIndex = 13;
            }

            //adds the character count data to the array
            foreach (bool thisBool in qrAssistance.BitsWithPadding(messageLength, characterLengthIndex))
            {
                outQueue.Enqueue(thisBool);
            }

            //convert message to corresponding integer values
            for (int i = 0; i < messageLength; i++)
            {
                //if the character being input is actually in the dictionary..
                if (alphaNumericDictionary.ContainsKey(message.Substring(0, 1)))
                {
                    messageDecoded.Add(alphaNumericDictionary[message.Substring(0, 1)]);
                }

                //if the character was not found in the dictionary, a space in put in its place (no rhyme intended)
                else
                {
                    messageDecoded.Add(36);
                }
                message = message.Substring(1);
            }

            //converts the decoded chacter stream into bools and adds them to the queue
            for (int i = 0; i < messageDecoded.Count; i += 2)
            {
                int currentStream;
                int streamLength;
                //two characters are converted into 11 bit streams, sometimes there is an odd character left at the end
                if (i == messageDecoded.Count - 1)
                {
                    currentStream = messageDecoded[i];
                    //stream changes to 6 when there is only one character being converted
                    streamLength = 6;
                }
                else
                {
                    //the stream = the current character x 45 + the next character
                    currentStream = messageDecoded[i] * 45 + messageDecoded[i + 1];
                    streamLength  = 11;
                }

                //adds the current data value into the data stream
                foreach (bool thisBool in qrAssistance.BitsWithPadding(currentStream, streamLength))
                {
                    outQueue.Enqueue(thisBool);
                }
            }

            ///adds some terminator bits - four bits that are zeros which are added
            ///these bits are used to signal the end of a message to the decoder
            ///as usual, there are specific cases that need to be looked out for, for example when the capacity of the symbol
            ///is almost filled by the data stream (within 4 from the capacity), only the amount needed to fill the stream length are added
            ///in general, 4 bits are added at this step
            if (outQueue.Count < numberOfCodeWords[version - 1, errorCorrectionLevel] * 8 - 4)
            {
                //adding of the four bits that are mentioned above
                for (int i = 0; i < 4; i++)
                {
                    outQueue.Enqueue(false);
                }
            }

            //the queue must be a multiple of 8, otherwise when it is divided into codewords the operation would fail
            outQueue = qrAssistance.ModTo8(outQueue);
            return(outQueue);
        }