Esempio n. 1
0
        private Crc32(Crc32Formula formula, uint[] polynomialTable, uint initValue, uint xorOut)
        {
            _polynomialTable = polynomialTable ?? throw new ArgumentNullException(nameof(polynomialTable));
            _formula         = formula;

            _initValue = initValue;
            _xorOut    = xorOut;
        }
Esempio n. 2
0
        private static uint[] InitializeTable(Crc32Formula formula, uint polynomial)
        {
            if (formula == Crc32Formula.Normal && polynomial == DefaultPolynomial)
            {
                return(_defaultCrc32Table);
            }

            if (formula == Crc32Formula.Reflected && polynomial == DefaultReflectedPolynomial)
            {
                return(_defaultReflectedCrc32Table);
            }

            var polynomialTable = new uint[256];

            for (uint i = 0; i < 256; i++)
            {
                uint entry;
                if (formula == Crc32Formula.Normal)
                {
                    entry = i << 24;
                    for (var j = 0; j < 8; j++)
                    {
                        if ((entry & 0x80000000) != 0)
                        {
                            entry = (entry << 1) ^ polynomial;
                        }
                        else
                        {
                            entry <<= 1;
                        }
                    }
                }
                else
                {
                    entry = i;
                    for (var j = 0; j < 8; j++)
                    {
                        if ((entry & 1) == 1)
                        {
                            entry = (entry >> 1) ^ polynomial;
                        }
                        else
                        {
                            entry >>= 1;
                        }
                    }
                }
                polynomialTable[i] = entry;
            }
            return(polynomialTable);
        }
Esempio n. 3
0
        public static Crc32 Create(Crc32Formula formula, uint polynomial)
        {
            var table = InitializeTable(formula, polynomial);

            return(new Crc32(formula, table));
        }
Esempio n. 4
0
 public static Crc32 Create(Crc32Formula formula)
 {
     return(new Crc32(formula, formula == Crc32Formula.Normal ?
                      _defaultCrc32Table :
                      _defaultReflectedCrc32Table));
 }
Esempio n. 5
0
 private Crc32(Crc32Formula formula, uint[] polynomialTable)
 {
     _polynomialTable = polynomialTable ?? throw new ArgumentNullException(nameof(polynomialTable));
     _formula         = formula;
 }
Esempio n. 6
0
        public static Crc32 Create(Crc32Formula formula, uint polynomial, uint initValue = 0xFFFFFFFF, uint xorOut = 0xFFFFFFFF)
        {
            var table = InitializeTable(formula, polynomial);

            return(new Crc32(formula, table, initValue, xorOut));
        }