From 05ae1fc2fef778138bb3a136340dd923ca9687a0 Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 8 Mar 2024 14:14:39 -0600 Subject: fixed hashes and net stuff --- src/hash/cityhash.c | 4 +-- src/hash/farmhash.c | 2 +- src/hash/fasthash.c | 2 +- src/hash/fnv.c | 6 ++-- src/hash/fnv.h | 3 ++ src/hash/metrohash.c | 8 ++--- src/hash/sdbm.c | 2 +- src/hash/sha2-256.c | 6 ++-- src/hash/xxh.c | 2 +- src/net.c | 56 +++++++++++++++++------------ src/types/map.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/types/map.h | 29 +++++++++++++++ src/types/parray.h | 1 + tests/hash.lua | 2 +- tests/net.lua | 11 ++++-- 15 files changed, 191 insertions(+), 42 deletions(-) create mode 100644 src/types/map.c create mode 100644 src/types/map.h diff --git a/src/hash/cityhash.c b/src/hash/cityhash.c index 46bb1ed..f32453d 100644 --- a/src/hash/cityhash.c +++ b/src/hash/cityhash.c @@ -411,7 +411,7 @@ int l_cityhash64(lua_State* L){ char digest[64]; uint64_t u = cityhash64(a, len); - sprintf(digest,"%016lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } @@ -424,7 +424,7 @@ int l_cityhash128(lua_State* L){ uint64_t u1, u2; cityhash128(a, len, &u1, &u2); - sprintf(digest,"%08lx%08lx",u1, u2); + sprintf(digest,"%08llx%08llx",u1, u2); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/farmhash.c b/src/hash/farmhash.c index 6533eb4..95047ee 100644 --- a/src/hash/farmhash.c +++ b/src/hash/farmhash.c @@ -163,7 +163,7 @@ int l_farmhash64(lua_State* L){ char digest[64]; uint64_t u = farmhash64(a, len); - sprintf(digest,"%08lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/fasthash.c b/src/hash/fasthash.c index 0ac2cc0..993a22a 100644 --- a/src/hash/fasthash.c +++ b/src/hash/fasthash.c @@ -62,7 +62,7 @@ int l_fasthash64(lua_State* L){ char digest[64]; uint64_t u = fasthash64(a, len, seed); - sprintf(digest,"%08lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/fnv.c b/src/hash/fnv.c index ac1f88b..9d5c646 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -32,7 +32,7 @@ int l_fnv_0(lua_State* L){ char digest[64]; uint64_t u = fnv_1(a, len, v_0); - sprintf(digest,"%08lx",u); + sprintf(digest,"%16llx",u); lua_pushstring(L, digest); return 1; } @@ -44,7 +44,7 @@ int l_fnv_1(lua_State* L){ char digest[64]; uint64_t u = fnv_1(a, len, v_1); - sprintf(digest,"%08lx",u); + sprintf(digest,"%16llx",u); lua_pushstring(L, digest); return 1; } @@ -56,7 +56,7 @@ int l_fnv_a(lua_State* L){ char digest[64]; uint64_t u = fnv_1(a, len, v_a); - sprintf(digest,"%08lx",u); + sprintf(digest,"%16llx",u); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/fnv.h b/src/hash/fnv.h index 029abcc..74f1722 100644 --- a/src/hash/fnv.h +++ b/src/hash/fnv.h @@ -1,9 +1,12 @@ #include "../lua.h" +#include "stdint.h" enum fnv_version { v_1, v_a, v_0 }; +uint64_t fnv_1(uint8_t*, size_t, enum fnv_version); + int l_fnv_1(lua_State*); int l_fnv_a(lua_State*); int l_fnv_0(lua_State*); diff --git a/src/hash/metrohash.c b/src/hash/metrohash.c index 9ce08b3..4e59f81 100644 --- a/src/hash/metrohash.c +++ b/src/hash/metrohash.c @@ -174,7 +174,7 @@ int l_metrohash64_v1(lua_State* L){ char digest[64]; uint64_t u = metrohash64(a, len, seed, v1); - sprintf(digest,"%016lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } @@ -189,7 +189,7 @@ int l_metrohash64_v2(lua_State* L){ char digest[64]; uint64_t u = metrohash64(a, len, seed, v2); - sprintf(digest,"%016lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } @@ -205,7 +205,7 @@ int l_metrohash128_v1(lua_State* L){ uint64_t u1, u2; metrohash128(a, len, seed, &u1, &u2, v1); - sprintf(digest,"%016lx%016lx",u1,u2); + sprintf(digest,"%016llx%016llx",u1,u2); lua_pushstring(L, digest); return 1; } @@ -221,7 +221,7 @@ int l_metrohash128_v2(lua_State* L){ uint64_t u1, u2; metrohash128(a, len, seed, &u1, &u2, v2); - sprintf(digest,"%016lx%016lx",u1,u2); + sprintf(digest,"%016llx%016llx",u1,u2); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c index d913d01..0c10289 100644 --- a/src/hash/sdbm.c +++ b/src/hash/sdbm.c @@ -21,7 +21,7 @@ int l_sdbm(lua_State* L){ char digest[64]; uint64_t u = sdbm(a, len); - sprintf(digest,"%016lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; } diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index e343825..d0c03f9 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -160,20 +160,20 @@ void sha2_512_t(uint8_t* out, uint8_t* in, int t){ if(t%8!=0) return; uint64_t out_stream[8] = {0}; sha512_gen(out_stream, in, sha_iv_gen(t)); - for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016lx", out, out_stream[i]); + for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); out[t/4] = '\0'; } void sha2_512(uint8_t* out, uint8_t* in){ uint64_t out_stream[8] = {0}; sha512_gen(out_stream, in, sha512_iv); - for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016lx", out, out_stream[i]); + for(int i = 0; i != 8; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); } void sha2_384(uint8_t* out, uint8_t* in){ uint64_t out_stream[8] = {0}; sha512_gen(out_stream, in, sha384_iv); - for(int i = 0; i != 6; i++) sprintf((char*)out, "%s%016lx", out, out_stream[i]); + for(int i = 0; i != 6; i++) sprintf((char*)out, "%s%016llx", out, out_stream[i]); } int l_sha512(lua_State* L){ diff --git a/src/hash/xxh.c b/src/hash/xxh.c index 74cb202..deb9709 100644 --- a/src/hash/xxh.c +++ b/src/hash/xxh.c @@ -129,7 +129,7 @@ int l_xxh64(lua_State* L){ char digest[64]; uint64_t u = i_xxhash64(a, seed, len); - sprintf(digest,"%lx",u); + sprintf(digest,"%016llx",u); lua_pushstring(L, digest); return 1; diff --git a/src/net.c b/src/net.c index 8d22121..4f5e2b5 100644 --- a/src/net.c +++ b/src/net.c @@ -30,7 +30,7 @@ //2^42 #define BUFFER_SIZE 1//20000 #define HTTP_BUFFER_SIZE 4098 -#define max_content_length 200000 +#define max_content_length 20 static int ports[65535] = { 0 }; static parray_t* paths = NULL; @@ -394,14 +394,14 @@ int l_send(lua_State* L){ i_write_header(L, header, &resp, "", 0); } else i_write_header(L, header, &resp, content, len); - //printf("sent%i\n",send(client_fd, resp->c, resp->len, 0)); - ; + int a = send(client_fd, resp->c, resp->len, 0); - lua_pushstring(L, "client_fd"); - lua_pushinteger(L, -1); - lua_settable(L, res_idx); - closesocket(client_fd); - + // + //lua_pushstring(L, "client_fd"); + //lua_pushinteger(L, -1); + //lua_settable(L, res_idx); + //sclosesocket(client_fd); + //printf("%i | %i\n'%s'\n%i\n",client_fd,a,resp->c,resp->len); str_free(resp); return 0; } @@ -536,10 +536,10 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer blen-=i+2; str_clear(current); + + *table_idx = lua_rawlen(L, *files_idx) + 1; + lua_pushinteger(L, *table_idx); lua_newtable(L); - *table_idx = lua_gettop(L); - lua_pushinteger(L, lua_rawlen(L, *files_idx) + 1); - lua_pushvalue(L, *table_idx); lua_settable(L, *files_idx); break; } @@ -547,6 +547,12 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer buffer++; } } + + lua_pushvalue(L, *files_idx); + lua_pushinteger(L, *table_idx); + lua_gettable(L, -2); + int rfiles_idx = lua_gettop(L); + if(*status == FILE_HEADER){ for(int i = 0; i < blen; i++){ @@ -563,15 +569,16 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer current = str_init(""); break; } - luaI_tsets(L, *table_idx , old->c, current->c); - //printf("'%s' : '%s'\n",old->c, current->c); + //printf("%i '%s' : '%s'\n",*table_idx, old->c, current->c); + + luaI_tsets(L, rfiles_idx, old->c, current->c); + old = NULL; str_clear(current); } else if(buffer[i] != '\r' && !(buffer[i] == ' ' && current->len == 0)) str_pushl(current, buffer + i, 1); } } if(*status == FILE_BODY){ - if(old==NULL) old = str_init(""); for(int i = 0; i < blen; i++){ @@ -581,12 +588,12 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer if(buffer[i] == '-') (*dash_count)++; if(old->len - *dash_count >= boundary->len){ - luaI_tsets(L, *table_idx, "content", current->c); - + + luaI_tsets(L, rfiles_idx, "content", current->c); /*lua_pushinteger(L, lua_rawlen(L, *files_idx) + 1); lua_pushvalue(L, *table_idx); lua_settable(L, *files_idx);*/ - + for(; i < blen; i++) if(buffer[i] == '\n') break; str_clear(current); *status = BARRIER_READ; buffer+=i; @@ -609,7 +616,6 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer } } - parray_set(content, "_dash_count", dash_count); parray_set(content, "_boundary_id", boundary_id); parray_set(content, "_boundary", boundary); @@ -623,7 +629,6 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer } int l_roll(lua_State* L){ - lua_pushvalue(L, 1); lua_pushstring(L, "_data"); lua_gettable(L, -2); @@ -661,6 +666,7 @@ int l_roll(lua_State* L){ int files_idx = lua_gettop(L); rolling_file_parse(L, &files_idx, &body_idx, buffer, NULL, r, &data); + luaI_tsetv(L, 1, "Body", body_idx); luaI_tsetv(L, 1, "files", files_idx); @@ -865,7 +871,7 @@ void* handle_client(void *_arg){ } - /*void* awa; + void* awa; if((awa = parray_get(file_cont, "_current")) != NULL) str_free(awa); if((awa = parray_get(file_cont, "_boundary")) != NULL) str_free(awa); if((awa = parray_get(file_cont, "_boundary_id")) != NULL) str_free(awa); @@ -873,13 +879,15 @@ void* handle_client(void *_arg){ if((awa = parray_get(file_cont, "_status")) != NULL) free(awa); if((awa = parray_get(file_cont, "_dash_count")) != NULL) free(awa); - parray_clear(file_cont, NONE);*/ + parray_clear(file_cont, NONE); } //printf("end anyways\n"); parray_clear(table, STR); } - if(client_fd > 0)closesocket(client_fd); + shutdown(client_fd, 2); + close(client_fd); + free(args); free(buffer); lua_close(L); @@ -932,6 +940,7 @@ int start_serv(lua_State* L, int port){ //open a state to call shit, should be somewhat thread safe thread_arg_struct* args = malloc(sizeof * args); + args->fd = *client_fd; args->port = port; args->cli = client_addr; @@ -945,7 +954,8 @@ int start_serv(lua_State* L, int port){ pthread_t thread_id; pthread_create(&thread_id, NULL, handle_client, (void*)args); pthread_detach(thread_id); - + + //handle_client((void*)args); free(client_fd); } diff --git a/src/types/map.c b/src/types/map.c new file mode 100644 index 0000000..b5afc1c --- /dev/null +++ b/src/types/map.c @@ -0,0 +1,99 @@ +#include "map.h" +#include "../hash/fnv.h" +#include +#include +#include + +#define mod_inc 4 + +uint64_t hash(char* c, size_t len){ + return fnv_1((uint8_t*)c, len, v_a); +} + +map_t* map_init(){ + map_t* awa = malloc(sizeof * awa); + awa->M = malloc(sizeof * awa->M * 4); + awa->len = 0; + awa->mod = 4; + return awa; +} + +void map_regraph(map_t* M){ + map_t* remade = map_init(); + for(int i = 0; i != M->len; i++){ + //what happens if the map_set calls map_regraph??? idk + map_set(remade, M->M[i].key->c, M->M[i].value); + } + M = remade; +} + +void map_set(map_t* M, char* key, void* value){ + uint64_t h = hash(key, strlen(key)); + if(M->len >= M->mod){ + expand: + M->mod *= 4; + M->M = realloc(M->M, sizeof * M->M * M->mod); + //regraph it + map_regraph(M); + } + uint64_t ind = h % M->mod; + + for(int count = 0; M->M[ind].key != NULL && M->M[ind].hash != h && strcmp(M->M[ind].key->c, key) != 0; count++){ + ind++; + if(ind >= M->mod) ind = 0; + if(count > 5) goto expand; + } + + M->M[ind].hash = h; + M->M[ind].key = str_init(key); + M->M[ind].value = value; +} + +int map_geti(map_t* M, char* key){ + uint64_t h = hash(key, strlen(key)); + uint64_t ind = h % M->mod; + //melem_t sel = M->M[]; + + for(uint64_t initial = ind; ind != initial - 1;){ + if(M->M[ind].key == NULL) return -1; + if(M->M[ind].hash == h && strcmp(M->M[ind].key->c, key)==0) return ind; + ind++; + if(ind >= M->mod) ind = 0; + } + return -1; +} + +void* map_get(map_t* M, char* key){ + int r = map_geti(M, key); + printf("%i\n",r); + return r == -1? NULL : M->M[r].value; +} + +void map_remove(map_t* p, char* key, enum free_type free); +void map_clear(map_t*, enum free_type); +void map_lclear(map_t*); + +void map_dump(map_t* M){ + for(int i = 0; i != M->mod; i++){ + if(M->M[i].key != NULL){ + printf("%i | %s : %p\n",i,M->M[i].key->c, M->M[i].value); + } + } +} +int main(){ + int i = 5; + int b = 24; + int c = 9; + map_t* m = map_init(); + for(int i = 65; i != 91; i++){ + //printf("%c\n",i); + int* ww = malloc(sizeof * ww * 55); + ww[0] = i; + map_set(m, ((char[]){i, 0}), ww); + } + map_dump(m); + + printf("%i\n",*(int*)map_get(m, "B")); + + return 0; +} \ No newline at end of file diff --git a/src/types/map.h b/src/types/map.h new file mode 100644 index 0000000..cb8fed2 --- /dev/null +++ b/src/types/map.h @@ -0,0 +1,29 @@ + +#ifndef _MAP_H +#define _MAP_H + +#include +#include "str.h" +#include "parray.h" + +typedef struct { + void* value; + str* key; + uint64_t hash; +} melem_t; + +typedef struct { + melem_t* M; + int len; + int mod; +} map_t; + +map_t* map_init(); +void map_set(map_t*, char*, void*); +void* map_get(map_t* , char*); +int map_geti(map_t* , char*); +void map_remove(map_t* p, char* key, enum free_type free); +void map_clear(map_t*, enum free_type); +void map_lclear(map_t*); + +#endif //_MAP_H \ No newline at end of file diff --git a/src/types/parray.h b/src/types/parray.h index 306ade3..bedadbf 100644 --- a/src/types/parray.h +++ b/src/types/parray.h @@ -24,5 +24,6 @@ void parray_remove(parray_t* p, char* key, enum free_type free); void parray_clear(parray_t*, enum free_type); void parray_lclear(parray_t*); parray_t* parray_find(parray_t*, char*); +void free_method(void*, enum free_type); #endif //parray_h \ No newline at end of file diff --git a/tests/hash.lua b/tests/hash.lua index 76cb4a5..b4e455a 100644 --- a/tests/hash.lua +++ b/tests/hash.lua @@ -3,7 +3,7 @@ require "llib" function test(name,b,exp) local hash = llib.crypto[name](b) if not (hash == exp) then - llib.io.error(name.." not working, got "..hash.." wanted "..exp) + llib.io.error(name.." not working, got \n\t"..hash.." wanted\n\t"..exp) else llib.io.log(name.." was correct, "..hash) end diff --git a/tests/net.lua b/tests/net.lua index 3e88f83..80a2315 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -46,9 +46,16 @@ llib.net.listen( --_G.llib.io.pprint(_G.llib.crypto.md5(_G.llib.io.readfile(".gitignore"))) --_G.llib.io.pprint(req) --_G.llib.io.pprint(req) - --req:roll(2000) + a = req:roll(100000) + --print(a) + while a > 0 do + a = req:roll(100000) + --print(a) + end --_G.llib.io.pprint(req) - --res:send("owoowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaowoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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