diff options
| author | ame <[email protected]> | 2024-03-27 13:34:34 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-03-27 13:34:34 -0500 |
| commit | 57d67eb910aa765df2495df9f625bfc00436014b (patch) | |
| tree | 11db0e99732c9851c4a2c94f3e318da1cdec7716 /src/hash | |
| parent | b1f57a05f31a04b6d0dfbdfaf358ce8adfd45dda (diff) | |
fixed a few hashes
Diffstat (limited to 'src/hash')
| -rw-r--r-- | src/hash/adler.c | 31 | ||||
| -rw-r--r-- | src/hash/bsdchecksum.c | 50 | ||||
| -rw-r--r-- | src/hash/bsdchecksum.h | 12 | ||||
| -rw-r--r-- | src/hash/buzhash.c | 20 | ||||
| -rw-r--r-- | src/hash/buzhash.h | 4 | ||||
| -rw-r--r-- | src/hash/crc.c | 126 | ||||
| -rw-r--r-- | src/hash/crc.h | 63 |
7 files changed, 195 insertions, 111 deletions
diff --git a/src/hash/adler.c b/src/hash/adler.c index 7d922e9..c6b4d7f 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -23,35 +23,7 @@ uint32_t adler32(uint8_t* aa, size_t len){ return adler32_final(&dig); } -int l_adler32_init(lua_State* L){ - lua_newtable(L); - int t = lua_gettop(L); - - struct adler32_hash* a = (struct adler32_hash*)lua_newuserdata(L, sizeof * a); - int ud = lua_gettop(L); - *a = adler32_init(); - - luaI_tsetv(L, t, "ud", ud); - luaI_tsetcf(L, t, "update", l_adler32_update); - luaI_tsetcf(L, t, "final", l_adler32_final); - - lua_pushvalue(L, t); - return 1; -} - -int l_adler32_update(lua_State* L){ - lua_pushstring(L, "ud"); - lua_gettable(L, 1); - - struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, -1); - size_t len = 0; - uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len); - - adler32_update(b, len, a); - - lua_pushvalue(L, 1); - return 1; -} +common_hash_init_update(adler32); int l_adler32_final(lua_State* L){ lua_pushstring(L, "ud"); @@ -66,6 +38,7 @@ int l_adler32_final(lua_State* L){ } int l_adler32(lua_State* L){ + if(lua_gettop(L) == 0) return l_adler32_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c index 9904352..a816b5c 100644 --- a/src/hash/bsdchecksum.c +++ b/src/hash/bsdchecksum.c @@ -2,26 +2,52 @@ #include <stdio.h> #include <stdint.h> -uint16_t i_bsdchecksum(uint8_t *aa, size_t len){ - uint16_t check = 0x0; - - for(int i = 0; i != len; i++){ - uint8_t a = aa[i]; - check = (check >> 1) + ((check & 1) << 15); - check += a; - check &= 0xffff; - } - return check; +struct bsdchecksum_hash bsdchecksum_init(){ + return (struct bsdchecksum_hash){.check = 0x0}; +} + +void bsdchecksum_update(uint8_t* aa, size_t len, struct bsdchecksum_hash* hash){ + for(int i = 0; i != len; i++){ + uint8_t a = aa[i]; + hash->check = (hash->check >> 1) + ((hash->check & 1) << 15); + hash->check += a; + hash->check &= 0xffff; + } +} + +uint16_t bsdchecksum_final(struct bsdchecksum_hash* hash){ + return hash->check; +} + +uint16_t bsdchecksum(uint8_t* a, size_t len){ + struct bsdchecksum_hash b = bsdchecksum_init(); + bsdchecksum_update(a, len, &b); + return bsdchecksum_final(&b); +} + +common_hash_init_update(bsdchecksum); + +int l_bsdchecksum_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, -1); + uint32_t u = bsdchecksum_final(a); + char digest[32]; + sprintf(digest,"%i",u); + lua_pushstring(L, digest); + return 1; } int l_bsdchecksum(lua_State* L){ - + if(lua_gettop(L) == 0) return l_bsdchecksum_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[16]; - uint16_t u = i_bsdchecksum(a, len); + //uint16_t u = i_bsdchecksum(a, len); + uint16_t u = bsdchecksum(a, len); sprintf(digest,"%i",u); lua_pushstring(L, digest); diff --git a/src/hash/bsdchecksum.h b/src/hash/bsdchecksum.h index 8e41893..9344e9a 100644 --- a/src/hash/bsdchecksum.h +++ b/src/hash/bsdchecksum.h @@ -1,6 +1,10 @@ #include "../lua.h" #include <stdint.h> +struct bsdchecksum_hash { + uint16_t check; +}; + /** * calculates a bsdchecksum of (len) bytes * @@ -8,6 +12,12 @@ * @param {size_t} input length * @return {uint16_t} 16 bit checksum */ -uint16_t i_bsdchecksum(uint8_t*, size_t); +uint16_t bsdchecksum(uint8_t*, size_t); +struct bsdchecksum_hash bsdchecksum_init(); +void bsdchecksum_update(uint8_t*, size_t, struct bsdchecksum_hash*); +uint16_t bsdchecksum_final(struct bsdchecksum_hash*); int l_bsdchecksum(lua_State*); +int l_bsdchecksum_init(lua_State*); +int l_bsdchecksum_update(lua_State*); +int l_bsdchecksum_final(lua_State*); diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c index 9e1b560..ec2705d 100644 --- a/src/hash/buzhash.c +++ b/src/hash/buzhash.c @@ -19,28 +19,20 @@ static uint8_t T[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, 252,253,254,255}; -uint8_t i_lr8(uint8_t y, uint8_t offset){ - return ( y << offset ) | ( y >> (8 - offset)); -} - -uint16_t i_lr16(uint16_t y, uint16_t offset){ - return ( y << offset ) | ( y >> (16 - offset)); -} - -uint8_t i_buzhash8(uint8_t* in, size_t len){ +uint8_t buzhash8(uint8_t* in, size_t len){ uint8_t hash = 0; for(int i = 0; i != len; i++){ - hash ^= i_lr8(T[(uint8_t)in[i]],len - (i + 1)); + hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1)); } return hash; } -uint16_t i_buzhash16(uint8_t* in, size_t len){ +uint16_t buzhash16(uint8_t* in, size_t len){ uint16_t hash = 0; for(int i = 0; i != len; i++){ - hash ^= i_lr16(T[(uint8_t)in[i]],len - (i + 1)); + hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1)); } return hash; @@ -72,7 +64,7 @@ int l_buzhash8(lua_State* L){ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[3]; - uint8_t u = i_buzhash8(a, len); + uint8_t u = buzhash8(a, len); sprintf(digest,"%x",u); @@ -86,7 +78,7 @@ int l_buzhash16(lua_State* L){ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[6]; - uint16_t u = i_buzhash16(a, len); + uint16_t u = buzhash16(a, len); sprintf(digest,"%04x",u); diff --git a/src/hash/buzhash.h b/src/hash/buzhash.h index abc7fea..98f4929 100644 --- a/src/hash/buzhash.h +++ b/src/hash/buzhash.h @@ -1,8 +1,8 @@ #include "../lua.h" #include <stdint.h> -uint8_t i_buzhash8(uint8_t*, size_t); -uint16_t i_buzhash16(uint8_t*, size_t); +uint8_t buzhash8(uint8_t*, size_t); +uint16_t buzhash16(uint8_t*, size_t); int l_setbuzhash(lua_State*); int l_buzhash8(lua_State*); diff --git a/src/hash/crc.c b/src/hash/crc.c index 5e4f8d7..7d9c41f 100644 --- a/src/hash/crc.c +++ b/src/hash/crc.c @@ -4,60 +4,132 @@ #include <stdio.h> #include <stdint.h> -uint32_t i_crc32(uint8_t *data, size_t len){ - uint32_t crc = 0xFFFFFFFF; +struct crc32_hash crc32_init(){ + return (struct crc32_hash){.crc = 0xFFFFFFFF}; +} +void crc32_update(uint8_t* data, size_t len, struct crc32_hash* hash){ for(int i = 0; i < len; i++){ uint32_t extract = data[i]; for(int z = 0; z < 8; z++){ - uint32_t b = (extract^crc)&1; - crc>>=1; - if(b) crc^=0xEDB88320; + uint32_t b = (extract^hash->crc)&1; + hash->crc>>=1; + if(b) hash->crc^=0xEDB88320; extract>>=1; } } - return -(crc+1); } -uint16_t i_crc16(uint8_t *aa, size_t len){ - uint16_t crc = 0x0; - +uint32_t crc32_final(struct crc32_hash* hash){ + return -(hash->crc+1); +} + +uint32_t crc32(uint8_t* data, size_t len){ + struct crc32_hash a = crc32_init(); + crc32_update(data, len, &a); + return crc32_final(&a); +} + +struct crc16_hash crc16_init(){ + return (struct crc16_hash){.crc = 0x0}; +} + +void crc16_update(uint8_t *aa, size_t len, struct crc16_hash *hash){ for(int i = 0; i != len; i++){ uint8_t a = aa[i]; - crc ^= a; + hash->crc ^= a; for (int z = 0; z < 8; z++){ - if (crc & 1) crc = (crc >> 1) ^ 0xA001; - else crc = (crc >> 1); + if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001; + else hash->crc = (hash->crc >> 1); } } - return crc; } -uint8_t i_crc8(uint8_t *aa, size_t len){ - //crc8 maxim - uint8_t crc = 0x00; - +uint16_t crc16_final(struct crc16_hash *hash){ + return hash->crc; +} + +uint16_t crc16(uint8_t *aa, size_t len){ + struct crc16_hash a = crc16_init(); + crc16_update(aa, len, &a); + return crc16_final(&a); +} + +struct crc8_hash crc8_init(){ + return (struct crc8_hash){.crc = 0x00}; +} + +void crc8_update(uint8_t *aa, size_t len, struct crc8_hash *hash){ for(int i = 0; i != len; i++){ uint8_t a = aa[i]; for (int z = 0; z < 8; z++){ - uint8_t b = (crc ^ a) & 1; - crc >>= 1; - if(b) crc ^= 0x8c; + uint8_t b = (hash->crc ^ a) & 1; + hash->crc >>= 1; + if(b) hash->crc ^= 0x8c; a >>=1; } } - return crc; +} + +uint8_t crc8_final(struct crc8_hash *hash){ + return hash->crc; +} + +uint8_t crc8(uint8_t *aa, size_t len){ + struct crc8_hash a = crc8_init(); + crc8_update(aa, len, &a); + return crc8_final(&a); +} + +common_hash_init_update(crc32); +common_hash_init_update(crc16); +common_hash_init_update(crc8); + +int l_crc8_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, -1); + uint32_t u = crc8_final(a); + char digest[8]; + sprintf(digest,"%x",u); + lua_pushstring(L, digest); + return 1; +} + +int l_crc16_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, -1); + uint32_t u = crc16_final(a); + char digest[16]; + sprintf(digest,"%04x",u); + lua_pushstring(L, digest); + return 1; +} + +int l_crc32_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, -1); + uint32_t u = crc32_final(a); + char digest[8]; + sprintf(digest,"%08lx",u); + lua_pushstring(L, digest); + return 1; } int l_crc8(lua_State* L){ - + if(lua_gettop(L) == 0) return l_crc8_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[8]; - uint8_t u = i_crc8(a, len); + uint8_t u = crc8(a, len); sprintf(digest,"%x",u); lua_pushstring(L, digest); @@ -65,13 +137,13 @@ int l_crc8(lua_State* L){ } int l_crc16(lua_State* L){ - + if(lua_gettop(L) == 0) return l_crc16_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[16]; - uint16_t u = i_crc16(a, len); + uint16_t u = crc16(a, len); sprintf(digest,"%x",u); lua_pushstring(L, digest); @@ -79,13 +151,13 @@ int l_crc16(lua_State* L){ } int l_crc32(lua_State* L){ - + if(lua_gettop(L) == 0) return l_crc32_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[32]; - uint32_t u = i_crc32(a, len); + uint32_t u = crc32(a, len); sprintf(digest,"%x",u); lua_pushstring(L, digest); diff --git a/src/hash/crc.h b/src/hash/crc.h index de61efd..b3fdbf9 100644 --- a/src/hash/crc.h +++ b/src/hash/crc.h @@ -1,33 +1,44 @@ #include "../lua.h" #include <stdint.h> -/** - * calculates a crc of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint8_t} 8 bit checksum -*/ -uint8_t i_crc8(uint8_t*, size_t); - -/** - * calculates a crc of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint16_t} 16 bit checksum -*/ -uint16_t i_crc16(uint8_t*, size_t); - -/** - * calculates a crc of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint32_t} 32 bit checksum -*/ -uint32_t i_crc32(uint8_t*, size_t); +struct crc32_hash { + uint32_t crc; +}; + +struct crc16_hash { + uint16_t crc; +}; + +struct crc8_hash { + uint8_t crc; +}; + +uint8_t crc8(uint8_t*, size_t len); +struct crc8_hash crc8_init(); +void crc8_update(uint8_t*, size_t, struct crc8_hash*); +uint8_t crc8_final(struct crc8_hash*); + +uint16_t crc16(uint8_t*, size_t len); +struct crc16_hash crc16_init(); +void crc16_update(uint8_t*, size_t, struct crc16_hash*); +uint16_t crc16_final(struct crc16_hash*); + +uint32_t crc32(uint8_t*, size_t len); +struct crc32_hash crc32_init(); +void crc32_update(uint8_t*, size_t, struct crc32_hash*); +uint32_t crc32_final(struct crc32_hash*); int l_crc8(lua_State*); +int l_crc8_init(lua_State*); +int l_crc8_update(lua_State*); +int l_crc8_final(lua_State*); + int l_crc16(lua_State*); +int l_crc16_init(lua_State*); +int l_crc16_update(lua_State*); +int l_crc16_final(lua_State*); + int l_crc32(lua_State*); +int l_crc32_init(lua_State*); +int l_crc32_update(lua_State*); +int l_crc32_final(lua_State*); |
