diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | hash/CMakeLists.txt | 14 | ||||
| -rw-r--r-- | hash/include/fnv1a.h | 9 | ||||
| -rw-r--r-- | hash/src/fnv1a.c | 23 |
4 files changed, 47 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d1fb3ab..a7ce893 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -8,6 +8,7 @@ add_subdirectory(cassert) | |||
| 8 | add_subdirectory(cstring) | 8 | add_subdirectory(cstring) |
| 9 | add_subdirectory(error) | 9 | add_subdirectory(error) |
| 10 | add_subdirectory(filesystem) | 10 | add_subdirectory(filesystem) |
| 11 | add_subdirectory(hash) | ||
| 11 | add_subdirectory(list) | 12 | add_subdirectory(list) |
| 12 | add_subdirectory(log) | 13 | add_subdirectory(log) |
| 13 | add_subdirectory(mem) | 14 | add_subdirectory(mem) |
diff --git a/hash/CMakeLists.txt b/hash/CMakeLists.txt new file mode 100644 index 0000000..8191f25 --- /dev/null +++ b/hash/CMakeLists.txt | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.5) | ||
| 2 | |||
| 3 | project(hash) | ||
| 4 | |||
| 5 | set(CMAKE_C_STANDARD 23) | ||
| 6 | set(CMAKE_C_STANDARD_REQUIRED On) | ||
| 7 | set(CMAKE_C_EXTENSIONS Off) | ||
| 8 | |||
| 9 | add_library(hash | ||
| 10 | src/fnv1a.c) | ||
| 11 | |||
| 12 | target_include_directories(hash PUBLIC include) | ||
| 13 | |||
| 14 | target_compile_options(hash PRIVATE -Wall -Wextra) | ||
diff --git a/hash/include/fnv1a.h b/hash/include/fnv1a.h new file mode 100644 index 0000000..0d2e7cf --- /dev/null +++ b/hash/include/fnv1a.h | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | |||
| 6 | uint32_t fnv1a32(const void* buffer, size_t size_bytes); | ||
| 7 | |||
| 8 | uint32_t fnv1a32_begin(); | ||
| 9 | uint32_t fnv1a32_update(uint32_t hash, const void* buffer, size_t size_bytes); | ||
diff --git a/hash/src/fnv1a.c b/hash/src/fnv1a.c new file mode 100644 index 0000000..32cb538 --- /dev/null +++ b/hash/src/fnv1a.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #include <fnv1a.h> | ||
| 2 | |||
| 3 | #include <assert.h> | ||
| 4 | |||
| 5 | #define FNV_PRIME 2166136261 | ||
| 6 | #define FNV_OFFSET_BASIS 16777619 | ||
| 7 | |||
| 8 | uint32_t fnv1a_32(const void* buffer, size_t size) { | ||
| 9 | assert(buffer); | ||
| 10 | return fnv1a32_update(fnv1a32_begin(), buffer, size); | ||
| 11 | } | ||
| 12 | |||
| 13 | uint32_t fnv1a32_begin() { return FNV_PRIME; } | ||
| 14 | |||
| 15 | uint32_t fnv1a32_update(uint32_t hash, const void* buffer, size_t size) { | ||
| 16 | assert(buffer); | ||
| 17 | const uint8_t* bytes = buffer; | ||
| 18 | for (size_t i = 0; i < size; i++) { | ||
| 19 | hash = hash ^ bytes[i]; | ||
| 20 | hash = hash * FNV_OFFSET_BASIS; | ||
| 21 | } | ||
| 22 | return hash; | ||
| 23 | } | ||
