72 lines
3.4 KiB
C#
72 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
namespace ITF_StringID
|
|
{
|
|
public class StringID
|
|
{
|
|
static char ToUp(char c)
|
|
{
|
|
return Char.ToUpper(c);
|
|
}
|
|
|
|
static void mix(ref UInt32 a, ref UInt32 b, ref UInt32 c)
|
|
{
|
|
a -= b; a -= c; a ^= (c>>13);
|
|
b -= c; b -= a; b ^= (a<<8);
|
|
c -= a; c -= b; c ^= (b>>13);
|
|
a -= b; a -= c; a ^= (c>>12);
|
|
b -= c; b -= a; b ^= (a<<16);
|
|
c -= a; c -= b; c ^= (b>>5);
|
|
a -= b; a -= c; a ^= (c>>3);
|
|
b -= c; b -= a; b ^= (a<<10);
|
|
c -= a; c -= b; c ^= (b>>15);
|
|
}
|
|
static public ulong ComputeCRC(string _str)
|
|
{
|
|
UInt32 _stride = 1;
|
|
UInt32 _len = (UInt32)_str.Length;
|
|
/* Set up the internal state */
|
|
UInt32 Len = _len;
|
|
UInt32 len = Len;
|
|
UInt32 a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
|
|
UInt32 b = a;
|
|
UInt32 c = 0;
|
|
int pos = 0;
|
|
/*---------------------------------------- handle most of the key */
|
|
while (len >= 12)
|
|
{
|
|
a += (ToUp(_str[pos+(int)(0 * _stride)]) + ((uint)ToUp(_str[pos+(int)(1 * _stride)]) << 8) + ((uint)ToUp(_str[pos+(int)(2 * _stride)]) << 16) + ((uint)ToUp(_str[pos+(int)(3 * _stride)]) << 24));
|
|
b += (ToUp(_str[pos+(int)(4 * _stride)]) + ((uint)ToUp(_str[pos+(int)(5 * _stride)]) << 8) + ((uint)ToUp(_str[pos+(int)(6 * _stride)]) << 16) + ((uint)ToUp(_str[pos+(int)(7 * _stride)]) << 24));
|
|
c += (ToUp(_str[pos+(int)(8 * _stride)]) + ((uint)ToUp(_str[pos+(int)(9 * _stride)]) << 8) + ((uint)ToUp(_str[pos+(int)(10 * _stride)]) << 16) + ((uint)ToUp(_str[pos+(int)(11 * _stride)]) << 24));
|
|
mix(ref a, ref b, ref c);
|
|
pos += 12 * (int)_stride;
|
|
len -= 12;
|
|
}
|
|
|
|
/*------------------------------------- handle the last 11 bytes */
|
|
c += Len;
|
|
switch (len) /* all the case statements fall through */
|
|
{
|
|
case 11: c += ((uint)ToUp(_str[pos+(int)(10 * _stride)]) << 24); goto case 10;
|
|
case 10: c += ((uint)ToUp(_str[pos+(int)(9 * _stride)]) << 16); goto case 9;
|
|
case 9: c += ((uint)ToUp(_str[pos+(int)(8 * _stride)]) << 8); goto case 8;
|
|
/* the first byte of c is reserved for the length */
|
|
case 8: b += ((uint)ToUp(_str[pos+(int)(7 * _stride)]) << 24); goto case 7;
|
|
case 7: b += ((uint)ToUp(_str[pos+(int)(6 * _stride)]) << 16); goto case 6;
|
|
case 6: b += ((uint)ToUp(_str[pos+(int)(5 * _stride)]) << 8); goto case 5;
|
|
case 5: b += ToUp(_str[pos+(int)(4 * _stride)]); goto case 4;
|
|
case 4: a += ((uint)ToUp(_str[pos+(int)(3 * _stride)]) << 24); goto case 3;
|
|
case 3: a += ((uint)ToUp(_str[pos+(int)(2 * _stride)]) << 16); goto case 2;
|
|
case 2: a += ((uint)ToUp(_str[pos+(int)(1 * _stride)]) << 8); goto case 1;
|
|
case 1: a += ToUp(_str[pos+(int)(0 * _stride)]); break;
|
|
/* case 0: nothing left to add */
|
|
}
|
|
mix(ref a,ref b,ref c);
|
|
|
|
return c;
|
|
}
|
|
}
|
|
}
|