meta: rename to fx
This commit is contained in:
136
core/hash/hash.c
136
core/hash/hash.c
@@ -1,7 +1,7 @@
|
||||
#include "hash.h"
|
||||
|
||||
#include <blue/core/hash.h>
|
||||
#include <blue/core/rope.h>
|
||||
#include <fx/core/hash.h>
|
||||
#include <fx/core/rope.h>
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -10,46 +10,46 @@
|
||||
#define FNV1_OFFSET_BASIS 0xcbf29ce484222325
|
||||
#define FNV1_PRIME 0x100000001b3
|
||||
|
||||
extern struct b_hash_function_ops z__b_md4_ops;
|
||||
extern struct b_hash_function_ops z__b_md5_ops;
|
||||
extern struct b_hash_function_ops z__b_sha1_ops;
|
||||
extern struct b_hash_function_ops z__b_sha2_224_ops;
|
||||
extern struct b_hash_function_ops z__b_sha2_256_ops;
|
||||
extern struct b_hash_function_ops z__b_sha2_384_ops;
|
||||
extern struct b_hash_function_ops z__b_sha2_512_ops;
|
||||
extern struct b_hash_function_ops z__b_sha3_224_ops;
|
||||
extern struct b_hash_function_ops z__b_sha3_256_ops;
|
||||
extern struct b_hash_function_ops z__b_sha3_384_ops;
|
||||
extern struct b_hash_function_ops z__b_sha3_512_ops;
|
||||
extern struct b_hash_function_ops z__b_shake128_ops;
|
||||
extern struct b_hash_function_ops z__b_shake256_ops;
|
||||
extern struct fx_hash_function_ops z__fx_md4_ops;
|
||||
extern struct fx_hash_function_ops z__fx_md5_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha1_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha2_224_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha2_256_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha2_384_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha2_512_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha3_224_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha3_256_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha3_384_ops;
|
||||
extern struct fx_hash_function_ops z__fx_sha3_512_ops;
|
||||
extern struct fx_hash_function_ops z__fx_shake128_ops;
|
||||
extern struct fx_hash_function_ops z__fx_shake256_ops;
|
||||
|
||||
static const struct b_hash_function_ops *hash_functions[] = {
|
||||
[B_HASH_NONE] = NULL,
|
||||
[B_HASH_MD4] = &z__b_md4_ops,
|
||||
[B_HASH_MD5] = &z__b_md5_ops,
|
||||
[B_HASH_SHA1] = &z__b_sha1_ops,
|
||||
[B_HASH_SHA2_224] = &z__b_sha2_224_ops,
|
||||
[B_HASH_SHA2_256] = &z__b_sha2_256_ops,
|
||||
[B_HASH_SHA2_384] = &z__b_sha2_384_ops,
|
||||
[B_HASH_SHA2_512] = &z__b_sha2_512_ops,
|
||||
[B_HASH_SHA3_224] = &z__b_sha3_224_ops,
|
||||
[B_HASH_SHA3_256] = &z__b_sha3_256_ops,
|
||||
[B_HASH_SHA3_384] = &z__b_sha3_384_ops,
|
||||
[B_HASH_SHA3_512] = &z__b_sha3_512_ops,
|
||||
[B_HASH_SHAKE128] = &z__b_shake128_ops,
|
||||
[B_HASH_SHAKE256] = &z__b_shake256_ops,
|
||||
static const struct fx_hash_function_ops *hash_functions[] = {
|
||||
[FX_HASH_NONE] = NULL,
|
||||
[FX_HASH_MD4] = &z__fx_md4_ops,
|
||||
[FX_HASH_MD5] = &z__fx_md5_ops,
|
||||
[FX_HASH_SHA1] = &z__fx_sha1_ops,
|
||||
[FX_HASH_SHA2_224] = &z__fx_sha2_224_ops,
|
||||
[FX_HASH_SHA2_256] = &z__fx_sha2_256_ops,
|
||||
[FX_HASH_SHA2_384] = &z__fx_sha2_384_ops,
|
||||
[FX_HASH_SHA2_512] = &z__fx_sha2_512_ops,
|
||||
[FX_HASH_SHA3_224] = &z__fx_sha3_224_ops,
|
||||
[FX_HASH_SHA3_256] = &z__fx_sha3_256_ops,
|
||||
[FX_HASH_SHA3_384] = &z__fx_sha3_384_ops,
|
||||
[FX_HASH_SHA3_512] = &z__fx_sha3_512_ops,
|
||||
[FX_HASH_SHAKE128] = &z__fx_shake128_ops,
|
||||
[FX_HASH_SHAKE256] = &z__fx_shake256_ops,
|
||||
};
|
||||
static const size_t nr_hash_functions
|
||||
= sizeof hash_functions / sizeof hash_functions[0];
|
||||
|
||||
uint64_t b_hash_cstr(const char *s)
|
||||
uint64_t fx_hash_cstr(const char *s)
|
||||
{
|
||||
size_t x = 0;
|
||||
return b_hash_cstr_ex(s, &x);
|
||||
return fx_hash_cstr_ex(s, &x);
|
||||
}
|
||||
|
||||
uint64_t b_hash_cstr_ex(const char *s, size_t *len)
|
||||
uint64_t fx_hash_cstr_ex(const char *s, size_t *len)
|
||||
{
|
||||
uint64_t hash = FNV1_OFFSET_BASIS;
|
||||
size_t i = 0;
|
||||
@@ -66,15 +66,15 @@ uint64_t b_hash_cstr_ex(const char *s, size_t *len)
|
||||
return hash;
|
||||
}
|
||||
|
||||
enum b_status b_hash_ctx_init(struct b_hash_ctx *ctx, enum b_hash_function func)
|
||||
enum fx_status fx_hash_ctx_init(struct fx_hash_ctx *ctx, enum fx_hash_function func)
|
||||
{
|
||||
if (func < 0 || func >= nr_hash_functions) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const struct b_hash_function_ops *hash_function = hash_functions[func];
|
||||
const struct fx_hash_function_ops *hash_function = hash_functions[func];
|
||||
if (!hash_function) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
memset(ctx, 0x0, sizeof *ctx);
|
||||
@@ -86,13 +86,13 @@ enum b_status b_hash_ctx_init(struct b_hash_ctx *ctx, enum b_hash_function func)
|
||||
hash_function->hash_init(ctx);
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_hash_ctx_reset(struct b_hash_ctx *ctx)
|
||||
enum fx_status fx_hash_ctx_reset(struct fx_hash_ctx *ctx)
|
||||
{
|
||||
if (ctx->ctx_func == B_HASH_NONE || !ctx->ctx_ops) {
|
||||
return B_ERR_BAD_STATE;
|
||||
if (ctx->ctx_func == FX_HASH_NONE || !ctx->ctx_ops) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
memset(&ctx->ctx_state, 0x0, sizeof ctx->ctx_state);
|
||||
@@ -101,69 +101,69 @@ enum b_status b_hash_ctx_reset(struct b_hash_ctx *ctx)
|
||||
ctx->ctx_ops->hash_init(ctx);
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_hash_ctx_update(struct b_hash_ctx *ctx, const void *p, size_t len)
|
||||
enum fx_status fx_hash_ctx_update(struct fx_hash_ctx *ctx, const void *p, size_t len)
|
||||
{
|
||||
if (!ctx->ctx_ops) {
|
||||
return B_ERR_BAD_STATE;
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (!ctx->ctx_ops->hash_update) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
ctx->ctx_ops->hash_update(ctx, p, len);
|
||||
return B_SUCCESS;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static void update_rope(const b_rope *rope, void *arg)
|
||||
static void update_rope(const fx_rope *rope, void *arg)
|
||||
{
|
||||
struct b_hash_ctx *ctx = arg;
|
||||
unsigned int type = B_ROPE_TYPE(rope->r_flags);
|
||||
struct fx_hash_ctx *ctx = arg;
|
||||
unsigned int type = FX_ROPE_TYPE(rope->r_flags);
|
||||
char tmp[64];
|
||||
size_t len = 0;
|
||||
|
||||
switch (type) {
|
||||
case B_ROPE_F_CHAR:
|
||||
b_hash_ctx_update(ctx, &rope->r_v.v_char, sizeof rope->r_v.v_char);
|
||||
case FX_ROPE_F_CHAR:
|
||||
fx_hash_ctx_update(ctx, &rope->r_v.v_char, sizeof rope->r_v.v_char);
|
||||
break;
|
||||
case B_ROPE_F_CSTR:
|
||||
case B_ROPE_F_CSTR_BORROWED:
|
||||
case B_ROPE_F_CSTR_STATIC:
|
||||
b_hash_ctx_update(
|
||||
case FX_ROPE_F_CSTR:
|
||||
case FX_ROPE_F_CSTR_BORROWED:
|
||||
case FX_ROPE_F_CSTR_STATIC:
|
||||
fx_hash_ctx_update(
|
||||
ctx, rope->r_v.v_cstr.s, strlen(rope->r_v.v_cstr.s));
|
||||
break;
|
||||
case B_ROPE_F_INT:
|
||||
case FX_ROPE_F_INT:
|
||||
len = snprintf(tmp, sizeof tmp, "%" PRIdPTR, rope->r_v.v_int);
|
||||
b_hash_ctx_update(ctx, tmp, len);
|
||||
fx_hash_ctx_update(ctx, tmp, len);
|
||||
break;
|
||||
case B_ROPE_F_UINT:
|
||||
case FX_ROPE_F_UINT:
|
||||
len = snprintf(tmp, sizeof tmp, "%" PRIuPTR, rope->r_v.v_uint);
|
||||
b_hash_ctx_update(ctx, tmp, len);
|
||||
fx_hash_ctx_update(ctx, tmp, len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
enum b_status b_hash_ctx_update_rope(
|
||||
struct b_hash_ctx *ctx, const struct b_rope *rope)
|
||||
enum fx_status fx_hash_ctx_update_rope(
|
||||
struct fx_hash_ctx *ctx, const struct fx_rope *rope)
|
||||
{
|
||||
b_rope_iterate(rope, update_rope, ctx);
|
||||
return B_SUCCESS;
|
||||
fx_rope_iterate(rope, update_rope, ctx);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_hash_ctx_finish(
|
||||
struct b_hash_ctx *ctx, void *out_digest, size_t out_max)
|
||||
enum fx_status fx_hash_ctx_finish(
|
||||
struct fx_hash_ctx *ctx, void *out_digest, size_t out_max)
|
||||
{
|
||||
if (!ctx->ctx_ops) {
|
||||
return B_ERR_BAD_STATE;
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (!ctx->ctx_ops->hash_finish) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
return FX_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
ctx->ctx_ops->hash_finish(ctx, out_digest, out_max);
|
||||
@@ -174,5 +174,5 @@ enum b_status b_hash_ctx_finish(
|
||||
ctx->ctx_ops->hash_init(ctx);
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user