コード例 #1
0
 public static unsafe ushort Substract(ushort crc1, ushort crc2, long len2)
 {
     crc1 = Crc16.Reflect(crc1);
     crc2 = Crc16.Reflect(crc2);
     /* degenerate case */
     if (len2 == 0)
     {
         return(crc1);
     }
     if (len2 < 0)
         throw new ArgumentException("crc.Combine length cannot be negative", "len2"); }
コード例 #2
0
        static unsafe Crc16()
        {
            for (ushort i = 0; i < table.Length; i++)
            {
                int crc = i;
                for (int j = 0; j < GF2_DIM; j++)
                {
                    if ((crc & (1U << (GF2_DIM - 1))) != 0)
                    {
                        crc = ((crc << 1) ^ polynomial);
                    }
                    else
                    {
                        crc <<= 1;
                    }
                }
                table[i] = (ushort)(crc & ((1 << GF2_DIM) - 1));
            }

            combineTable[0, 0]             = Crc16.Reflect(polynomial);
            substractTable[0, GF2_DIM - 1] = reversePolynomial;
            for (int n = 1; n < GF2_DIM; n++)
            {
                combineTable[0, n]       = (ushort)(1 << (n - 1));
                substractTable[0, n - 1] = (ushort)(1 << n);
            }

            fixed(ushort *ct = &combineTable[0, 0], st = &substractTable[0, 0])
            {
                //for (int i = 0; i < GF2_DIM; i++)
                //	st[32 + i] = ct[i];
                //invert_binary_matrix(st + 32, st, GF2_DIM);

                for (int i = 1; i < GF2_DIM; i++)
                {
                    gf2_matrix_square(ct + i * GF2_DIM, ct + (i - 1) * GF2_DIM);
                    gf2_matrix_square(st + i * GF2_DIM, st + (i - 1) * GF2_DIM);
                }
            }
        }
コード例 #3
0
        public static unsafe ushort Combine(ushort crc1, ushort crc2, long len2)
        {
            crc1 = Crc16.Reflect(crc1);
            crc2 = Crc16.Reflect(crc2);

            /* degenerate case */
            if (len2 == 0)
            {
                return(crc1);
            }
            if (crc1 == 0)
            {
                return(crc2);
            }
            if (len2 < 0)
            {
                throw new ArgumentException("crc.Combine length cannot be negative", "len2");

                fixed(ushort *ct = &combineTable[0, 0])
                {
                    int n = 3;

                    do
                    {
                        /* apply zeros operator for this bit of len2 */
                        if ((len2 & 1) != 0)
                        {
                            crc1 = gf2_matrix_times(ct + GF2_DIM * n, crc1);
                        }
                        len2 >>= 1;
                        n      = (n + 1) & (GF2_DIM - 1);
                        /* if no more bits set, then done */
                    } while (len2 != 0);
                }

                /* return combined crc */
                crc1 ^= crc2;
                crc1  = Crc16.Reflect(crc1);
                return(crc1);
        }