From 0963bf950533632a26f266f2f8575dc4344f22d8 Mon Sep 17 00:00:00 2001 From: JAJames Date: Wed, 3 Aug 2016 14:32:15 -0400 Subject: [PATCH] Committed Hash.c & Hash.h, which just provide some basic hash algorithms. More may come in the future. --- Jupiter/Hash.c | 49 ++++++++++++++++++++++++++++++++ Jupiter/Hash.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 Jupiter/Hash.c create mode 100644 Jupiter/Hash.h diff --git a/Jupiter/Hash.c b/Jupiter/Hash.c new file mode 100644 index 0000000..c8358f2 --- /dev/null +++ b/Jupiter/Hash.c @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2015 Jessica James. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Written by Jessica James + */ + +#include "Hash.h" +#include + +const uint32_t JUPITER_FNV_1_32_OFFSET_BASIS = 2166136261UL; +const uint64_t JUPITER_FNV_1_64_OFFSET_BASIS = 14695981039346656037ULL; +const uint32_t JUPITER_FNV_1_32_PRIME = 16777619UL; +const uint64_t JUPITER_FNV_1_64_PRIME = 1099511628211ULL; + +uint64_t Jupiter_fnv1(void *data, size_t length) +{ + uint8_t *ptr = (uint8_t *)data; + uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS; + while (length-- != 0) + { + hash = hash * JUPITER_FNV_1_64_PRIME; + hash = hash ^ *ptr++; + } + return hash; +} + +uint64_t Jupiter_fnv1a(void *data, size_t length) +{ + uint8_t *ptr = (uint8_t *)data; + uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS; + while (length-- != 0) + { + hash = hash ^ *ptr++; + hash = hash * JUPITER_FNV_1_64_PRIME; + } + return hash; +} \ No newline at end of file diff --git a/Jupiter/Hash.h b/Jupiter/Hash.h new file mode 100644 index 0000000..0b616b2 --- /dev/null +++ b/Jupiter/Hash.h @@ -0,0 +1,76 @@ +/** + * Copyright (C) 2015 Jessica James. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Written by Jessica James + */ + +#if !defined _HASH_H_HEADER +#define _HASH_H_HEADER + +/** + * @file Hash.h + * @brief Defines some hashing algorithms. + */ + +#include "Jupiter.h" +#if defined __cplusplus +#include + +namespace Jupiter +{ + /** Fowler-Noll-Vo hash algorithms */ + template inline uint64_t fnv1(const T *str, size_t length); + template inline uint64_t fnv1(const Jupiter::Readable_String &str); + + template inline uint64_t fnv1a(const T *str, size_t length); + template inline uint64_t fnv1a(const Jupiter::Readable_String &str); +} + +extern "C" +{ +#else +#include +#endif // __cplusplus + +JUPITER_API uint64_t Jupiter_fnv1(void *data, size_t length); +JUPITER_API uint64_t Jupiter_fnv1a(void *data, size_t length); + +#if defined __cplusplus +} + +/** fnv1 implementation */ +template inline uint64_t Jupiter::fnv1(const T *data, size_t length) +{ + return Jupiter_fnv1(data, length * sizeof(T)); +} + +template inline uint64_t Jupiter::fnv1(const Jupiter::Readable_String &data) +{ + return Jupiter_fnv1(data.ptr(), data.size() * sizeof(T)); +} + +template inline uint64_t Jupiter::fnv1a(const T *data, size_t length) +{ + return Jupiter_fnv1a(data, length * sizeof(T)); +} + +template inline uint64_t Jupiter::fnv1a(const Jupiter::Readable_String &data) +{ + return Jupiter_fnv1a(data.ptr(), data.size() * sizeof(T)); +} + +#endif // __cplusplus + +#endif // _HASH_H_HEADER \ No newline at end of file