This commit is contained in:
@@ -6,16 +6,16 @@
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
|
||||
wp_intern inline void validateMur3HasherData(const WpMur3HasherData *io);
|
||||
wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *io);
|
||||
wp_intern inline u64 fmix64(u64 k);
|
||||
|
||||
WpHasher wpX64Mur3Hasher(WpMur3HasherData *data) {
|
||||
WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *data) {
|
||||
return (WpHasher){ .func = wpX64Mur3Hasher128, .data = (void *)data };
|
||||
}
|
||||
|
||||
WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) {
|
||||
WpMur3HasherData *io = (WpMur3HasherData *)hasher_io;
|
||||
validateMur3HasherData(io);
|
||||
WpMur3HasherCtx *io = (WpMur3HasherCtx *)hasher_io;
|
||||
validateMur3HasherCtx(io);
|
||||
|
||||
const i32 nblocks = (bytes->count * bytes->item_size) / 16;
|
||||
|
||||
@@ -99,8 +99,8 @@ WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io) {
|
||||
return (WpU128Hash){ .hash = { [0] = h1, [1] = h2 } };
|
||||
}
|
||||
|
||||
wp_intern inline void validateMur3HasherData(const WpMur3HasherData *data) {
|
||||
wpRuntimeAssert(data->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash data");
|
||||
wp_intern inline void validateMur3HasherCtx(const WpMur3HasherCtx *ctx) {
|
||||
wpRuntimeAssert(ctx->magic == WP_MUR3_HASHER_MAGIC, "Invalid Murmur3 hash context");
|
||||
}
|
||||
|
||||
wp_intern inline u64 fmix64(u64 k) {
|
||||
|
||||
@@ -19,18 +19,18 @@ BEGIN_C_LINKAGE
|
||||
|
||||
#define WP_MUR3_HASHER_MAGIC wpU64Const(0x57504d4841534833)
|
||||
|
||||
typedef struct WpMur3HasherData {
|
||||
typedef struct WpMur3HasherCtx {
|
||||
u64 magic;
|
||||
u32 seed;
|
||||
} WpMur3HasherData;
|
||||
} WpMur3HasherCtx;
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
#define wpMur3HasherData(SEED) (WpMur3HasherData{WP_MUR3_HASHER_MAGIC, SEED})
|
||||
#define wpMur3HasherCtx(SEED) (WpMur3HasherCtx{WP_MUR3_HASHER_MAGIC, SEED})
|
||||
#else
|
||||
#define wpMur3HasherData(SEED) ((WpMur3HasherData){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED })
|
||||
#define wpMur3HasherCtx(SEED) ((WpMur3HasherCtx){ .magic = WP_MUR3_HASHER_MAGIC, .seed = SEED })
|
||||
#endif
|
||||
|
||||
WpHasher wpX64Mur3Hasher(WpMur3HasherData *data);
|
||||
WpHasher wpX64Mur3Hasher(WpMur3HasherCtx *ctx);
|
||||
WpU128Hash wpX64Mur3Hasher128(WpU8Stream *bytes, void *hasher_io);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out, u64 outlen);
|
||||
wp_intern inline void validateSipHasherData(const WpSipHasherData *io);
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherData *params);
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out, u64 outlen);
|
||||
wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *io);
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherCtx *params);
|
||||
|
||||
#define _u32To8LE(p, v) \
|
||||
(p)[0] = (u8)((v)); \
|
||||
@@ -44,13 +44,13 @@ wp_intern inline b8 checkRounds(const WpSipHasherData *params);
|
||||
v2 = wpMiscUtilsRotl64(v2, 32); \
|
||||
} while (0)
|
||||
|
||||
WpHasher wpSipHasher(WpSipHasherData *data) {
|
||||
WpHasher wpSipHasher(WpSipHasherCtx *data) {
|
||||
return (WpHasher){ .func = wpSipHasher128, .data = (void *)data };
|
||||
}
|
||||
|
||||
WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) {
|
||||
WpSipHasherData *data = (WpSipHasherData *)hasher_data;
|
||||
validateSipHasherData(data);
|
||||
WpSipHasherCtx *data = (WpSipHasherCtx *)hasher_data;
|
||||
validateSipHasherCtx(data);
|
||||
WpU128Hash output = {0};
|
||||
siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2);
|
||||
return output;
|
||||
@@ -59,7 +59,7 @@ WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) {
|
||||
/**
|
||||
* Implementation of the 128-variant of the siphash algorithm
|
||||
*/
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out,
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out,
|
||||
u64 outlen) {
|
||||
const u8 *kk = (const u8 *)¶ms->key;
|
||||
u64 inlen = bytes->count * bytes->item_size;
|
||||
@@ -150,14 +150,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8
|
||||
_u64To8LE(out + 8, b);
|
||||
}
|
||||
|
||||
wp_intern inline void validateSipHasherData(const WpSipHasherData *data) {
|
||||
b8 magic_match = data->magic == WP_SIP_HASHER_MAGIC;
|
||||
b8 rounds = checkRounds(data);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash data");
|
||||
wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *ctx) {
|
||||
b8 magic_match = ctx->magic == WP_SIP_HASHER_MAGIC;
|
||||
b8 rounds = checkRounds(ctx);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash context");
|
||||
}
|
||||
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherData *data) {
|
||||
b8 rounds24 = data->compression == 2 && data->finalisation == 4;
|
||||
b8 rounds48 = data->compression == 4 && data->finalisation == 8;
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherCtx *ctx) {
|
||||
b8 rounds24 = ctx->compression == 2 && ctx->finalisation == 4;
|
||||
b8 rounds48 = ctx->compression == 4 && ctx->finalisation == 8;
|
||||
return rounds24 || rounds48;
|
||||
}
|
||||
|
||||
@@ -24,24 +24,24 @@ typedef struct WpSipHasherKey {
|
||||
u64 hi;
|
||||
} WpSipHasherKey;
|
||||
|
||||
typedef struct WpSipHasherData {
|
||||
typedef struct WpSipHasherCtx {
|
||||
WpSipHasherKey key;
|
||||
u64 magic;
|
||||
u64 compression;
|
||||
u64 finalisation;
|
||||
} WpSipHasherData;
|
||||
} WpSipHasherCtx;
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
#define wpSip24HasherData(KEY) (WpSipHasherData{(KEY), WP_SIP_HASHER_MAGIC, 2, 4})
|
||||
#define wpSip48HasherData(KEY) (WpSipHasherData{(KEY), WP_SIP_HASHER_MAGIC, 4, 8})
|
||||
#define wpSip24HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 2, 4})
|
||||
#define wpSip48HasherCtx(KEY) (WpSipHasherCtx{(KEY), WP_SIP_HASHER_MAGIC, 4, 8})
|
||||
#else
|
||||
#define wpSip24HasherData(KEY) ((WpSipHasherData){ \
|
||||
#define wpSip24HasherCtx(KEY) ((WpSipHasherCtx){ \
|
||||
.key = (KEY), \
|
||||
.magic = WP_SIP_HASHER_MAGIC, \
|
||||
.compression = 2, \
|
||||
.finalisation = 4, \
|
||||
})
|
||||
#define wpSip48HasherData(KEY) ((WpSipHasherData){ \
|
||||
#define wpSip48HasherCtx(KEY) ((WpSipHasherCtx){ \
|
||||
.key = (KEY), \
|
||||
.magic = WP_SIP_HASHER_MAGIC, \
|
||||
.compression = 4, \
|
||||
@@ -49,7 +49,7 @@ typedef struct WpSipHasherData {
|
||||
})
|
||||
#endif
|
||||
|
||||
WpHasher wpSipHasher(WpSipHasherData *data);
|
||||
WpHasher wpSipHasher(WpSipHasherCtx *ctx);
|
||||
WpU128Hash wpSipHasher128(WpU8Stream *bytes, void *hasher_io);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
|
||||
Reference in New Issue
Block a user