hash: add hash_digestcb() function

May be useful for calculating hash of a stream of input data.
This commit is contained in:
CismonX 2025-02-06 16:09:58 +08:00
parent 767e25e867
commit b195e7821a
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
2 changed files with 48 additions and 0 deletions

View file

@ -67,6 +67,29 @@ hash_digestv (
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
hash_seed (
uint64_t s

View file

@ -29,6 +29,19 @@
#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.
*/
@ -49,6 +62,18 @@ hash_digestv (
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.
* If not called, the seed equals to value 0.