Added a checksum to the content file.

This commit is contained in:
Andrea Mazzoleni 2011-12-31 12:09:05 +01:00
parent cd906d5dac
commit a0ad57d4f1
8 changed files with 210 additions and 39 deletions

80
util.h
View file

@ -369,5 +369,85 @@ void* malloc_nofail_align(size_t size, void** freeptr);
*/
void memhash(unsigned kind, void* digest, const void* src, unsigned size);
/****************************************************************************/
/* sax */
/**
* One-at-a-time hash type.
* This is a really fast, but insecure hash.
* This is only *partially* based on the Bob Jenkins one-at-a-time hash.
*/
typedef uint32_t oathash_t;
/**
* Hash a byte.
*/
static inline oathash_t oathash8(oathash_t hash, unsigned char value)
{
hash += value;
hash += hash << 10;
hash ^= hash >> 6;
return hash;
}
/**
* Hash a 32 bits word.
*/
static inline oathash_t oathash32(oathash_t hash, uint32_t value)
{
hash += value;
hash += hash << 10;
hash ^= hash >> 6;
return hash;
}
/**
* Hash a 64 bits word.
* \note Based on one-at-a-time by Bob Jenkins.
*/
static inline oathash_t oathash64(oathash_t hash, uint64_t value)
{
hash += value & 0xFFFFFFFF;
hash += hash << 10;
hash ^= hash >> 6;
hash += value >> 32;
hash += hash << 10;
hash ^= hash >> 6;
return hash;
}
/**
* Hash a string.
*/
static inline oathash_t oathashs(oathash_t hash, const char* value)
{
while (*value) {
hash += *value;
hash += hash << 10;
hash ^= hash >> 6;
++value;
}
return hash;
}
/**
* Hash a range of memory.
* The size must be a 32 bits multiplier.
*/
static inline oathash_t oathashm(oathash_t hash, const unsigned char* value, unsigned size)
{
assert(size % 4 == 0);
size /= 4;
while (size--) {
uint32_t w32 = value[0] | ((unsigned)value[1] << 8) | ((unsigned)value[2] << 16) | ((unsigned)value[3] << 24);
hash += w32;
hash += hash << 10;
hash ^= hash >> 6;
value += 4;
}
return hash;
}
#endif