mirror of
https://git.sr.ht/~cismonx/bookmarkfs
synced 2025-06-07 19:58:50 +00:00
hash: add hash_digestcb() function
May be useful for calculating hash of a stream of input data.
This commit is contained in:
parent
767e25e867
commit
b195e7821a
2 changed files with 48 additions and 0 deletions
23
src/hash.c
23
src/hash.c
|
@ -67,6 +67,29 @@ hash_digestv (
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t
|
||||||
|
hash_digestcb (
|
||||||
|
hash_digestcb_func *callback,
|
||||||
|
void *user_data
|
||||||
|
) {
|
||||||
|
XXH3_state_t *state = XXH3_createState();
|
||||||
|
xassert(state != NULL);
|
||||||
|
|
||||||
|
xassert(0 == XXH3_64bits_reset_withSeed(state, seed));
|
||||||
|
while (1) {
|
||||||
|
void const *buf;
|
||||||
|
size_t data_len = callback(user_data, &buf);
|
||||||
|
if (data_len == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
xassert(0 == XXH3_64bits_update(state, buf, data_len));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t result = XXH3_64bits_digest(state);
|
||||||
|
XXH3_freeState(state);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
hash_seed (
|
hash_seed (
|
||||||
uint64_t s
|
uint64_t s
|
||||||
|
|
25
src/hash.h
25
src/hash.h
|
@ -29,6 +29,19 @@
|
||||||
|
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback function for hash_digestcb().
|
||||||
|
*
|
||||||
|
* The function should store the pointer to a buffer to buf_ptr,
|
||||||
|
* which only need to be valid until the next call to the callback.
|
||||||
|
*
|
||||||
|
* Returns the length of data in buffer, or 0 to terminate.
|
||||||
|
*/
|
||||||
|
typedef size_t (hash_digestcb_func) (
|
||||||
|
void *user_data,
|
||||||
|
void const **buf_ptr
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the 64-bit hash for the given input.
|
* Calculate the 64-bit hash for the given input.
|
||||||
*/
|
*/
|
||||||
|
@ -49,6 +62,18 @@ hash_digestv (
|
||||||
int bufcnt
|
int bufcnt
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like hash_digest(), but takes a callback function.
|
||||||
|
*
|
||||||
|
* The callback function is called continuously for each part of
|
||||||
|
* the input data, until it returns 0.
|
||||||
|
*/
|
||||||
|
uint64_t
|
||||||
|
hash_digestcb (
|
||||||
|
hash_digestcb_func *callback,
|
||||||
|
void *user_data
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seed the hash function with the given value.
|
* Seed the hash function with the given value.
|
||||||
* If not called, the seed equals to value 0.
|
* If not called, the seed equals to value 0.
|
||||||
|
|
Loading…
Add table
Reference in a new issue