Skip to content

Commit

Permalink
init commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seok-ju, Yun committed Jul 15, 2014
1 parent 3235c89 commit c35d85f
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 1 deletion.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
xxHashSharp
===========

A pure C# implementation of xxhash
A pure C# implementation of [xxhash](http://code.google.com/p/xxhash/)

Example
----------

byte[] input = Encoding.UTF8.GetBytes("hello world");

xxHash hash = new xxHash();
hash.Update(input, input.Count());
Console.WriteLine("{0:X}", hash.Digest());

or

byte[] input = Encoding.UTF8.GetBytes("hello world");
Console.WriteLine("{0:X}", xxHash.CalculateHash(input));

License
----------

BSD 2-clause license.
36 changes: 36 additions & 0 deletions xxHashSharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 어셈블리의 일반 정보는 다음 특성 집합을 통해 제어됩니다.
// 어셈블리와 관련된 정보를 수정하려면
// 이 특성 값을 변경하십시오.
[assembly: AssemblyTitle("xxHashSharp")]
[assembly: AssemblyDescription("https://github.com/noricube/xxHashSharp")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("xxHashSharp")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에
// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면
// 해당 형식에 대해 ComVisible 특성을 true로 설정하십시오.
[assembly: ComVisible(false)]

// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다.
[assembly: Guid("2136af9e-b821-4f1b-ad04-bbae83fe8e61")]

// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다.
//
// 주 버전
// 부 버전
// 빌드 번호
// 수정 버전
//
// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 버전이 자동으로
// 지정되도록 할 수 있습니다.
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
256 changes: 256 additions & 0 deletions xxHashSharp/xxHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/*
xxHashSharp - A pure C# implementation of xxhash
Copyright (C) 2014, Seok-Ju, Yun. (https://github.com/noricube/xxHashSharp)
Original C Implementation Copyright (C) 2012-2014, Yann Collet. (https://code.google.com/p/xxhash/)
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xxHashSharp
{
public class xxHash
{
public struct XXH_State
{
public ulong total_len;
public uint seed;
public uint v1;
public uint v2;
public uint v3;
public uint v4;
public int memsize;
public byte[] memory;
};

const uint PRIME32_1 = 2654435761U;
const uint PRIME32_2 = 2246822519U;
const uint PRIME32_3 = 3266489917U;
const uint PRIME32_4 = 668265263U;
const uint PRIME32_5 = 374761393U;

protected XXH_State _state;
public xxHash()
{

}

public static uint CalculateHash(byte[] buf, int len = -1, uint seed = 0)
{
uint h32;
int index = 0;
if (len == -1)
{
len = buf.Length;
}


if (len >= 16)
{
int limit = len - 16;
uint v1 = seed + PRIME32_1 + PRIME32_2;
uint v2 = seed + PRIME32_2;
uint v3 = seed + 0;
uint v4 = seed - PRIME32_1;

do
{
v1 = CalcSubHash(v1, buf, index);
index += 4;
v2 = CalcSubHash(v2, buf, index);
index += 4;
v3 = CalcSubHash(v3, buf, index);
index += 4;
v4 = CalcSubHash(v4, buf, index);
index += 4;
} while (index <= limit);

h32 = RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
}
else
{
h32 = seed + PRIME32_5;
}

h32 += (uint)len;

while (index <= len - 4)
{
h32 += BitConverter.ToUInt32(buf, index) * PRIME32_3;
h32 = RotateLeft(h32, 17) * PRIME32_4;
index += 4;
}

while(index<len)
{
h32 += buf[index] * PRIME32_5;
h32 = RotateLeft(h32, 11) * PRIME32_1;
index++;
}

h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
h32 *= PRIME32_3;
h32 ^= h32 >> 16;

return h32;
}

public void Init(uint seed = 0)
{
_state.seed = seed;
_state.v1 = seed + PRIME32_1 + PRIME32_2;
_state.v2 = seed + PRIME32_2;
_state.v3 = seed + 0;
_state.v4 = seed - PRIME32_1;
_state.total_len = 0;
_state.memsize = 0;
_state.memory = new byte[16];
}

public bool Update(byte[] input, int len)
{
int index = 0;

_state.total_len += (uint)len;

if (_state.memsize + len < 16) // 버퍼 + 입력길이가 16바이트 이하일경우 버퍼에 저장만 해둔다
{
Array.Copy(input, 0, _state.memory, _state.memsize, len);
_state.memsize += len;

return true;
}

if (_state.memsize > 0) // 이전데이터가 남아있을경우 먼저 처리한다.
{
Array.Copy(input, 0, _state.memory, _state.memsize, 16 - _state.memsize);

_state.v1 = CalcSubHash(_state.v1, _state.memory, index);
index += 4;
_state.v2 = CalcSubHash(_state.v2, _state.memory, index);
index += 4;
_state.v3 = CalcSubHash(_state.v3, _state.memory, index);
index += 4;
_state.v4 = CalcSubHash(_state.v4, _state.memory, index);
index += 4;

index = 0;
_state.memsize = 0;
}

if (index <= len - 16)
{
int limit = len - 16;
uint v1 = _state.v1;
uint v2 = _state.v2;
uint v3 = _state.v3;
uint v4 = _state.v4;

do
{
v1 = CalcSubHash(v1, input, index);
index += 4;
v2 = CalcSubHash(v2, input, index);
index += 4;
v3 = CalcSubHash(v3, input, index);
index += 4;
v4 = CalcSubHash(v4, input, index);
index += 4;
} while (index <= limit);

_state.v1 = v1;
_state.v2 = v2;
_state.v3 = v3;
_state.v4 = v4;
}

if (index < len)
{
Array.Copy(input, index, _state.memory, 0, len - index);
_state.memsize = len - index;
}
return true;
}

public uint Digest()
{
uint h32;
int index = 0;
if (_state.total_len >= 16)
{
h32 = RotateLeft(_state.v1, 1) + RotateLeft(_state.v2, 7) + RotateLeft(_state.v3, 12) + RotateLeft(_state.v4, 18);
}
else
{
h32 = _state.seed + PRIME32_5;
}

h32 += (UInt32)_state.total_len;

while (index <= _state.memsize - 4)
{
h32 += BitConverter.ToUInt32(_state.memory, index) * PRIME32_3;
h32 = RotateLeft(h32, 17) * PRIME32_4;
index += 4;
}

while (index < _state.memsize)
{
h32 += _state.memory[index] * PRIME32_5;
h32 = RotateLeft(h32, 11) * PRIME32_1;
index++;
}

h32 ^= h32 >> 15;
h32 *= PRIME32_2;
h32 ^= h32 >> 13;
h32 *= PRIME32_3;
h32 ^= h32 >> 16;

return h32;
}
private static uint CalcSubHash(uint value, byte[] buf, int index)
{
uint read_value = BitConverter.ToUInt32(buf, index);
value += read_value * PRIME32_2;
value = RotateLeft(value, 13);
value *= PRIME32_1;
return value;
}

private static uint RotateLeft(uint value, int count)
{
return (value << count) | (value >> (32 - count));
}

}
}
Loading

0 comments on commit c35d85f

Please sign in to comment.