libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
Hash methods

Classes

struct  clod_sip64_state

Macros

#define clod_sip64_init(seed)
#define clod_sip64(seed, data, size)
#define clod_crc64_init()
#define clod_crc64_add_at(crc, data, data_size, offset)
#define clod_crc64_finalise(crc)
#define clod_crc64(data, size)
#define clod_crc32_init()
#define clod_crc32_add_at(crc, data, data_size, offset)
#define clod_crc32_finalise(crc)
#define clod_crc32(data, size)
#define clod_crc24_init()
#define clod_crc24_add_at(crc, data, data_size, offset)
#define clod_crc24_finalise(crc)
#define clod_crc24(data, size)
#define clod_crc16_init()
#define clod_crc16_add_at(crc, data, data_size, offset)
#define clod_crc16_finalise(crc)
#define clod_crc16(data, size)
#define clod_crc8_init()
#define clod_crc8_add_at(crc, data, data_size, offset)
#define clod_crc8_finalise(crc)
#define clod_crc8(data, size)

Functions

clod_sip64_state clod_sip64_add (clod_sip64_state state, const void *data, size_t size)
uint64_t clod_sip64_finalise (clod_sip64_state state)
uint64_t clod_crc64_add (uint64_t crc, const void *data, size_t data_len)
uint32_t clod_crc32_add (uint32_t crc, const void *data, size_t data_len)
uint32_t clod_crc24_add (uint32_t crc, const void *data, size_t data_len)
uint16_t clod_crc16_add (uint16_t crc, const void *data, size_t data_len)
uint8_t clod_crc8_add (uint8_t crc, const void *data, size_t data_len)

Detailed Description

The general idea is each hash method provides an init/add/finalise method. Initialisation creates a hash state which is initialised to some constant optionally including a seed value. Adding is the meat of the implementation and updates the state with new data. Finalisation parses the state to produce a final output. In the case of CRC, finalisation is a single xor with a constant, which can be undone to produce the hash state as it was before finalisation by simply performing finalisation a second time.

The CRC methods use lookup tables. See libclod/src/hash/crc_tables_generate.c for how they are generated, and libclod/src/hash/crc.c for how they are used.

state = clod_<alg>_init([seed]);
state = clod_<alg>_add(state, data, size);
state = clod_<alg>_add(state, data2, size2);
state = clod_<alg>_add(state, data3, size3);
hash = clod_<alg>_finalise(state);

Often, using streaming methods is an annoying complexity, so a method for computing the checksum of a single blob of data is provided.

hash = clod_<alg>([seed], data, size)

In addition, the crc add methods can be provided a null data argument to skip the crc ahead so many bytes. This enables some fun gymnastics such as the offset add methods. One can create a crc for some large data set without actually reading or hashing it, and then add different parts of the data later - sparse data sets can be hashed efficiently.

state = clod_<alg>_init();
state = clod_<alg>_add(state, 10000); // Fast for any size
state = clod_<alg>_add_at(state, data, 1000, 9000);
state = clod_<alg>_add_at(state, data3, 2000, 0);
state = clod_<alg>_add_at(state, data2, 7000, 2000);
crc = clod_<alg>_finalise(state);

Or, an already computed crc can be updated when a small portion of the original data is modified without needing to hash anything other than the modified section.

state = clod_<alg>_finalise(old_crc);
state = clod_<alg>_add_at(state, old_data, 1000, 9000); // Adding the old data again xors it to zero.
state = clod_<alg>_add_at(state, new_data, 1000, 9000);
new_crc = clod_<alg>_finalise(state);

Macro Definition Documentation

◆ clod_sip64_init

#define clod_sip64_init ( seed)
Value:
._v0 = UINT64_C(0x736f6d6570736575) ^ seed,\
._v1 = UINT64_C(0x646f72616e646f6d) ^ seed,\
._v2 = UINT64_C(0x6c7967656e657261) ^ seed,\
._v3 = UINT64_C(0x7465646279746573) ^ seed,\
._size = 0\
})

Initialise the hash state.

Parameters
[in]seedSeed value for the hash

Definition at line 81 of file hash.h.

◆ clod_sip64

#define clod_sip64 ( seed,
data,
size )
Value:
uint64_t clod_sip64_finalise(clod_sip64_state state)
Definition siphash.c:94
clod_sip64_state clod_sip64_add(clod_sip64_state state, const void *data, size_t size)
#define clod_sip64_init(seed)
Definition hash.h:81

One-shot a SipHash result.

This is a modified variant of SipHash to support streaming, so results will be incompatible with the reference implementation.

Parameters
[in]seedSeed value for the hash
[in]dataValue to be hashed
[in]sizeSize of data
Returns
64-bit hash value

Definition at line 126 of file hash.h.

◆ clod_crc64_init

#define clod_crc64_init ( )
Value:
UINT64_C(0xFFFFFFFFFFFFFFFF)

Initialise a crc64 hash state.

Definition at line 131 of file hash.h.

◆ clod_crc64_add_at

#define clod_crc64_add_at ( crc,
data,
data_size,
offset )
Value:
(crc ^ clod_crc64_add(clod_crc64_add(0, data, data_size), nullptr, offset))
uint64_t clod_crc64_add(uint64_t crc, const void *data, size_t data_len)

Add data to the hash state at an offset. The added data is conceptually xored with any existing data.

Parameters
[in]crcHash state
[in]dataThe data to add to the hash state
[in]data_sizeThe size of data
[in]offsetThe offset of data from the end of the hashed data
Returns
The crc hash state with the given data added

Definition at line 159 of file hash.h.

◆ clod_crc64_finalise

#define clod_crc64_finalise ( crc)
Value:
(crc ^ UINT64_C(0xFFFFFFFFFFFFFFFF))

Finalise a crc64 hash.

Parameters
[in]crcHash state
Returns
64-bit hash value

Definition at line 168 of file hash.h.

◆ clod_crc64

#define clod_crc64 ( data,
size )
Value:
#define clod_crc64_init()
Definition hash.h:131
#define clod_crc64_finalise(crc)
Definition hash.h:168

One-shot a crc64 hash.

Parameters
[in]dataData to hash
[in]sizeSize of data
Returns
64-bit hash value

Definition at line 177 of file hash.h.

◆ clod_crc32_init

#define clod_crc32_init ( )
Value:
UINT32_C(0xFFFFFFFF)

Initialise a crc32 hash state.

Definition at line 182 of file hash.h.

◆ clod_crc32_add_at

#define clod_crc32_add_at ( crc,
data,
data_size,
offset )
Value:
(crc ^ clod_crc32_add(clod_crc32_add(0, data, data_size), nullptr, offset))
uint32_t clod_crc32_add(uint32_t crc, const void *data, size_t data_len)

Add data to the hash state at an offset. The added data is conceptually xored with any existing data.

Parameters
[in]crcHash state
[in]dataThe data to add to the hash state
[in]data_sizeThe size of data
[in]offsetThe offset of data from the end of the hashed data
Returns
The crc hash state with the given data added

Definition at line 212 of file hash.h.

◆ clod_crc32_finalise

#define clod_crc32_finalise ( crc)
Value:
(crc ^ UINT32_C(0xFFFFFFFF))

Finalise a crc32 hash state.

Parameters
[in]crcHash state
Returns
32-bit hash value

Definition at line 221 of file hash.h.

◆ clod_crc32

#define clod_crc32 ( data,
size )
Value:
#define clod_crc32_finalise(crc)
Definition hash.h:221
#define clod_crc32_init()
Definition hash.h:182

One-shot a crc32 hash.

Parameters
[in]dataData to hash
[in]sizeSize of data
Returns
32-bit hash value

Definition at line 230 of file hash.h.

◆ clod_crc24_init

#define clod_crc24_init ( )
Value:
UINT32_C(0xFFFFFF)

Initialise a crc24 hash state.

Definition at line 235 of file hash.h.

◆ clod_crc24_add_at

#define clod_crc24_add_at ( crc,
data,
data_size,
offset )
Value:
(crc ^ clod_crc24_add(clod_crc24_add(0, data, data_size), nullptr, offset))
uint32_t clod_crc24_add(uint32_t crc, const void *data, size_t data_len)

Add data to the hash state at an offset. The added data is conceptually xored with any existing data.

Parameters
[in]crcHash state
[in]dataThe data to add to the hash state
[in]data_sizeThe size of data
[in]offsetThe offset of data from the end of the hashed data
Returns
The crc hash state with the given data added

Definition at line 263 of file hash.h.

◆ clod_crc24_finalise

#define clod_crc24_finalise ( crc)
Value:
(crc ^ UINT32_C(0xFFFFFF))

Finalise a crc24 hash state.

Parameters
[in]crcHash state
Returns
24-bit hash value

Definition at line 272 of file hash.h.

◆ clod_crc24

#define clod_crc24 ( data,
size )
Value:
#define clod_crc24_finalise(crc)
Definition hash.h:272
#define clod_crc24_init()
Definition hash.h:235

One-shot a crc24 hash.

Parameters
[in]dataData to hash
[in]sizeSize of data
Returns
24-bit hash value

Definition at line 281 of file hash.h.

◆ clod_crc16_init

#define clod_crc16_init ( )
Value:
UINT16_C(0xFFFF)

Initialise a crc16 hash state.

Definition at line 286 of file hash.h.

◆ clod_crc16_add_at

#define clod_crc16_add_at ( crc,
data,
data_size,
offset )
Value:
(crc ^ clod_crc16_add(clod_crc16_add(0, data, data_size), nullptr, offset))
uint16_t clod_crc16_add(uint16_t crc, const void *data, size_t data_len)

Add data to the hash state at an offset. The added data is conceptually xored with any existing data.

Parameters
[in]crcHash state
[in]dataThe data to add to the hash state
[in]data_sizeThe size of data
[in]offsetThe offset of data from the end of the hashed data
Returns
The crc hash state with the given data added

Definition at line 314 of file hash.h.

◆ clod_crc16_finalise

#define clod_crc16_finalise ( crc)
Value:
(crc ^ UINT16_C(0xFFFF))

Finalise a crc16 hash state.

Parameters
[in]crcHash state
Returns
16-bit hash value

Definition at line 323 of file hash.h.

◆ clod_crc16

#define clod_crc16 ( data,
size )
Value:
#define clod_crc16_init()
Definition hash.h:286
#define clod_crc16_finalise(crc)
Definition hash.h:323

One-shot a crc16 hash.

Parameters
[in]dataData to hash
[in]sizeSize of data
Returns
16-bit hash value

Definition at line 332 of file hash.h.

◆ clod_crc8_init

#define clod_crc8_init ( )
Value:
UINT8_C(0xFF)

Initialise a crc8 hash state.

Definition at line 337 of file hash.h.

◆ clod_crc8_add_at

#define clod_crc8_add_at ( crc,
data,
data_size,
offset )
Value:
(crc ^ clod_crc8_add(clod_crc8_add(0, data, data_size), nullptr, offset))
uint8_t clod_crc8_add(uint8_t crc, const void *data, size_t data_len)

Add data to the hash state at an offset. The added data is conceptually xored with any existing data.

Parameters
[in]crcHash state
[in]dataThe data to add to the hash state
[in]data_sizeThe size of data
[in]offsetThe offset of data from the end of the hashed data
Returns
The crc hash state with the given data added

Definition at line 365 of file hash.h.

◆ clod_crc8_finalise

#define clod_crc8_finalise ( crc)
Value:
(crc ^ UINT8_C(0xFF))

Finalise a crc8 hash state.

Parameters
[in]crcHash state
Returns
8-bit hash value

Definition at line 374 of file hash.h.

◆ clod_crc8

#define clod_crc8 ( data,
size )
Value:
#define clod_crc8_init()
Definition hash.h:337
#define clod_crc8_finalise(crc)
Definition hash.h:374

One-shot a crc8 hash.

Parameters
[in]dataData to hash
[in]sizeSize of data
Returns
8-bit hash value

Definition at line 383 of file hash.h.

Function Documentation

◆ clod_sip64_add()

clod_sip64_state clod_sip64_add ( clod_sip64_state state,
const void * data,
size_t size )

Add data to a SipHash state.

This is a modified variant of SipHash to support streaming, so results will be incompatible with the reference implementation.

Parameters
[in]stateHash state
[in]dataValue to be hashed
[in]sizeSize of data
Returns
Updated state

◆ clod_sip64_finalise()

uint64_t clod_sip64_finalise ( clod_sip64_state state)

Finalise a SipHash state.

This is a modified variant of SipHash to support streaming, so results will be incompatible with the reference implementation.

Parameters
[in]stateHash state
Returns
64-bit hash value

Definition at line 94 of file siphash.c.

◆ clod_crc64_add()

uint64_t clod_crc64_add ( uint64_t crc,
const void * data,
size_t data_len )

Add data to the hash state. If data is null it functions as if data was zeroed, but can skip almost all the work.

Polynomial: 0x42F0E1EBA9EA3693 Reflected: false

Parameters
[in]crcHash state
[in]data(nullable) Data to add to hash state
[in]data_lenSize of data
Returns
Updated hash state

◆ clod_crc32_add()

uint32_t clod_crc32_add ( uint32_t crc,
const void * data,
size_t data_len )

Add data to the hash state. If data is null it functions as if data was zeroed, but can skip almost all the work.

This method uses CPU intrinsics for CRC generation when possible.

Polynomial: 0x1EDC6F41 Reflected: true

Parameters
[in]crcHash state
[in]data(nullable) Data to add to hash state
[in]data_lenSize of data
Returns
Updated hash state

◆ clod_crc24_add()

uint32_t clod_crc24_add ( uint32_t crc,
const void * data,
size_t data_len )

Add data to the hash state. If data is null it functions as if data was zeroed, but can skip almost all the work.

Polynomial: 0x864CFB Reflected: false

Parameters
[in]crcHash state
[in]data(nullable) Data to add to hash state
[in]data_lenSize of data
Returns
Updated hash state

◆ clod_crc16_add()

uint16_t clod_crc16_add ( uint16_t crc,
const void * data,
size_t data_len )

Add data to the hash state. If data is null it functions as if data was zeroed, but can skip almost all the work.

Polynomial: 0x1021 Reflected: true

Parameters
[in]crcHash state
[in]data(nullable) Data to add to hash state
[in]data_lenSize of data
Returns
Updated hash state

◆ clod_crc8_add()

uint8_t clod_crc8_add ( uint8_t crc,
const void * data,
size_t data_len )

Add data to the hash state. If data is null it functions as if data was zeroed, but can skip almost all the work.

Polynomial: 0x7 Reflected: false

Parameters
[in]crcHash state
[in]data(nullable) Data to add to hash state
[in]data_lenSize of data
Returns
Updated hash state