using System; using System.Collections.Generic; namespace SharpCompress.Compressors.Xz { internal static class Crc32 { public const UInt32 DefaultPolynomial = 0xedb88320u; public const UInt32 DefaultSeed = 0xffffffffu; private static UInt32[] defaultTable; public static UInt32 Compute(byte[] buffer) { return Compute(DefaultSeed, buffer); } public static UInt32 Compute(UInt32 seed, byte[] buffer) { return Compute(DefaultPolynomial, seed, buffer); } public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) { return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length); } private static UInt32[] InitializeTable(UInt32 polynomial) { if (polynomial == DefaultPolynomial && defaultTable != null) return defaultTable; var createTable = new UInt32[256]; for (var i = 0; i < 256; i++) { var entry = (UInt32)i; for (var j = 0; j < 8; j++) if ((entry & 1) == 1) entry = (entry >> 1) ^ polynomial; else entry = entry >> 1; createTable[i] = entry; } if (polynomial == DefaultPolynomial) defaultTable = createTable; return createTable; } private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList buffer, int start, int size) { var crc = seed; for (var i = start; i < size - start; i++) crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; return crc; } } }