123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using System.Collections.Generic;
- namespace SharpCompress.Compressors.Xz
- {
- internal static class Crc64
- {
- public const UInt64 DefaultSeed = 0x0;
- internal static UInt64[] Table;
- public const UInt64 Iso3309Polynomial = 0xD800000000000000;
- public static UInt64 Compute(byte[] buffer)
- {
- return Compute(DefaultSeed, buffer);
- }
- public static UInt64 Compute(UInt64 seed, byte[] buffer)
- {
- if (Table == null)
- Table = CreateTable(Iso3309Polynomial);
- return CalculateHash(seed, Table, buffer, 0, buffer.Length);
- }
- public static UInt64 CalculateHash(UInt64 seed, UInt64[] table, IList<byte> buffer, int start, int size)
- {
- var crc = seed;
- for (var i = start; i < size; i++)
- unchecked
- {
- crc = (crc >> 8) ^ table[(buffer[i] ^ crc) & 0xff];
- }
- return crc;
- }
- public static ulong[] CreateTable(ulong polynomial)
- {
- var createTable = new UInt64[256];
- for (var i = 0; i < 256; ++i)
- {
- var entry = (UInt64)i;
- for (var j = 0; j < 8; ++j)
- if ((entry & 1) == 1)
- entry = (entry >> 1) ^ polynomial;
- else
- entry = entry >> 1;
- createTable[i] = entry;
- }
- return createTable;
- }
- }
- }
|