Author | Jamozed <[email protected]> |
Date | 2021-12-08 00:59:42 |
Commit | 7d31e3ad7ba3afe8e6cc7c42bbed4943b2081c21 |
Parent | 359885de5b31e33b98e5d73e284ffb8836144966 |
fnv: Update to use util.h
Diffstat
M | src/fnv.c | | | 40 | ++++++++++++++++++++-------------------- |
M | src/fnv.h | | | 17 | ++++++++--------- |
M | src/test/test_fnv.c | | | 19 | +++++-------------- |
3 files changed, 33 insertions, 43 deletions
diff --git a/src/fnv.c b/src/fnv.c index d675213..c393e67 100644 --- a/src/fnv.c +++ b/src/fnv.c @@ -1,4 +1,4 @@ -// util/fnv.c, version 1.0.0 +// util/fnv.c, version 1.0.1 // FNV hash source file from libutil // Copyright (C) 2021, Jakob Wakeling // All rights reserved. @@ -31,33 +31,33 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ #include "fnv.h" +#include "util.h" -#include <stddef.h> -#include <stdint.h> - -static const uint32_t FNV_PRIME_32 = 0x01000193; -static const uint32_t FNV_BASIS_32 = 0x811C9DC5; -static const uint64_t FNV_PRIME_64 = 0x00000100000001B3; -static const uint64_t FNV_BASIS_64 = 0xCBF29CE484222325; +static const u32 FNV_PRIME_32 = 0x01000193; +static const u32 FNV_BASIS_32 = 0x811C9DC5; +static const u64 FNV_PRIME_64 = 0x00000100000001B3; +static const u64 FNV_BASIS_64 = 0xCBF29CE484222325; /* Compute the FNV1a-32 hash of some data. */ -uint32_t fnv1a32(uint8_t *dat, size_t len) { - register uint32_t fnv = FNV_BASIS_32; - for (; len; --len, ++dat) { fnv ^= *dat; fnv *= FNV_PRIME_32; } return fnv; +u32 fnv1a32(const char *dat, UINT len) { + register u32 fnv = FNV_BASIS_32; + for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_32; } + return fnv; } /* Compute the FNV1a-64 hash of some data. */ -uint64_t fnv1a64(uint8_t *dat, size_t len) { - register uint64_t fnv = FNV_BASIS_64; - for (; len; --len, ++dat) { fnv ^= *dat; fnv *= FNV_PRIME_64; } return fnv; +u64 fnv1a64(const char *dat, UINT len) { + register u64 fnv = FNV_BASIS_64; + for (; len; len -= 1, dat += 1) { fnv ^= *dat; fnv *= FNV_PRIME_64; } + return fnv; } -void fnv1a32_init(uint32_t *ctx) { *ctx = FNV_BASIS_32; } -void fnv1a32_hash(uint32_t *ctx, uint8_t *dat, size_t len) { - for (; len; --len, ++dat) { *ctx ^= *dat; *ctx *= FNV_PRIME_32; } +void fnv1a32_init(u32 *ctx) { *ctx = FNV_BASIS_32; } +void fnv1a32_hash(u32 *ctx, char *dat, UINT len) { + for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_32; } } -void fnv1a64_init(uint64_t *ctx) { *ctx = FNV_BASIS_64; } -void fnv1a64_hash(uint64_t *ctx, uint8_t *dat, size_t len) { - for (; len; --len, ++dat) { *ctx ^= *dat; *ctx *= FNV_PRIME_64; } +void fnv1a64_init(u64 *ctx) { *ctx = FNV_BASIS_64; } +void fnv1a64_hash(u64 *ctx, char *dat, UINT len) { + for (; len; len -= 1, dat += 1) { *ctx ^= *dat; *ctx *= FNV_PRIME_64; } } diff --git a/src/fnv.h b/src/fnv.h index f453f5f..e79e964 100644 --- a/src/fnv.h +++ b/src/fnv.h @@ -1,4 +1,4 @@ -// util/fnv.h, version 1.0.0 +// util/fnv.h, version 1.0.1 // FNV hash header file from libutil // Copyright (C) 2021, Jakob Wakeling // All rights reserved. @@ -33,16 +33,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #ifndef UTIL_FNV_H_O4TYU6Q1 #define UTIL_FNV_H_O4TYU6Q1 -#include <stddef.h> -#include <stdint.h> +#include "util.h" -extern uint32_t fnv1a32(uint8_t *dat, size_t len); -extern uint64_t fnv1a64(uint8_t *dat, size_t len); +extern u32 fnv1a32(const char *dat, UINT len); +extern u64 fnv1a64(const char *dat, UINT len); -extern void fnv1a32_init(uint32_t *ctx); -extern void fnv1a32_hash(uint32_t *ctx, uint8_t *dat, size_t len); +extern void fnv1a32_init(u32 *ctx); +extern void fnv1a32_hash(u32 *ctx, char *dat, UINT len); -extern void fnv1a64_init(uint64_t *ctx); -extern void fnv1a64_hash(uint64_t *ctx, uint8_t *dat, size_t len); +extern void fnv1a64_init(u64 *ctx); +extern void fnv1a64_hash(u64 *ctx, char *dat, UINT len); #endif // UTIL_FNV_H_O4TYU6Q1 diff --git a/src/test/test_fnv.c b/src/test/test_fnv.c index 0c0c301..28385a0 100644 --- a/src/test/test_fnv.c +++ b/src/test/test_fnv.c @@ -18,6 +18,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. */ #include "../fnv.h" +#include "../util.h" #include "unit.h" #include <stdint.h> @@ -26,32 +27,22 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. int main(void) { { - uint8_t i[9] = "123456789"; uint32_t fnv32; - - fnv32 = fnv1a32(i, 9); + u32 fnv32 = fnv1a32("123456789", 9); ASSERT("T000 HASH STRING fnv1a32()", fnv32 == 0xBB86B11C); } { - uint8_t i[9] = "123456789"; uint32_t fnv32; - - fnv1a32_init(&fnv32); - fnv1a32_hash(&fnv32, i, 9); + u32 fnv32; fnv1a32_init(&fnv32); fnv1a32_hash(&fnv32, "123456789", 9); ASSERT("T001 HASH STRING fnv1a32_hash()", fnv32 == 0xBB86B11C); } { - uint8_t i[9] = "123456789"; uint64_t fnv64; - - fnv64 = fnv1a64(i, 9); + u64 fnv64 = fnv1a64("123456789", 9); ASSERT("T002 HASH STRING fnv1a64()", fnv64 == 0x06D5573923C6CDFC); } { - uint8_t i[9] = "123456789"; uint64_t fnv64; - - fnv1a64_init(&fnv64); - fnv1a64_hash(&fnv64, i, 9); + u64 fnv64; fnv1a64_init(&fnv64); fnv1a64_hash(&fnv64, "123456789", 9); ASSERT("T003 HASH STRING fnv1a64_hash()", fnv64 == 0x06D5573923C6CDFC); }