Beispiel #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="codingType"></param>
 public CRCEntity(CRCCoding codingType)
 {
     Initialize(codingType);
 }
Beispiel #2
0
        private void Initialize(CRCCoding codingType)
        {
            switch (codingType)
            {
            case CRCCoding.CRC16CCITT:
                order = 16; direct = 1; polynom = 0x1021; crcinit = 0xFFFF; crcxor = 0; refin = 0; refout = 0;
                break;

            case CRCCoding.CRC16:
                order = 16; direct = 1; polynom = 0x8005; crcinit = 0x0; crcxor = 0x0; refin = 1; refout = 1;
                break;

            case CRCCoding.CRC32:
                order = 32; direct = 1; polynom = 0x4c11db7; crcinit = 0xFFFFFFFF; crcxor = 0xFFFFFFFF; refin = 1; refout = 1;
                break;
            }

            //Initialize all variables for seeding and builing based upon the given coding type
            //at first, compute constant bit masks for whole CRC and CRC high bit

            crcmask    = ((((ulong)1 << (order - 1)) - 1) << 1) | 1;
            crchighbit = (ulong)1 << (order - 1);

            //generate lookup table
            generate_crc_table();

            ulong bit, crc;
            int   i;

            if (direct == 0)
            {
                crcinit_nondirect = crcinit;
                crc = crcinit;
                for (i = 0; i < order; i++)
                {
                    bit   = crc & crchighbit;
                    crc <<= 1;
                    if (bit != 0)
                    {
                        crc ^= polynom;
                    }
                }
                crc           &= crcmask;
                crcinit_direct = crc;
            }
            else
            {
                crcinit_direct = crcinit;
                crc            = crcinit;
                for (i = 0; i < order; i++)
                {
                    bit = crc & 1;
                    if (bit != 0)
                    {
                        crc ^= polynom;
                    }
                    crc >>= 1;
                    if (bit != 0)
                    {
                        crc |= crchighbit;
                    }
                }
                crcinit_nondirect = crc;
            }
        }