Public API » XXH32 family module

Contains functions used in the classic 32-bit xxHash algorithm.

Classes

struct XXH32_canonical_t
Canonical (big endian) representation of XXH32_hash_t.

Typedefs

using XXH32_state_t = struct XXH32_state_s
The opaque state struct for the XXH32 streaming API.

Functions

auto XXH32(const void* input, size_t length, XXH32_hash_t seed) -> XXH_PUBLIC_API XXH32_hash_t
Calculates the 32-bit hash of input using xxHash32.
auto XXH32_createState(void) -> XXH_PUBLIC_API XXH32_state_t*
Allocates an XXH32_state_t.
auto XXH32_freeState(XXH32_state_t* statePtr) -> XXH_PUBLIC_API XXH_errorcode
Frees an XXH32_state_t.
auto XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state) -> XXH_PUBLIC_API void
Copies one XXH32_state_t to another.
auto XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed) -> XXH_PUBLIC_API XXH_errorcode
Resets an XXH32_state_t to begin a new hash.
auto XXH32_update(XXH32_state_t* statePtr, const void* input, size_t length) -> XXH_PUBLIC_API XXH_errorcode
Consumes a block of input to an XXH32_state_t.
auto XXH32_digest(const XXH32_state_t* statePtr) -> XXH_PUBLIC_API XXH32_hash_t
Returns the calculated hash value from an XXH32_state_t.
auto XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash) -> XXH_PUBLIC_API void
Converts an XXH32_hash_t to a big endian XXH32_canonical_t.
auto XXH32_hashFromCanonical(const XXH32_canonical_t* src) -> XXH_PUBLIC_API XXH32_hash_t
Converts an XXH32_canonical_t to a native XXH32_hash_t.

Typedef documentation

typedef struct XXH32_state_s XXH32_state_t

The opaque state struct for the XXH32 streaming API.

Streaming functions generate the xxHash value from an incremental input. This method is slower than single-call functions, due to state management. For small inputs, prefer XXH32() and XXH64(), which are better optimized.

An XXH state must first be allocated using XXH*_createState().

Start a new hash by initializing the state with a seed using XXH*_reset().

Then, feed the hash state by calling XXH*_update() as many times as necessary.

The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.

Finally, a hash value can be produced anytime, by using XXH*_digest(). This function returns the nn-bits hash as an int or long long.

It's still possible to continue inserting input into the hash state after a digest, and generate new hash values later on by invoking XXH*_digest().

When done, release the state using XXH*_freeState().

Example code for incrementally hashing a file:

#include <stdio.h>
#include <xxhash.h>
#define BUFFER_SIZE 256

// Note: XXH64 and XXH3 use the same interface.
XXH32_hash_t
hashFile(FILE* stream)
{
    XXH32_state_t* state;
    unsigned char buf[BUFFER_SIZE];
    size_t amt;
    XXH32_hash_t hash;

    state = XXH32_createState();       // Create a state
    assert(state != NULL);             // Error check here
    XXH32_reset(state, 0xbaad5eed);    // Reset state with our seed
    while ((amt = fread(buf, 1, sizeof(buf), stream)) != 0) {
        XXH32_update(state, buf, amt); // Hash the file in chunks
    }
    hash = XXH32_digest(state);        // Finalize the hash
    XXH32_freeState(state);            // Clean up
    return hash;
}

Function documentation

XXH_PUBLIC_API XXH32_hash_t XXH32(const void* input, size_t length, XXH32_hash_t seed)

Calculates the 32-bit hash of input using xxHash32.

Parameters
input The block of data to be hashed, at least length bytes in size.
length The length of input, in bytes.
seed The 32-bit seed to alter the hash's output predictably.
Returns The calculated 32-bit hash value.

Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark): 5.4 GB/s

XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)

Allocates an XXH32_state_t.

Returns An allocated XXH32_state_t on success, NULL on failure.

Must be freed with XXH32_freeState().

XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)

Frees an XXH32_state_t.

Parameters
statePtr A pointer to an XXH32_state_t allocated with XXH32_createState().
Returns XXH_OK.

Must be allocated with XXH32_createState().

XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state)

Copies one XXH32_state_t to another.

Parameters
dst_state The state to copy to.
src_state The state to copy from.

XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)

Resets an XXH32_state_t to begin a new hash.

Parameters
statePtr The state struct to reset.
seed The 32-bit seed to alter the hash result predictably.
Returns XXH_OK on success, XXH_ERROR on failure.

This function resets and seeds a state. Call it before XXH32_update().

XXH_PUBLIC_API XXH_errorcode XXH32_update(XXH32_state_t* statePtr, const void* input, size_t length)

Consumes a block of input to an XXH32_state_t.

Parameters
statePtr The state struct to update.
input The block of data to be hashed, at least length bytes in size.
length The length of input, in bytes.
Returns XXH_OK on success, XXH_ERROR on failure.

Call this to incrementally consume blocks of data.

XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t* statePtr)

Returns the calculated hash value from an XXH32_state_t.

Parameters
statePtr The state struct to calculate the hash from.
Returns The calculated xxHash32 value from that state.

XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)

Converts an XXH32_hash_t to a big endian XXH32_canonical_t.

Parameters
dst The XXH32_canonical_t pointer to be stored to.
hash The XXH32_hash_t to be converted.

XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)

Converts an XXH32_canonical_t to a native XXH32_hash_t.

Parameters
src The XXH32_canonical_t to convert.
Returns The converted hash.