aboutsummaryrefslogtreecommitdiff
path: root/src/hash
diff options
context:
space:
mode:
authorame <[email protected]>2024-04-04 10:06:53 -0500
committerame <[email protected]>2024-04-04 10:06:53 -0500
commitdae9e34168b56a399b2b1e04e657e322b9c6f803 (patch)
tree174eef159441750b71e720d6639b9b221502fd1c /src/hash
parent35c37ef1221d5860731435137059dc4533adff42 (diff)
all basic hash funcs
Diffstat (limited to 'src/hash')
-rw-r--r--src/hash/loselose.c37
-rw-r--r--src/hash/loselose.h12
-rw-r--r--src/hash/pearson.c39
-rw-r--r--src/hash/pearson.h20
-rw-r--r--src/hash/pjw.c52
-rw-r--r--src/hash/pjw.h11
-rw-r--r--src/hash/sdbm.c37
-rw-r--r--src/hash/sdbm.h11
-rw-r--r--src/hash/sysvchecksum.c45
-rw-r--r--src/hash/sysvchecksum.h19
-rw-r--r--src/hash/xor.c40
-rw-r--r--src/hash/xor.h18
12 files changed, 266 insertions, 75 deletions
diff --git a/src/hash/loselose.c b/src/hash/loselose.c
index f3350f5..55d3fc6 100644
--- a/src/hash/loselose.c
+++ b/src/hash/loselose.c
@@ -3,18 +3,43 @@
#include <stdio.h>
#include <stdint.h>
+struct loselose_hash loselose_init(){
+ return (struct loselose_hash){.hash = 0};
+}
+
+void loselose_update(uint8_t* in, size_t len, struct loselose_hash* hash){
+ for(int i = 0; i != len; i++){
+ hash->hash += (uint64_t)*in;
+ in++;
+ }
+}
+
+uint64_t loselose_final(struct loselose_hash* hash){
+ return hash->hash;
+}
+
uint64_t loselose(uint8_t* in, size_t len){
- uint64_t hash = 0;
+ struct loselose_hash a = loselose_init();
+ loselose_update(in, len, &a);
+ return loselose_final(&a);
+}
+
+common_hash_init_update(loselose);
- for(int i = 0; i != len; i++){
- hash += (uint64_t)*in;
- in++;
- }
+int l_loselose_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
- return hash;
+ struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, -1);
+ uint64_t u = loselose_final(a);
+ char digest[64];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_loselose(lua_State* L){
+ if(lua_gettop(L) == 0) return l_loselose_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/loselose.h b/src/hash/loselose.h
index 434b8cb..8ce1ed7 100644
--- a/src/hash/loselose.h
+++ b/src/hash/loselose.h
@@ -1,5 +1,17 @@
#include "../lua.h"
#include <stdint.h>
+struct loselose_hash {
+ uint64_t hash;
+};
+
+struct loselose_hash loselose_init();
+void loselose_update(uint8_t*, size_t, struct loselose_hash*);
+uint64_t loselose_final(struct loselose_hash*);
+
uint64_t loselose(uint8_t* in, size_t len);
+
int l_loselose(lua_State*);
+int l_loselose_init(lua_State*);
+int l_loselose_update(lua_State*);
+int l_loselose_final(lua_State*);
diff --git a/src/hash/pearson.c b/src/hash/pearson.c
index 4e1ea0a..1110891 100644
--- a/src/hash/pearson.c
+++ b/src/hash/pearson.c
@@ -19,12 +19,23 @@ static uint8_t pearson_table[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_pearson(uint8_t *aa, size_t len){
- uint8_t ret = 0;
+struct pearson_hash pearson_init(){
+ return (struct pearson_hash){.ret = 0};
+}
+
+void pearson_update(uint8_t* aa, size_t len, struct pearson_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->ret = pearson_table[(uint8_t)(hash->ret^aa[i])];
+}
- for(int i = 0; i != len; i++) ret = pearson_table[(uint8_t)(ret^aa[i])];
+uint8_t pearson_final(struct pearson_hash* hash){
+ return hash->ret;
+}
- return ret;
+uint8_t pearson(uint8_t* aa, size_t len){
+ struct pearson_hash a = pearson_init();
+ pearson_update(aa, len, &a);
+ return pearson_final(&a);
}
int l_setpearson(lua_State* L){
@@ -48,13 +59,27 @@ int l_setpearson(lua_State* L){
return 0;
}
+common_hash_init_update(pearson);
+
+int l_pearson_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct pearson_hash* a = (struct pearson_hash*)lua_touserdata(L, -1);
+ uint8_t u = pearson_final(a);
+ char digest[8];
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
int l_pearson(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_pearson_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[3];
- uint8_t u = i_pearson(a, len);
+ char digest[8];
+ uint8_t u = pearson(a, len);
sprintf(digest,"%x",u);
diff --git a/src/hash/pearson.h b/src/hash/pearson.h
index 646e6dd..4cdfda9 100644
--- a/src/hash/pearson.h
+++ b/src/hash/pearson.h
@@ -1,13 +1,17 @@
#include "../lua.h"
#include <stdint.h>
-/**
- * calculates a pearson hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint8_t} 8 bit hash
-*/
-uint8_t i_pearson(uint8_t*,size_t);
+
+struct pearson_hash {
+ uint8_t ret;
+};
+
+struct pearson_hash pearson_init();
+void pearson_update(uint8_t*, size_t, struct pearson_hash* hash);
+uint8_t pearson_final(struct pearson_hash* hash);
+uint8_t pearson(uint8_t*,size_t);
int l_setpearson(lua_State* L);
int l_pearson(lua_State* L);
+int l_pearson_init(lua_State* L);
+int l_pearson_update(lua_State* L);
+int l_pearson_final(lua_State* L);
diff --git a/src/hash/pjw.c b/src/hash/pjw.c
index 50593b9..5dea872 100644
--- a/src/hash/pjw.c
+++ b/src/hash/pjw.c
@@ -4,28 +4,52 @@
#include <stdio.h>
#include <stdint.h>
-uint32_t pjw(uint8_t* in, size_t len){
- uint32_t hash = 0;
- uint32_t high;
-
+struct pjw_hash pjw_init(){
+ return (struct pjw_hash){.hash = 0, .high = 0};
+}
+
+void pjw_update(uint8_t* in, size_t len, struct pjw_hash* hash){
for(int i = 0; i != len; i++){
- hash = (hash << 4) + *in++;
- if((high = (hash & 0xf0000000)))
- hash ^= high >> 24;
- hash &= ~high;
+ hash->hash = (hash->hash << 4) + *in++;
+ if((hash->high = (hash->hash & 0xf0000000)))
+ hash->hash ^= hash->high >> 24;
+ hash->hash &= ~hash->high;
}
+}
- return hash;
+uint32_t pjw_final(struct pjw_hash* hash){
+ return hash->hash;
}
-int l_pjw(lua_State* L){
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+uint32_t pjw(uint8_t* in, size_t len){
+ struct pjw_hash a = pjw_init();
+ pjw_update(in, len, &a);
+ return pjw_final(&a);
+}
- char digest[32];
+common_hash_init_update(pjw);
- uint32_t u = pjw(a, len);
+int l_pjw_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct pjw_hash* a = (struct pjw_hash*)lua_touserdata(L, -1);
+ uint32_t u = pjw_final(a);
+ char digest[32];
sprintf(digest,"%08x",u);
lua_pushstring(L, digest);
return 1;
}
+
+int l_pjw(lua_State* L){
+ if(lua_gettop(L) == 0) return l_pjw_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[32];
+
+ uint32_t u = pjw(a, len);
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/pjw.h b/src/hash/pjw.h
index f1ab910..a4e36e7 100644
--- a/src/hash/pjw.h
+++ b/src/hash/pjw.h
@@ -1,5 +1,16 @@
#include "../lua.h"
#include <stdint.h>
+struct pjw_hash {
+ uint32_t hash, high;
+};
+
+struct pjw_hash pjw_init();
+void pjw_update(uint8_t*, size_t, struct pjw_hash*);
+uint32_t pjw_final(struct pjw_hash*);
uint32_t pjw(uint8_t* in, size_t len);
+
int l_pjw(lua_State*);
+int l_pjw_init(lua_State*);
+int l_pjw_update(lua_State*);
+int l_pjw_final(lua_State*);
diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c
index b32a627..bc5f0d9 100644
--- a/src/hash/sdbm.c
+++ b/src/hash/sdbm.c
@@ -2,18 +2,43 @@
#include <stdio.h>
#include <stdint.h>
+struct sdbm_hash sdbm_init(){
+ return (struct sdbm_hash){.hash = 0};
+}
+
+void sdbm_update(uint8_t* in, size_t len, struct sdbm_hash* hash){
+ for(int i = 0; i != len; i++){
+ hash->hash = (uint64_t)*in + (hash->hash << 6) + (hash->hash << 16) - hash->hash;
+ in++;
+ }
+}
+
+uint64_t sdbm_final(struct sdbm_hash* hash){
+ return hash->hash;
+}
+
uint64_t sdbm(uint8_t* in, size_t len){
- uint64_t hash = 0;
+ struct sdbm_hash a = sdbm_init();
+ sdbm_update(in, len, &a);
+ return sdbm_final(&a);
+}
+
+common_hash_init_update(sdbm);
- for(int i = 0; i != len; i++){
- hash = (uint64_t)*in + (hash << 6) + (hash << 16) - hash;
- in++;
- }
+int l_sdbm_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
- return hash;
+ struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, -1);
+ uint64_t u = sdbm_final(a);
+ char digest[64];
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_sdbm(lua_State* L){
+ if(lua_gettop(L) == 0) return l_sdbm_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/sdbm.h b/src/hash/sdbm.h
index 797cc22..a937c92 100644
--- a/src/hash/sdbm.h
+++ b/src/hash/sdbm.h
@@ -1,5 +1,16 @@
#include "../lua.h"
#include <stdint.h>
+struct sdbm_hash {
+ uint64_t hash;
+};
+
+struct sdbm_hash sdbm_init();
+void sdbm_update(uint8_t*, size_t len, struct sdbm_hash*);
+uint64_t sdbm_final(struct sdbm_hash*);
uint64_t sdbm(uint8_t* in, size_t len);
+
int l_sdbm(lua_State*);
+int l_sdbm_init(lua_State*);
+int l_sdbm_update(lua_State*);
+int l_sdbm_final(lua_State*);
diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c
index a4ce824..d22270d 100644
--- a/src/hash/sysvchecksum.c
+++ b/src/hash/sysvchecksum.c
@@ -3,25 +3,48 @@
#include <stdint.h>
#include <math.h>
-uint32_t i_sysvchecksum(uint8_t *aa, size_t len){
- uint32_t check = 0x0;
-
- for(int i = 0; i != len; i++){
- check += aa[i];
- }
-
- uint32_t r = check % (int)pow(2,16) + (check % (int)pow(2,32)) / (int)pow(2,16);
- return (r % (int)pow(2,16)) + r / (int)pow(2,16);
+struct sysvchecksum_hash sysvchecksum_init(){
+ return (struct sysvchecksum_hash){.check = 0};
}
+void sysvchecksum_update(uint8_t* aa, size_t len, struct sysvchecksum_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->check += aa[i];
+}
+
+uint32_t sysvchecksum_final(struct sysvchecksum_hash* hash){
+ uint32_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16);
+ return (r % (int)pow(2,16)) + r / (int)pow(2,16);
+}
+
+uint32_t sysvchecksum(uint8_t* aa, size_t len){
+ struct sysvchecksum_hash a = sysvchecksum_init();
+ sysvchecksum_update(aa, len, &a);
+ return sysvchecksum_final(&a);
+}
+
+common_hash_init_update(sysvchecksum);
+
+int l_sysvchecksum_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, -1);
+ uint32_t u = sysvchecksum_final(a);
+ char digest[32];
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
int l_sysvchecksum(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_sysvchecksum_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[32];
- uint32_t u = i_sysvchecksum(a, len);
+ uint32_t u = sysvchecksum(a, len);
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/sysvchecksum.h b/src/hash/sysvchecksum.h
index e45cff8..3414f7f 100644
--- a/src/hash/sysvchecksum.h
+++ b/src/hash/sysvchecksum.h
@@ -1,13 +1,16 @@
#include "../lua.h"
#include <stdint.h>
-/**
- * calculates a sysv checksum of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint32_t} 32 bit checksum
-*/
-uint32_t i_sysvchecksum(uint8_t*, size_t);
+struct sysvchecksum_hash {
+ uint32_t check;
+};
+
+struct sysvchecksum_hash sysvchecksum_init();
+void sysvchecksum_update(uint8_t*, size_t, struct sysvchecksum_hash*);
+uint32_t sysvchecksum_final(struct sysvchecksum_hash*);
+uint32_t sysvchecksum(uint8_t*, size_t);
int l_sysvchecksum(lua_State*);
+int l_sysvchecksum_init(lua_State*);
+int l_sysvchecksum_update(lua_State*);
+int l_sysvchecksum_final(lua_State*);
diff --git a/src/hash/xor.c b/src/hash/xor.c
index 5fe23b7..1785414 100644
--- a/src/hash/xor.c
+++ b/src/hash/xor.c
@@ -2,23 +2,47 @@
#include <stdio.h>
#include <stdint.h>
-uint8_t i_xor8(uint8_t *aa, size_t len){
- uint8_t a = 0;
+struct xor8_hash xor8_init(){
+ return (struct xor8_hash){.a = 0};
+}
+
+void xor8_update(uint8_t* aa, size_t len, struct xor8_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->a += aa[i] & 0xff;
+}
+
+uint8_t xor8_final(struct xor8_hash* hash){
+ return ((hash->a ^ 0xff) + 1) & 0xff;
+}
+
+uint8_t xor8(uint8_t* aa, size_t len){
+ struct xor8_hash a = xor8_init();
+ xor8_update(aa, len, &a);
+ return xor8_final(&a);
+}
- for(int i = 0; i != len; i++)
- a += aa[i] & 0xff;
-
- return ((a ^ 0xff) + 1) & 0xff;
+common_hash_init_update(xor8);
+
+int l_xor8_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct xor8_hash* a = (struct xor8_hash*)lua_touserdata(L, -1);
+ uint8_t u = xor8_final(a);
+ char digest[8];
+ sprintf(digest,"%02x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_xor8(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_xor8_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[8];
- uint8_t u = i_xor8(a, len);
+ uint8_t u = xor8(a, len);
sprintf(digest,"%02x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/xor.h b/src/hash/xor.h
index bd9aaed..6065524 100644
--- a/src/hash/xor.h
+++ b/src/hash/xor.h
@@ -1,12 +1,16 @@
#include "../lua.h"
#include <stdint.h>
-/**
- * calculates a xor hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint8_t} 8 bit checksum
-*/
+
+struct xor8_hash {
+ uint8_t a;
+};
+
+struct xor8_hash xor8_init();
+void xor8_update(uint8_t*, size_t, struct xor8_hash*);
+uint8_t xor8_final(struct xor8_hash*);
uint8_t xor8(uint8_t*, size_t);
int l_xor8(lua_State*);
+int l_xor8_init(lua_State*);
+int l_xor8_update(lua_State*);
+int l_xor8_final(lua_State*); \ No newline at end of file