Lookup3

#include <Windows.h>

SIZE_T StringLengthW(_In_ LPCWSTR String)
{
	LPCWSTR String2;

	for (String2 = String; *String2; ++String2);

	return (String2 - String);
}


UINT32 RotateToLeft32(UINT32 x, UINT32 Rotate)
{
    return (x << Rotate) | (x >> (32 - Rotate));
}

UINT32 HashStringLookup3W(PVOID String, SIZE_T Length, UINT32 Seed)
{
    PBYTE Pointer = (PBYTE)String;
    UINT32 State0 = 0;
    UINT32 State1 = 0;
    UINT32 State2 = 0;

    State0 = State1 = State2 = 0xdeadbeef + (UINT32)Length + Seed;

    while (Length > 12)
    {
        State0 += *(PUINT32)(Pointer + 0);
        State1 += *(PUINT32)(Pointer + 4);
        State2 += *(PUINT32)(Pointer + 8);

        State0 -= State2;  State0 ^= RotateToLeft32(State2, 4);  State2 += State1;
        State1 -= State0;  State1 ^= RotateToLeft32(State0, 6);  State0 += State2;
        State2 -= State1;  State2 ^= RotateToLeft32(State1, 8);  State1 += State0;
        State0 -= State2;  State0 ^= RotateToLeft32(State2, 16);  State2 += State1;
        State1 -= State0;  State1 ^= RotateToLeft32(State0, 19);  State0 += State2;
        State2 -= State1;  State2 ^= RotateToLeft32(State1, 4);  State1 += State0;

        Pointer += 12;
        Length -= 12;
    }

    switch (Length)
    {
        case 12: State2 += *(PUINT32)(Pointer + 8);
        case 11: State2 += (UINT32)Pointer[10] << 16;
        case 10: State2 += (UINT32)Pointer[9] << 8;
        case 9: State2 += Pointer[8];
        case 8: State1 += *(PUINT32)(Pointer + 4);
        case 7: State1 += (UINT32)Pointer[6] << 16;
        case 6: State1 += (UINT32)Pointer[5] << 8;
        case 5: State1 += Pointer[4];
        case 4: State0 += *(PUINT32)(Pointer + 0);
        case 3: State0 += (UINT32)Pointer[2] << 16;
        case 2: State0 += (UINT32)Pointer[1] << 8;
        case 1: State0 += Pointer[0];
        default: break;
    }

    State2 ^= State1; State2 -= RotateToLeft32(State1, 14);
    State0 ^= State2; State0 -= RotateToLeft32(State2, 11);
    State1 ^= State0; State1 -= RotateToLeft32(State0, 25);
    State2 ^= State1; State2 -= RotateToLeft32(State1, 16);
    State0 ^= State2; State0 -= RotateToLeft32(State2, 4);
    State1 ^= State0; State1 -= RotateToLeft32(State0, 14);
    State2 ^= State1; State2 -= RotateToLeft32(State1, 24);

    return State2;
}

INT main(VOID)
{
    WCHAR StringHashExample[] = L"Hash This String";
    UINT32 Hash = 0;

    Hash = HashStringLookup3W(StringHashExample, (StringLengthW(StringHashExample) * sizeof(WCHAR)), 1);

    return ERROR_SUCCESS;
}

Last updated