diff options
62 files changed, 2420 insertions, 2418 deletions
diff --git a/src/config.c b/src/config.c index 60d4079..3b6cd56 100644 --- a/src/config.c +++ b/src/config.c @@ -83,7 +83,7 @@ int m_config_newindex(lua_State* L){ int i_config_metatable(lua_State* L, struct config* conf){ int idx = lua_gettop(L); luaI_tsetlud(L, idx, "_config", conf); - + lua_newtable(L); int meta_idx = lua_gettop(L); luaI_tsetcf(L, meta_idx, "__index", m_config_index); diff --git a/src/crypto.c b/src/crypto.c index 7556b6f..1cab822 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -1,25 +1,25 @@ #include "crypto.h"
uint8_t rotl8(uint8_t y, uint8_t offset){
- return ( y << offset ) | ( y >> (8 - offset));
+ return ( y << offset ) | ( y >> (8 - offset));
}
uint16_t rotl16(uint16_t y, uint16_t offset){
- return ( y << offset ) | ( y >> (16 - offset));
+ return ( y << offset ) | ( y >> (16 - offset));
}
unsigned rotl32(unsigned y, unsigned offset){
- return ( y << offset ) | ( y >> (32 - offset));
+ return ( y << offset ) | ( y >> (32 - offset));
}
unsigned rotr32(unsigned x, unsigned n) {
- return (x >> n % 32) | (x << (32-n) % 32);
+ return (x >> n % 32) | (x << (32-n) % 32);
}
uint64_t rotl64(uint64_t y, uint64_t offset){
- return ( y << offset ) | ( y >> (64 - offset));
+ return ( y << offset ) | ( y >> (64 - offset));
}
uint64_t rotr64(uint64_t x, uint64_t n) {
- return (x >> n) | (x << (64-n));
+ return (x >> n) | (x << (64-n));
}
diff --git a/src/crypto.h b/src/crypto.h index 9561008..0e28fc3 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -45,16 +45,16 @@ int tp(lua_State*); #define common_hash_clone(hashname) lua_common_hash_clone(hashname, hashname)
#define lua_common_hash_clone(hashname, luaname) lua_common_hash_clone_oargs(hashname, luaname, l_##luaname##_init(L), *b = *a)
#define lua_common_hash_clone_oargs(hashname, luaname, oinit, copy)\
-int l_##luaname##_clone(lua_State* L){\
- struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
- oinit;\
- struct hashname##_hash* b = (struct hashname##_hash*)lua_touserdata(L, -1);\
- copy;\
- return 1;\
-}
+ int l_##luaname##_clone(lua_State* L){\
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ oinit;\
+ struct hashname##_hash* b = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ copy;\
+ return 1;\
+ }
#define lua_common_hash_meta(luaname)\
-int _##luaname##_hash_add(lua_State*L){\
+ int _##luaname##_hash_add(lua_State*L){\
lua_pushvalue(L, 1);\
l_##luaname##_clone(L);\
lua_pushvalue(L, -1);\
@@ -62,28 +62,28 @@ int _##luaname##_hash_add(lua_State*L){\ return l_##luaname##_update(L);\
}\
int _##luaname##_common_hash(lua_State* L){\
- lua_newtable(L);\
- int ti = lua_gettop(L);\
- luaI_tsetcf(L, ti, "update", l_##luaname##_update);\
- luaI_tsetcf(L, ti, "final", l_##luaname##_final);\
- \
- lua_pushvalue(L, 2);\
- lua_gettable(L, ti);\
- return 1;\
- }
+ lua_newtable(L);\
+ int ti = lua_gettop(L);\
+ luaI_tsetcf(L, ti, "update", l_##luaname##_update);\
+ luaI_tsetcf(L, ti, "final", l_##luaname##_final);\
+ \
+ lua_pushvalue(L, 2);\
+ lua_gettable(L, ti);\
+ return 1;\
+}
#define lua_common_hash_meta_def(luaname, exitf)\
lua_newtable(L);\
- int mt = lua_gettop(L);\
- luaI_tsetcf(L, mt, "__index", _##luaname##_common_hash);\
- luaI_tsetcf(L, mt, "__add", _##luaname##_hash_add);\
- luaI_tsetcf(L, mt, "__gc", exitf);\
- lua_pushvalue(L, mt);\
- lua_setmetatable(L, ud);\
+int mt = lua_gettop(L);\
+luaI_tsetcf(L, mt, "__index", _##luaname##_common_hash);\
+luaI_tsetcf(L, mt, "__add", _##luaname##_hash_add);\
+luaI_tsetcf(L, mt, "__gc", exitf);\
+lua_pushvalue(L, mt);\
+lua_setmetatable(L, ud);\
#define lua_common_hash_init_ni(hashname, luaname, initf, exitf)\
- lua_common_hash_meta(luaname);\
- int l_##luaname##_init(lua_State* L){\
+ lua_common_hash_meta(luaname);\
+int l_##luaname##_init(lua_State* L){\
\
struct hashname##_hash* a = (struct hashname##_hash*)lua_newuserdata(L, sizeof * a);\
int ud = lua_gettop(L);\
@@ -94,70 +94,70 @@ int _##luaname##_common_hash(lua_State* L){\ }
#define lua_common_hash_update(hashname, luaname)\
-int l_##luaname##_update(lua_State* L){\
- struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -2);\
- size_t len = 0;\
- uint8_t* b = (uint8_t*)luaL_checklstring(L, -1, &len);\
- \
- hashname##_update(b, len, a);\
- \
- lua_pushvalue(L, -2);\
- return 1;\
-}
+ int l_##luaname##_update(lua_State* L){\
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -2);\
+ size_t len = 0;\
+ uint8_t* b = (uint8_t*)luaL_checklstring(L, -1, &len);\
+ \
+ hashname##_update(b, len, a);\
+ \
+ lua_pushvalue(L, -2);\
+ return 1;\
+ }
#define clean_lullaby_crypto luaI_nothing
static const luaL_Reg crypto_function_list [] = {
- {"setpearson",l_setpearson}, {"fletcher8",l_fletcher8},
- {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
- {"setbuzhash",l_setbuzhash},
- {"loselose", l_loselose},
- {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32},
-
- {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final},
- {"bsdchecksum",l_bsdchecksum}, {"bsdchecksum_init",l_bsdchecksum_init}, {"bsdchecksum_update",l_bsdchecksum_update}, {"bsdchecksum_final",l_bsdchecksum_final},
- {"buzhash8",l_buzhash8}, {"buzhash16",l_buzhash16},
- {"crc8",l_crc8}, {"crc8_init",l_crc8_init}, {"crc8_update",l_crc8_update}, {"crc8_final",l_crc8_final},
- {"crc16",l_crc16}, {"crc16_init",l_crc16_init}, {"crc16_update",l_crc16_update}, {"crc16_final",l_crc16_final},
- {"crc32",l_crc32}, {"crc32_init",l_crc32_init}, {"crc32_update",l_crc32_update}, {"crc32_final",l_crc32_final},
- {"djb2", l_djb2}, {"djb2_init", l_djb2_init}, {"djb2_update", l_djb2_update}, {"djb2_final", l_djb2_final},
- {"fletcher8",l_fletcher8}, {"fletcher8_init",l_fletcher8_init}, {"fletcher8_update",l_fletcher8_update}, {"fletcher8_final",l_fletcher8_final},
- {"fletcher16",l_fletcher16}, {"fletcher16_init",l_fletcher16_init}, {"fletcher16_update",l_fletcher16_update}, {"fletcher16_final",l_fletcher16_final},
- {"fletcher32",l_fletcher32}, {"fletcher32_init",l_fletcher32_init}, {"fletcher32_update",l_fletcher32_update}, {"fletcher32_final",l_fletcher32_final},
- {"fnv_0", l_fnv_0}, {"fnv_0_init", l_fnv_0_init}, {"fnv_0_update", l_fnv_0_update}, {"fnv_0_final", l_fnv_0_final},
- {"fnv_1", l_fnv_1}, {"fnv_1_init", l_fnv_1_init}, {"fnv_1_update", l_fnv_1_update}, {"fnv_1_final", l_fnv_1_final},
- {"fnv_a", l_fnv_a}, {"fnv_a_init", l_fnv_a_init}, {"fnv_a_update", l_fnv_a_update}, {"fnv_a_final", l_fnv_a_final},
- {"oaat", l_oaat}, {"oaat_init", l_oaat_init}, {"oaat_update", l_oaat_update}, {"oaat_final", l_oaat_final},
- {"loselose", l_loselose}, {"loselose_init", l_loselose_init}, {"loselose_update", l_loselose_update}, {"loselose_final", l_loselose_final},
- {"pearson",l_pearson}, {"pearson_init",l_pearson_init}, {"pearson_update",l_pearson_update}, {"pearson_final",l_pearson_final},
- {"pjw", l_pjw}, {"pjw_init", l_pjw_init}, {"pjw_update", l_pjw_update}, {"pjw_final", l_pjw_final},
- {"sdbm", l_sdbm}, {"sdbm_init", l_sdbm_init}, {"sdbm_update", l_sdbm_update}, {"sdbm_final", l_sdbm_final},
- {"sysvchecksum",l_sysvchecksum}, {"sysvchecksum_init",l_sysvchecksum_init}, {"sysvchecksum_update",l_sysvchecksum_update}, {"sysvchecksum_final",l_sysvchecksum_final},
- {"xor8",l_xor8}, {"xor8_init",l_xor8_init}, {"xor8_update",l_xor8_update}, {"xor8_final",l_xor8_final},
- {"md5",l_md5}, {"md5_init",l_md5_init}, {"md5_update",l_md5_update}, {"md5_final",l_md5_final},
- {"sha0",l_sha0}, {"sha0_init",l_sha0_init}, {"sha0_update",l_sha0_update}, {"sha0_final",l_sha0_final},
- {"sha1",l_sha1}, {"sha1_init",l_sha1_init}, {"sha1_update",l_sha1_update}, {"sha1_final",l_sha1_final},
- {"sha512", l_sha512}, {"sha512_init", l_sha512_init}, {"sha512_update", l_sha512_update}, {"sha512_final", l_sha512_final},
- {"sha384", l_sha384}, {"sha384_init", l_sha384_init}, {"sha384_update", l_sha384_update}, {"sha384_final", l_sha384_final},
- {"sha512_t", l_sha512_t}, {"sha512_t_init", l_sha512_t_init}, {"sha512_t_update", l_sha512_t_update}, {"sha512_t_final", l_sha512_t_final},
- {"sha256",l_sha256}, {"sha256_init",l_sha256_init}, {"sha256_update",l_sha256_update}, {"sha256_final",l_sha256_final},
- {"sha224",l_sha224}, {"sha224_init",l_sha224_init}, {"sha224_update",l_sha224_update}, {"sha224_final",l_sha224_final},
- {"blake256", l_blake256}, {"blake256_init", l_blake256_init}, {"blake256_update", l_blake256_update}, {"blake256_final", l_blake256_final},
- {"blake224", l_blake224}, {"blake224_init", l_blake224_init}, {"blake224_update", l_blake224_update}, {"blake224_final", l_blake224_final},
- {"blake512", l_blake512}, {"blake512_init", l_blake512_init}, {"blake512_update", l_blake512_update}, {"blake512_final", l_blake512_final},
- {"blake384", l_blake384}, {"blake384_init", l_blake384_init}, {"blake384_update", l_blake384_update}, {"blake384_final", l_blake384_final},
- {"blake2s", l_blake2s}, {"blake2s_init", l_blake2s_init}, {"blake2s_update", l_blake2s_update}, {"blake2s_final", l_blake2s_final},
- {"blake2b", l_blake2b}, {"blake2b_init", l_blake2b_init}, {"blake2b_update", l_blake2b_update}, {"blake2b_final", l_blake2b_final},
-
- {"uuencode",l_uuencode},
- {"uudecode",l_uudecode},
-
- {"base64encode",l_base64encode},
- {"base64decode",l_base64decode},
-
- {"baseconvert",l_baseconvert},
-
- {NULL,NULL}
+ {"setpearson",l_setpearson}, {"fletcher8",l_fletcher8},
+ {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
+ {"setbuzhash",l_setbuzhash},
+ {"loselose", l_loselose},
+ {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32},
+
+ {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final},
+ {"bsdchecksum",l_bsdchecksum}, {"bsdchecksum_init",l_bsdchecksum_init}, {"bsdchecksum_update",l_bsdchecksum_update}, {"bsdchecksum_final",l_bsdchecksum_final},
+ {"buzhash8",l_buzhash8}, {"buzhash16",l_buzhash16},
+ {"crc8",l_crc8}, {"crc8_init",l_crc8_init}, {"crc8_update",l_crc8_update}, {"crc8_final",l_crc8_final},
+ {"crc16",l_crc16}, {"crc16_init",l_crc16_init}, {"crc16_update",l_crc16_update}, {"crc16_final",l_crc16_final},
+ {"crc32",l_crc32}, {"crc32_init",l_crc32_init}, {"crc32_update",l_crc32_update}, {"crc32_final",l_crc32_final},
+ {"djb2", l_djb2}, {"djb2_init", l_djb2_init}, {"djb2_update", l_djb2_update}, {"djb2_final", l_djb2_final},
+ {"fletcher8",l_fletcher8}, {"fletcher8_init",l_fletcher8_init}, {"fletcher8_update",l_fletcher8_update}, {"fletcher8_final",l_fletcher8_final},
+ {"fletcher16",l_fletcher16}, {"fletcher16_init",l_fletcher16_init}, {"fletcher16_update",l_fletcher16_update}, {"fletcher16_final",l_fletcher16_final},
+ {"fletcher32",l_fletcher32}, {"fletcher32_init",l_fletcher32_init}, {"fletcher32_update",l_fletcher32_update}, {"fletcher32_final",l_fletcher32_final},
+ {"fnv_0", l_fnv_0}, {"fnv_0_init", l_fnv_0_init}, {"fnv_0_update", l_fnv_0_update}, {"fnv_0_final", l_fnv_0_final},
+ {"fnv_1", l_fnv_1}, {"fnv_1_init", l_fnv_1_init}, {"fnv_1_update", l_fnv_1_update}, {"fnv_1_final", l_fnv_1_final},
+ {"fnv_a", l_fnv_a}, {"fnv_a_init", l_fnv_a_init}, {"fnv_a_update", l_fnv_a_update}, {"fnv_a_final", l_fnv_a_final},
+ {"oaat", l_oaat}, {"oaat_init", l_oaat_init}, {"oaat_update", l_oaat_update}, {"oaat_final", l_oaat_final},
+ {"loselose", l_loselose}, {"loselose_init", l_loselose_init}, {"loselose_update", l_loselose_update}, {"loselose_final", l_loselose_final},
+ {"pearson",l_pearson}, {"pearson_init",l_pearson_init}, {"pearson_update",l_pearson_update}, {"pearson_final",l_pearson_final},
+ {"pjw", l_pjw}, {"pjw_init", l_pjw_init}, {"pjw_update", l_pjw_update}, {"pjw_final", l_pjw_final},
+ {"sdbm", l_sdbm}, {"sdbm_init", l_sdbm_init}, {"sdbm_update", l_sdbm_update}, {"sdbm_final", l_sdbm_final},
+ {"sysvchecksum",l_sysvchecksum}, {"sysvchecksum_init",l_sysvchecksum_init}, {"sysvchecksum_update",l_sysvchecksum_update}, {"sysvchecksum_final",l_sysvchecksum_final},
+ {"xor8",l_xor8}, {"xor8_init",l_xor8_init}, {"xor8_update",l_xor8_update}, {"xor8_final",l_xor8_final},
+ {"md5",l_md5}, {"md5_init",l_md5_init}, {"md5_update",l_md5_update}, {"md5_final",l_md5_final},
+ {"sha0",l_sha0}, {"sha0_init",l_sha0_init}, {"sha0_update",l_sha0_update}, {"sha0_final",l_sha0_final},
+ {"sha1",l_sha1}, {"sha1_init",l_sha1_init}, {"sha1_update",l_sha1_update}, {"sha1_final",l_sha1_final},
+ {"sha512", l_sha512}, {"sha512_init", l_sha512_init}, {"sha512_update", l_sha512_update}, {"sha512_final", l_sha512_final},
+ {"sha384", l_sha384}, {"sha384_init", l_sha384_init}, {"sha384_update", l_sha384_update}, {"sha384_final", l_sha384_final},
+ {"sha512_t", l_sha512_t}, {"sha512_t_init", l_sha512_t_init}, {"sha512_t_update", l_sha512_t_update}, {"sha512_t_final", l_sha512_t_final},
+ {"sha256",l_sha256}, {"sha256_init",l_sha256_init}, {"sha256_update",l_sha256_update}, {"sha256_final",l_sha256_final},
+ {"sha224",l_sha224}, {"sha224_init",l_sha224_init}, {"sha224_update",l_sha224_update}, {"sha224_final",l_sha224_final},
+ {"blake256", l_blake256}, {"blake256_init", l_blake256_init}, {"blake256_update", l_blake256_update}, {"blake256_final", l_blake256_final},
+ {"blake224", l_blake224}, {"blake224_init", l_blake224_init}, {"blake224_update", l_blake224_update}, {"blake224_final", l_blake224_final},
+ {"blake512", l_blake512}, {"blake512_init", l_blake512_init}, {"blake512_update", l_blake512_update}, {"blake512_final", l_blake512_final},
+ {"blake384", l_blake384}, {"blake384_init", l_blake384_init}, {"blake384_update", l_blake384_update}, {"blake384_final", l_blake384_final},
+ {"blake2s", l_blake2s}, {"blake2s_init", l_blake2s_init}, {"blake2s_update", l_blake2s_update}, {"blake2s_final", l_blake2s_final},
+ {"blake2b", l_blake2b}, {"blake2b_init", l_blake2b_init}, {"blake2b_update", l_blake2b_update}, {"blake2b_final", l_blake2b_final},
+
+ {"uuencode",l_uuencode},
+ {"uudecode",l_uudecode},
+
+ {"base64encode",l_base64encode},
+ {"base64decode",l_base64decode},
+
+ {"baseconvert",l_baseconvert},
+
+ {NULL,NULL}
};
static struct config crypto_config[] = {
diff --git a/src/encode/base64.c b/src/encode/base64.c index 2cdda7e..d6b3474 100644 --- a/src/encode/base64.c +++ b/src/encode/base64.c @@ -5,70 +5,70 @@ #include "../crypto.h" int char_index(int c){ - if(c <= 25) return 65 + c; - if(c <= 51) return 71 + c; - if(c <= 61) return (c - 52)+48; - if(c == 62) return 43; - if(c == 63) return 47; - return 61; + if(c <= 25) return 65 + c; + if(c <= 51) return 71 + c; + if(c <= 61) return (c - 52)+48; + if(c == 62) return 43; + if(c == 63) return 47; + return 61; } int ichar_index(int c){ - if(65 <= c && c <= 90) return c - 65; - if(97 <= c && c <= 122) return c - 97 + 26; - if(48 <= c && c <= 57) return c - 48 + 52; - if(c == 47) return 63; - return 0; + if(65 <= c && c <= 90) return c - 65; + if(97 <= c && c <= 122) return c - 97 + 26; + if(48 <= c && c <= 57) return c - 48 + 52; + if(c == 47) return 63; + return 0; } int de_base64(char* in, char* out){ - int len = 0; - for(int i = 0; in[i]!='\0'; i++) len++; - - //char out[len]; - for(int i = 0; i < len; i+=4){ - uint8_t u1 = i>len?0:in[i]; - uint8_t u2 = i+1>len?0:in[i+1]; - uint8_t u3 = i+2>len?0:in[i+2]; - uint8_t u4 = i+3>len?0:in[i+3]; - - u1 = ichar_index(u1); - u2 = ichar_index(u2); - u3 = ichar_index(u3); - u4 = ichar_index(u4); - - uint8_t left = u1 << 2 | u2 >> 4; - uint8_t middle = u2 << 4 | u3 >> 2; - uint8_t right = u3 << 6 | u4; - - if(u4==0) sprintf(out,"%s%c%c",out,left,middle); - else if(u3==0) sprintf(out,"%s%c",out,left); - else sprintf(out,"%s%c%c%c",out,left,middle,right); - - } - return 0; + int len = 0; + for(int i = 0; in[i]!='\0'; i++) len++; + + //char out[len]; + for(int i = 0; i < len; i+=4){ + uint8_t u1 = i>len?0:in[i]; + uint8_t u2 = i+1>len?0:in[i+1]; + uint8_t u3 = i+2>len?0:in[i+2]; + uint8_t u4 = i+3>len?0:in[i+3]; + + u1 = ichar_index(u1); + u2 = ichar_index(u2); + u3 = ichar_index(u3); + u4 = ichar_index(u4); + + uint8_t left = u1 << 2 | u2 >> 4; + uint8_t middle = u2 << 4 | u3 >> 2; + uint8_t right = u3 << 6 | u4; + + if(u4==0) sprintf(out,"%s%c%c",out,left,middle); + else if(u3==0) sprintf(out,"%s%c",out,left); + else sprintf(out,"%s%c%c%c",out,left,middle,right); + + } + return 0; } int en_base64(char* in, char* out){ - int len = 0; - for(int i = 0; in[i]!='\0'; i++) len++; - - //char out[(len+1)*3]; - for(int i = 0; i < len; i+=3){ - uint8_t f = i>len?0:in[i]; - uint8_t s = i+1>len?0:in[i+1]; - uint8_t t = i+2>len?0:in[i+2]; - - uint8_t i1 = f>>2; - uint8_t i2 = (uint8_t)(f<<6)>>2 | (s>>4); - uint8_t i3 = (uint8_t)(s<<4)>>2 | (t>>6); - uint8_t i4 = t & 0x3f; - - if(t==0)i4 = 64; - if(s==0)i3 = 64; - sprintf(out,"%s%c%c%c%c",out,char_index(i1),char_index(i2), - char_index(i3),char_index(i4)); - } - return 0; + int len = 0; + for(int i = 0; in[i]!='\0'; i++) len++; + + //char out[(len+1)*3]; + for(int i = 0; i < len; i+=3){ + uint8_t f = i>len?0:in[i]; + uint8_t s = i+1>len?0:in[i+1]; + uint8_t t = i+2>len?0:in[i+2]; + + uint8_t i1 = f>>2; + uint8_t i2 = (uint8_t)(f<<6)>>2 | (s>>4); + uint8_t i3 = (uint8_t)(s<<4)>>2 | (t>>6); + uint8_t i4 = t & 0x3f; + + if(t==0)i4 = 64; + if(s==0)i3 = 64; + sprintf(out,"%s%c%c%c%c",out,char_index(i1),char_index(i2), + char_index(i3),char_index(i4)); + } + return 0; } int l_base64encode(lua_State* L){ @@ -76,7 +76,7 @@ int l_base64encode(lua_State* L){ const char* _a = lua_tolstring(L, 1, &len); char *a = calloc(len, sizeof * a); memcpy(a, _a, len); - + char* encode = calloc(len * 3,sizeof * encode); en_base64(a, encode); lua_pushstring(L, encode); @@ -95,7 +95,7 @@ int l_base64decode(lua_State* L){ char* encode = calloc(len, sizeof *encode); de_base64(a, encode); lua_pushstring(L, encode); - + free(a); free(encode); return 1; diff --git a/src/encode/uuencode.c b/src/encode/uuencode.c index 6f92fb0..b692fdc 100644 --- a/src/encode/uuencode.c +++ b/src/encode/uuencode.c @@ -5,79 +5,79 @@ #include "../crypto.h" int uuechar_index(int c){ - return c + 32; + return c + 32; } int uueichar_index(int c){ - return c - 32; + return c - 32; } int de_uu(char* in2, char* out){ - int len = 0; - for(int i = 0; in2[i]!='\0'; i++) len++; - char in[len*2]; - sprintf(in," %s",in2); //padding byte to make things cleaner - - //char out[len]; - int skipped = 0; - for(int i = 0; i<len; i+=4){ - if((i - skipped*2)%60==0){ - i+=2; - skipped+=1; - }; - - uint8_t u1 = i>len?0:in[i]; - uint8_t u2 = i+1>len?0:in[i+1]; - uint8_t u3 = i+2>len?0:in[i+2]; - uint8_t u4 = i+3>len?0:in[i+3]; - - u1 = uueichar_index(u1); - u2 = uueichar_index(u2); - u3 = uueichar_index(u3); - u4 = uueichar_index(u4); - - uint8_t left = u1 << 2 | u2 >> 4; - uint8_t middle = u2 << 4 | u3 >> 2; - uint8_t right = u3 << 6 | u4; - if(u1 < 64 && u2 < 64){ - sprintf(out,"%s%c",out,left); - } - if(u2 < 64 && u3 < 64){ - sprintf(out,"%s%c",out,middle); - } - if(u3 < 64 && u4 < 64){ - sprintf(out,"%s%c",out,right); - } - + int len = 0; + for(int i = 0; in2[i]!='\0'; i++) len++; + char in[len*2]; + sprintf(in," %s",in2); //padding byte to make things cleaner + + //char out[len]; + int skipped = 0; + for(int i = 0; i<len; i+=4){ + if((i - skipped*2)%60==0){ + i+=2; + skipped+=1; + }; + + uint8_t u1 = i>len?0:in[i]; + uint8_t u2 = i+1>len?0:in[i+1]; + uint8_t u3 = i+2>len?0:in[i+2]; + uint8_t u4 = i+3>len?0:in[i+3]; + + u1 = uueichar_index(u1); + u2 = uueichar_index(u2); + u3 = uueichar_index(u3); + u4 = uueichar_index(u4); + + uint8_t left = u1 << 2 | u2 >> 4; + uint8_t middle = u2 << 4 | u3 >> 2; + uint8_t right = u3 << 6 | u4; + if(u1 < 64 && u2 < 64){ + sprintf(out,"%s%c",out,left); } - return 0; + if(u2 < 64 && u3 < 64){ + sprintf(out,"%s%c",out,middle); + } + if(u3 < 64 && u4 < 64){ + sprintf(out,"%s%c",out,right); + } + + } + return 0; } int en_uu(char* in, char* out){ - int len = 0; - for(int i = 0; in[i]!='\0'; i++) len++; - - for(int i = 0; i < len; i+=3){ - uint8_t f = i>len?0:in[i]; - uint8_t s = i+1>len?0:in[i+1]; - uint8_t t = i+2>len?0:in[i+2]; - - uint8_t i1 = f>>2; - uint8_t i2 = (uint8_t)(f<<6)>>2 | (s>>4); - uint8_t i3 = (uint8_t)(s<<4)>>2 | (t>>6); - uint8_t i4 = t & 0x3f; - - if(t==0)i4 = 64; - if(s==0)i3 = 64; - if(i / 3 * 4 % 60 == 0){ - if(i==0) sprintf(out,"%c",(len - i >= 45) ? 'M':uuechar_index(len - i)); - else sprintf(out,"%s\n%c",out,(len - i >= 45) ? 'M':uuechar_index(len - i)); - } - sprintf(out,"%s%c%c%c%c",out,uuechar_index(i1),uuechar_index(i2), - uuechar_index(i3),uuechar_index(i4)); + int len = 0; + for(int i = 0; in[i]!='\0'; i++) len++; + + for(int i = 0; i < len; i+=3){ + uint8_t f = i>len?0:in[i]; + uint8_t s = i+1>len?0:in[i+1]; + uint8_t t = i+2>len?0:in[i+2]; + + uint8_t i1 = f>>2; + uint8_t i2 = (uint8_t)(f<<6)>>2 | (s>>4); + uint8_t i3 = (uint8_t)(s<<4)>>2 | (t>>6); + uint8_t i4 = t & 0x3f; + + if(t==0)i4 = 64; + if(s==0)i3 = 64; + if(i / 3 * 4 % 60 == 0){ + if(i==0) sprintf(out,"%c",(len - i >= 45) ? 'M':uuechar_index(len - i)); + else sprintf(out,"%s\n%c",out,(len - i >= 45) ? 'M':uuechar_index(len - i)); } - sprintf(out,"%s\n`",out); - - return 0; + sprintf(out,"%s%c%c%c%c",out,uuechar_index(i1),uuechar_index(i2), + uuechar_index(i3),uuechar_index(i4)); + } + sprintf(out,"%s\n`",out); + + return 0; } @@ -87,7 +87,7 @@ int l_uuencode(lua_State* L){ const char* _a = lua_tolstring(L, 1, &len); char *a = calloc(len, sizeof * a); memcpy(a, _a, len); - + char* encode = calloc(len * 3,sizeof * encode); en_uu(a, encode); lua_pushstring(L, encode); @@ -102,7 +102,7 @@ int l_uudecode(lua_State* L){ const char* _a = lua_tolstring(L, 1, &len); char *a = calloc(len, sizeof * a); memcpy(a, _a, len); - + char* encode = calloc(len,sizeof * encode); de_uu(a, encode); lua_pushstring(L, encode); diff --git a/src/hash/adler.c b/src/hash/adler.c index b4a1135..de007e0 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -44,7 +44,7 @@ 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);
-
+
char digest[32];
uint32_t u = adler32(a, len);
diff --git a/src/hash/blake.c b/src/hash/blake.c index 38d7da2..ca07482 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -88,65 +88,65 @@ void compress256(uint32_t* hash, char *block, uint64_t compressed){ #define bs 64
struct blake256_hash blake256_init(){
- struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- a.hash[0] = 0x6a09e667;
- a.hash[1] = 0xbb67ae85;
- a.hash[2] = 0x3c6ef372;
- a.hash[3] = 0xa54ff53a;
- a.hash[4] = 0x510e527f;
- a.hash[5] = 0x9b05688c;
- a.hash[6] = 0x1f83d9ab;
- a.hash[7] = 0x5be0cd19;
- return a;
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.hash[0] = 0x6a09e667;
+ a.hash[1] = 0xbb67ae85;
+ a.hash[2] = 0x3c6ef372;
+ a.hash[3] = 0xa54ff53a;
+ a.hash[4] = 0x510e527f;
+ a.hash[5] = 0x9b05688c;
+ a.hash[6] = 0x1f83d9ab;
+ a.hash[7] = 0x5be0cd19;
+ return a;
}
struct blake256_hash blake256_init_l(lua_State* L){
- struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- memset(a.buffer, 0, bs);
- a.hash[0] = 0x6a09e667;
- a.hash[1] = 0xbb67ae85;
- a.hash[2] = 0x3c6ef372;
- a.hash[3] = 0xa54ff53a;
- a.hash[4] = 0x510e527f;
- a.hash[5] = 0x9b05688c;
- a.hash[6] = 0x1f83d9ab;
- a.hash[7] = 0x5be0cd19;
- return a;
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ memset(a.buffer, 0, bs);
+ a.hash[0] = 0x6a09e667;
+ a.hash[1] = 0xbb67ae85;
+ a.hash[2] = 0x3c6ef372;
+ a.hash[3] = 0xa54ff53a;
+ a.hash[4] = 0x510e527f;
+ a.hash[5] = 0x9b05688c;
+ a.hash[6] = 0x1f83d9ab;
+ a.hash[7] = 0x5be0cd19;
+ return a;
}
struct blake256_hash blake224_init(){
- struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- a.hash[0] = 0xc1059ed8;
- a.hash[1] = 0x367cd507;
- a.hash[2] = 0x3070dd17;
- a.hash[3] = 0xf70e5939;
- a.hash[4] = 0xffc00b31;
- a.hash[5] = 0x68581511;
- a.hash[6] = 0x64f98fa7;
- a.hash[7] = 0xbefa4fa4;
- return a;
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.hash[0] = 0xc1059ed8;
+ a.hash[1] = 0x367cd507;
+ a.hash[2] = 0x3070dd17;
+ a.hash[3] = 0xf70e5939;
+ a.hash[4] = 0xffc00b31;
+ a.hash[5] = 0x68581511;
+ a.hash[6] = 0x64f98fa7;
+ a.hash[7] = 0xbefa4fa4;
+ return a;
}
struct blake256_hash blake224_init_l(lua_State* L){
- struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- memset(a.buffer, 0, bs);
- a.hash[0] = 0xc1059ed8;
- a.hash[1] = 0x367cd507;
- a.hash[2] = 0x3070dd17;
- a.hash[3] = 0xf70e5939;
- a.hash[4] = 0xffc00b31;
- a.hash[5] = 0x68581511;
- a.hash[6] = 0x64f98fa7;
- a.hash[7] = 0xbefa4fa4;
- return a;
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ memset(a.buffer, 0, bs);
+ a.hash[0] = 0xc1059ed8;
+ a.hash[1] = 0x367cd507;
+ a.hash[2] = 0x3070dd17;
+ a.hash[3] = 0xf70e5939;
+ a.hash[4] = 0xffc00b31;
+ a.hash[5] = 0x68581511;
+ a.hash[6] = 0x64f98fa7;
+ a.hash[7] = 0xbefa4fa4;
+ return a;
}
int blake256_free_l(lua_State* L){
@@ -250,7 +250,7 @@ void blake224_final(struct blake256_hash* hash, char* out_stream){ for(int i = 0; i != 7; i++){
sprintf(out_stream + i * 8, "%08x",(hash->hash)[i]);
}
-
+
memcpy(hash, &old_hash, sizeof * hash);
memcpy(hash->buffer, old, bs);
memcpy(hash->hash, hhash, sizeof * hhash * 8);
@@ -285,7 +285,7 @@ void blake224(char *out, char *in, uint64_t inlen){ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){
uint64_t v[16], m[16], i;
- for( i = 0; i < 16; ++i ) m[i] = U8TO64_BIG( block + i * 8 );
+ for( i = 0; i < 16; ++i ) m[i] = U8TO64_BIG( block + i * 8 );
for(int i = 0; i < 8; i++) v[i] = hash[i];
@@ -308,7 +308,7 @@ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ blake_round_512(1, 6, 11, 12, 10);
blake_round_512(2, 7, 8, 13, 12);
blake_round_512(3, 4, 9, 14, 14);
-
+
}
for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i];
@@ -318,65 +318,65 @@ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ #define bs_2 128
struct blake512_hash blake512_init(){
- struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- a.hash[0] = 0x6a09e667f3bcc908ULL;
- a.hash[1] = 0xbb67ae8584caa73bULL;
- a.hash[2] = 0x3c6ef372fe94f82bULL;
- a.hash[3] = 0xa54ff53a5f1d36f1ULL;
- a.hash[4] = 0x510e527fade682d1ULL;
- a.hash[5] = 0x9b05688c2b3e6c1fULL;
- a.hash[6] = 0x1f83d9abfb41bd6bULL;
- a.hash[7] = 0x5be0cd19137e2179ULL;
- return a;
+ struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.hash[0] = 0x6a09e667f3bcc908ULL;
+ a.hash[1] = 0xbb67ae8584caa73bULL;
+ a.hash[2] = 0x3c6ef372fe94f82bULL;
+ a.hash[3] = 0xa54ff53a5f1d36f1ULL;
+ a.hash[4] = 0x510e527fade682d1ULL;
+ a.hash[5] = 0x9b05688c2b3e6c1fULL;
+ a.hash[6] = 0x1f83d9abfb41bd6bULL;
+ a.hash[7] = 0x5be0cd19137e2179ULL;
+ return a;
}
struct blake512_hash blake512_init_l(lua_State* L){
- struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- memset(a.buffer, 0, bs_2);
- a.hash[0] = 0x6a09e667f3bcc908ULL;
- a.hash[1] = 0xbb67ae8584caa73bULL;
- a.hash[2] = 0x3c6ef372fe94f82bULL;
- a.hash[3] = 0xa54ff53a5f1d36f1ULL;
- a.hash[4] = 0x510e527fade682d1ULL;
- a.hash[5] = 0x9b05688c2b3e6c1fULL;
- a.hash[6] = 0x1f83d9abfb41bd6bULL;
- a.hash[7] = 0x5be0cd19137e2179ULL;
- return a;
+ struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ memset(a.buffer, 0, bs_2);
+ a.hash[0] = 0x6a09e667f3bcc908ULL;
+ a.hash[1] = 0xbb67ae8584caa73bULL;
+ a.hash[2] = 0x3c6ef372fe94f82bULL;
+ a.hash[3] = 0xa54ff53a5f1d36f1ULL;
+ a.hash[4] = 0x510e527fade682d1ULL;
+ a.hash[5] = 0x9b05688c2b3e6c1fULL;
+ a.hash[6] = 0x1f83d9abfb41bd6bULL;
+ a.hash[7] = 0x5be0cd19137e2179ULL;
+ return a;
}
struct blake384_hash blake384_init(){
- struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- a.hash[0] = 0xcbbb9d5dc1059ed8ULL;
- a.hash[1] = 0x629a292a367cd507ULL;
- a.hash[2] = 0x9159015a3070dd17ULL;
- a.hash[3] = 0x152fecd8f70e5939ULL;
- a.hash[4] = 0x67332667ffc00b31ULL;
- a.hash[5] = 0x8eb44a8768581511ULL;
- a.hash[6] = 0xdb0c2e0d64f98fa7ULL;
- a.hash[7] = 0x47b5481dbefa4fa4ULL;
- return a;
+ struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.hash[0] = 0xcbbb9d5dc1059ed8ULL;
+ a.hash[1] = 0x629a292a367cd507ULL;
+ a.hash[2] = 0x9159015a3070dd17ULL;
+ a.hash[3] = 0x152fecd8f70e5939ULL;
+ a.hash[4] = 0x67332667ffc00b31ULL;
+ a.hash[5] = 0x8eb44a8768581511ULL;
+ a.hash[6] = 0xdb0c2e0d64f98fa7ULL;
+ a.hash[7] = 0x47b5481dbefa4fa4ULL;
+ return a;
}
struct blake384_hash blake384_init_l(lua_State* L){
- struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- memset(a.buffer, 0, bs_2);
- a.hash[0] = 0xcbbb9d5dc1059ed8ULL;
- a.hash[1] = 0x629a292a367cd507ULL;
- a.hash[2] = 0x9159015a3070dd17ULL;
- a.hash[3] = 0x152fecd8f70e5939ULL;
- a.hash[4] = 0x67332667ffc00b31ULL;
- a.hash[5] = 0x8eb44a8768581511ULL;
- a.hash[6] = 0xdb0c2e0d64f98fa7ULL;
- a.hash[7] = 0x47b5481dbefa4fa4ULL;
- return a;
+ struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ memset(a.buffer, 0, bs_2);
+ a.hash[0] = 0xcbbb9d5dc1059ed8ULL;
+ a.hash[1] = 0x629a292a367cd507ULL;
+ a.hash[2] = 0x9159015a3070dd17ULL;
+ a.hash[3] = 0x152fecd8f70e5939ULL;
+ a.hash[4] = 0x67332667ffc00b31ULL;
+ a.hash[5] = 0x8eb44a8768581511ULL;
+ a.hash[6] = 0xdb0c2e0d64f98fa7ULL;
+ a.hash[7] = 0x47b5481dbefa4fa4ULL;
+ return a;
}
int blake512_free_l(lua_State* L){
@@ -528,17 +528,17 @@ int l_blake256_final(lua_State* L){ }
int l_blake256(lua_State* L){
- if(lua_gettop(L) == 0) return l_blake256_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_blake256_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[257] = {0};
+ char digest[257] = {0};
- blake256(digest, (char*)a, len);
+ blake256(digest, (char*)a, len);
- lua_pushstring(L, digest);
+ lua_pushstring(L, digest);
- return 1;
+ return 1;
}
int l_blake224_clone(lua_State* L){
@@ -569,17 +569,17 @@ int l_blake224_final(lua_State* L){ }
int l_blake224(lua_State* L){
- if(lua_gettop(L) == 0) return l_blake224_init(L);
- size_t len = 0;
- char* a = (char*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_blake224_init(L);
+ size_t len = 0;
+ char* a = (char*)luaL_checklstring(L, 1, &len);
- char digest[257] = {0};
+ char digest[257] = {0};
- blake224(digest, (char*)a, len);
+ blake224(digest, (char*)a, len);
- lua_pushstring(L, digest);
+ lua_pushstring(L, digest);
- return 1;
+ return 1;
}
int l_blake512_clone(lua_State* L){
@@ -610,18 +610,18 @@ int l_blake512_final(lua_State* L){ }
int l_blake512(lua_State* L){
- if(lua_gettop(L) == 0) return l_blake512_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_blake512_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[513] = {0};
- //memset(digest, 0, 513);
+ char digest[513] = {0};
+ //memset(digest, 0, 513);
- //blake512(digest, a, len, b512);
- blake512(a, len, digest);
- lua_pushstring(L, digest);
+ //blake512(digest, a, len, b512);
+ blake512(a, len, digest);
+ lua_pushstring(L, digest);
- return 1;
+ return 1;
}
int l_blake384_clone(lua_State* L){
@@ -652,14 +652,14 @@ int l_blake384_final(lua_State* L){ }
int l_blake384(lua_State* L){
- if(lua_gettop(L) == 0) return l_blake384_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_blake384_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[513] = {0};
+ char digest[513] = {0};
- blake384(a, len, digest);
- lua_pushstring(L, digest);
+ blake384(a, len, digest);
+ lua_pushstring(L, digest);
- return 1;
+ return 1;
}
diff --git a/src/hash/blake.h b/src/hash/blake.h index 07caf3d..3dacef5 100644 --- a/src/hash/blake.h +++ b/src/hash/blake.h @@ -19,18 +19,18 @@ #define wtf(b) (b[0] << 24)&0xff000000 | (b[1] << 16)&0xff0000 | (b[2] << 8)&0xff00 | b[3]&0xff
struct blake256_hash {
- uint8_t* buffer;
- size_t bufflen;
- uint32_t total, *hash;
- uint64_t compressed;
+ uint8_t* buffer;
+ size_t bufflen;
+ uint32_t total, *hash;
+ uint64_t compressed;
};
#define blake224_hash blake256_hash
struct blake512_hash {
- uint8_t* buffer;
- size_t bufflen;
- uint64_t total, *hash;
- uint64_t compressed;
+ uint8_t* buffer;
+ size_t bufflen;
+ uint64_t total, *hash;
+ uint64_t compressed;
};
#define blake384_hash blake512_hash
@@ -52,4 +52,4 @@ int l_blake512_final(lua_State* L); int l_blake384(lua_State* L);
int l_blake384_init(lua_State* L);
int l_blake384_update(lua_State* L);
-int l_blake384_final(lua_State* L);
\ No newline at end of file +int l_blake384_final(lua_State* L);
diff --git a/src/hash/blake2.c b/src/hash/blake2.c index 42dc0ab..974a323 100644 --- a/src/hash/blake2.c +++ b/src/hash/blake2.c @@ -5,125 +5,125 @@ #include "../crypto.h"
void mix2b(uint64_t* a, uint64_t* b, uint64_t* c, uint64_t* d, int64_t x, int64_t y){
- *a = *a + *b + x;
- *d = rotr64((*d ^ *a), 32);
+ *a = *a + *b + x;
+ *d = rotr64((*d ^ *a), 32);
- *c += *d;
- *b = rotr64((*b ^ *c), 24);
+ *c += *d;
+ *b = rotr64((*b ^ *c), 24);
- *a += *b + y;
- *d = rotr64((*d ^ *a), 16);
+ *a += *b + y;
+ *d = rotr64((*d ^ *a), 16);
- *c += *d;
- *b = rotr64((*b ^ *c), 63);
+ *c += *d;
+ *b = rotr64((*b ^ *c), 63);
}
void mix2s(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d, int32_t x, int32_t y){
- *a = *a + *b + x;
- *d = rotr32((*d ^ *a), 16);
+ *a = *a + *b + x;
+ *d = rotr32((*d ^ *a), 16);
- *c += *d;
- *b = rotr32((*b ^ *c), 12);
+ *c += *d;
+ *b = rotr32((*b ^ *c), 12);
- *a += *b + y;
- *d = rotr32((*d ^ *a), 8);
+ *a += *b + y;
+ *d = rotr32((*d ^ *a), 8);
- *c += *d;
- *b = rotr32((*b ^ *c), 7);
+ *c += *d;
+ *b = rotr32((*b ^ *c), 7);
}
#define baking(type) \
- mix2##type(&v[0], &v[4], &v[8], &v[12], m[s[0]], m[s[1]]);\
- mix2##type(&v[1], &v[5], &v[9], &v[13], m[s[2]], m[s[3]]);\
- mix2##type(&v[2], &v[6], &v[10], &v[14], m[s[4]], m[s[5]]);\
- mix2##type(&v[3], &v[7], &v[11], &v[15], m[s[6]], m[s[7]]);\
- \
- mix2##type(&v[0], &v[5], &v[10], &v[15], m[s[8]], m[s[9]]);\
- mix2##type(&v[1], &v[6], &v[11], &v[12], m[s[10]], m[s[11]]);\
- mix2##type(&v[2], &v[7], &v[8], &v[13], m[s[12]], m[s[13]]);\
- mix2##type(&v[3], &v[4], &v[9], &v[14], m[s[14]], m[s[15]]);
+ mix2##type(&v[0], &v[4], &v[8], &v[12], m[s[0]], m[s[1]]);\
+ mix2##type(&v[1], &v[5], &v[9], &v[13], m[s[2]], m[s[3]]);\
+ mix2##type(&v[2], &v[6], &v[10], &v[14], m[s[4]], m[s[5]]);\
+ mix2##type(&v[3], &v[7], &v[11], &v[15], m[s[6]], m[s[7]]);\
+ \
+ mix2##type(&v[0], &v[5], &v[10], &v[15], m[s[8]], m[s[9]]);\
+ mix2##type(&v[1], &v[6], &v[11], &v[12], m[s[10]], m[s[11]]);\
+ mix2##type(&v[2], &v[7], &v[8], &v[13], m[s[12]], m[s[13]]);\
+ mix2##type(&v[3], &v[4], &v[9], &v[14], m[s[14]], m[s[15]]);
void compress2b(uint64_t* hash, uint8_t* inp, uint64_t compressed, int final){
- uint64_t v[16], s[16], m[16];
-
- #pragma unroll
- for(int i = 0; i != 8; i++)
- v[i] = hash[i];
-
- for(int i = 0; i != 16; i++)
- m[i] = ((uint64_t*)inp)[i];
-
- v[8] = sha512_iv.h0;
- v[9] = sha512_iv.h1;
- v[10] = sha512_iv.h2;
- v[11] = sha512_iv.h3;
- v[12] = sha512_iv.h4;
- v[13] = sha512_iv.h5;
- v[14] = sha512_iv.h6;
- v[15] = sha512_iv.h7;
-
- v[12] ^= compressed;
- v[13] ^= 0;
-
- if(final)
- v[14] ^= 0xFFFFFFFFFFFFFFFF;
-
- for(int i = 0; i != 12; i++){
- for(int j = 0; j != 16; j++){
- s[j] = blake2b_sigma[i%10][j];
- }
-
- baking(b);
- }
-
- for (int i = 0; i < 8; i++) {
- hash[i] = hash[i] ^ v[i] ^ v[i + 8];
+ uint64_t v[16], s[16], m[16];
+
+#pragma unroll
+ for(int i = 0; i != 8; i++)
+ v[i] = hash[i];
+
+ for(int i = 0; i != 16; i++)
+ m[i] = ((uint64_t*)inp)[i];
+
+ v[8] = sha512_iv.h0;
+ v[9] = sha512_iv.h1;
+ v[10] = sha512_iv.h2;
+ v[11] = sha512_iv.h3;
+ v[12] = sha512_iv.h4;
+ v[13] = sha512_iv.h5;
+ v[14] = sha512_iv.h6;
+ v[15] = sha512_iv.h7;
+
+ v[12] ^= compressed;
+ v[13] ^= 0;
+
+ if(final)
+ v[14] ^= 0xFFFFFFFFFFFFFFFF;
+
+ for(int i = 0; i != 12; i++){
+ for(int j = 0; j != 16; j++){
+ s[j] = blake2b_sigma[i%10][j];
}
+
+ baking(b);
+ }
+
+ for (int i = 0; i < 8; i++) {
+ hash[i] = hash[i] ^ v[i] ^ v[i + 8];
+ }
}
void compress2s(uint32_t* hash, uint8_t* inp, uint32_t compressed, int final){
- uint32_t v[16], s[16], m[16];
-
- #pragma unroll
- for(int i = 0; i != 8; i++)
- v[i] = hash[i];
-
- for(int i = 0; i != 16; i++)
- m[i] = ((uint32_t*)inp)[i];
-
- v[8] = sha512_iv.h0 >> 32;
- v[9] = sha512_iv.h1 >> 32;
- v[10] = sha512_iv.h2 >> 32;
- v[11] = sha512_iv.h3 >> 32;
- v[12] = sha512_iv.h4 >> 32;
- v[13] = sha512_iv.h5 >> 32;
- v[14] = sha512_iv.h6 >> 32;
- v[15] = sha512_iv.h7 >> 32;
-
- v[12] ^= compressed; //make this 64bit
- v[13] ^= 0;
-
- if(final)
- v[14] ^= 0xFFFFFFFFFFFFFFFF >> 32;
-
- for(int i = 0; i != 10; i++){
- for(int j = 0; j != 16; j++){
- s[j] = blake2b_sigma[i][j];
- }
-
- baking(s);
- }
-
- for (int i = 0; i < 8; i++) {
- hash[i] = hash[i] ^ v[i] ^ v[i + 8];
+ uint32_t v[16], s[16], m[16];
+
+#pragma unroll
+ for(int i = 0; i != 8; i++)
+ v[i] = hash[i];
+
+ for(int i = 0; i != 16; i++)
+ m[i] = ((uint32_t*)inp)[i];
+
+ v[8] = sha512_iv.h0 >> 32;
+ v[9] = sha512_iv.h1 >> 32;
+ v[10] = sha512_iv.h2 >> 32;
+ v[11] = sha512_iv.h3 >> 32;
+ v[12] = sha512_iv.h4 >> 32;
+ v[13] = sha512_iv.h5 >> 32;
+ v[14] = sha512_iv.h6 >> 32;
+ v[15] = sha512_iv.h7 >> 32;
+
+ v[12] ^= compressed; //make this 64bit
+ v[13] ^= 0;
+
+ if(final)
+ v[14] ^= 0xFFFFFFFFFFFFFFFF >> 32;
+
+ for(int i = 0; i != 10; i++){
+ for(int j = 0; j != 16; j++){
+ s[j] = blake2b_sigma[i][j];
}
+
+ baking(s);
+ }
+
+ for (int i = 0; i < 8; i++) {
+ hash[i] = hash[i] ^ v[i] ^ v[i + 8];
+ }
}
struct blake2b_hash {
- uint8_t *buffer, *key;
- size_t bufflen, keylen;
- uint64_t total, *hash;
- uint64_t compressed, digest_len;
+ uint8_t *buffer, *key;
+ size_t bufflen, keylen;
+ uint64_t total, *hash;
+ uint64_t compressed, digest_len;
};
void blake2b_round(struct blake2b_hash* hash, int last){
@@ -132,60 +132,60 @@ void blake2b_round(struct blake2b_hash* hash, int last){ #define bs_2 128
struct blake2b_hash blake2b_init(char* key, int key_len, int digest_len){
- struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- a.key = calloc(sizeof* a.key, key_len);
- memcpy(a.key, key, key_len);
- memcpy(a.buffer, key, key_len);
-
- a.hash[0] = sha512_iv.h0;
- a.hash[0] ^= digest_len;
- a.hash[0] ^= key_len << 8;
- a.hash[0] ^= 0x01010000;
- a.hash[1] = sha512_iv.h1;
- a.hash[2] = sha512_iv.h2;
- a.hash[3] = sha512_iv.h3;
- a.hash[4] = sha512_iv.h4;
- a.hash[5] = sha512_iv.h5;
- a.hash[6] = sha512_iv.h6;
- a.hash[7] = sha512_iv.h7;
- if(key_len != 0){
- a.compressed = 128;
- blake2b_round(&a, 0);
- memset(a.buffer, 0, bs_2);
- a.bufflen = 0;
- }
- return a;
+ struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.key = calloc(sizeof* a.key, key_len);
+ memcpy(a.key, key, key_len);
+ memcpy(a.buffer, key, key_len);
+
+ a.hash[0] = sha512_iv.h0;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1;
+ a.hash[2] = sha512_iv.h2;
+ a.hash[3] = sha512_iv.h3;
+ a.hash[4] = sha512_iv.h4;
+ a.hash[5] = sha512_iv.h5;
+ a.hash[6] = sha512_iv.h6;
+ a.hash[7] = sha512_iv.h7;
+ if(key_len != 0){
+ a.compressed = 128;
+ blake2b_round(&a, 0);
+ memset(a.buffer, 0, bs_2);
+ a.bufflen = 0;
+ }
+ return a;
}
struct blake2b_hash blake2b_init_l(lua_State* L, char* key, int key_len, int digest_len){
- struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
- a.buffer = calloc(sizeof * a.buffer, bs_2);
- a.hash = calloc(sizeof * a.hash, 8);
- a.key = calloc(sizeof* a.key, key_len);
+ struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = calloc(sizeof * a.buffer, bs_2);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.key = calloc(sizeof* a.key, key_len);
+ memset(a.buffer, 0, bs_2);
+ memcpy(a.key, key, key_len);
+ memcpy(a.buffer, key, key_len);
+
+ a.hash[0] = sha512_iv.h0;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1;
+ a.hash[2] = sha512_iv.h2;
+ a.hash[3] = sha512_iv.h3;
+ a.hash[4] = sha512_iv.h4;
+ a.hash[5] = sha512_iv.h5;
+ a.hash[6] = sha512_iv.h6;
+ a.hash[7] = sha512_iv.h7;
+ if(key_len != 0){
+ a.compressed = 128;
+ blake2b_round(&a, 0);
memset(a.buffer, 0, bs_2);
- memcpy(a.key, key, key_len);
- memcpy(a.buffer, key, key_len);
-
- a.hash[0] = sha512_iv.h0;
- a.hash[0] ^= digest_len;
- a.hash[0] ^= key_len << 8;
- a.hash[0] ^= 0x01010000;
- a.hash[1] = sha512_iv.h1;
- a.hash[2] = sha512_iv.h2;
- a.hash[3] = sha512_iv.h3;
- a.hash[4] = sha512_iv.h4;
- a.hash[5] = sha512_iv.h5;
- a.hash[6] = sha512_iv.h6;
- a.hash[7] = sha512_iv.h7;
- if(key_len != 0){
- a.compressed = 128;
- blake2b_round(&a, 0);
- memset(a.buffer, 0, bs_2);
- a.bufflen = 0;
- }
- return a;
+ a.bufflen = 0;
+ }
+ return a;
}
int blake2b_free_l(lua_State* L){
@@ -205,30 +205,30 @@ int blake2s_free_l(lua_State* L){ }
void blake2b_update(uint8_t* input, size_t len, struct blake2b_hash* hash){
- hash->total += len;
- size_t total_add = len + hash->bufflen;
- size_t read = 0;
- if(total_add < bs_2){
- memcpy(hash->buffer + hash->bufflen, input, len);
- hash->bufflen += len;
- return;
- }
+ hash->total += len;
+ size_t total_add = len + hash->bufflen;
+ size_t read = 0;
+ if(total_add < bs_2){
+ memcpy(hash->buffer + hash->bufflen, input, len);
+ hash->bufflen += len;
+ return;
+ }
- for(; total_add >= bs_2;){
- memcpy(hash->buffer + hash->bufflen, input + read, bs_2 - hash->bufflen);
- total_add -= bs_2;
- hash->bufflen = 0;
- read += bs_2;
- hash->compressed += 64;
- blake2b_round(hash, 0);
- }
+ for(; total_add >= bs_2;){
+ memcpy(hash->buffer + hash->bufflen, input + read, bs_2 - hash->bufflen);
+ total_add -= bs_2;
+ hash->bufflen = 0;
+ read += bs_2;
+ hash->compressed += 64;
+ blake2b_round(hash, 0);
+ }
- memset(hash->buffer, 0, bs_2);
+ memset(hash->buffer, 0, bs_2);
- if(0 != total_add){
- memcpy(hash->buffer, input + read, total_add);
- hash->bufflen = total_add;
- }
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
}
void blake2b_final(struct blake2b_hash* hash, char* out_stream){
@@ -245,26 +245,26 @@ void blake2b_final(struct blake2b_hash* hash, char* out_stream){ blake2b_round(hash, 1);
for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i]));
-
+
memcpy(hash, &old_hash, sizeof * hash);
memcpy(hash->buffer, old, bs_2 * sizeof * old);
memcpy(hash->hash, hashh, 8 * sizeof * hashh);
}
void blake2b(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* out){
- struct blake2b_hash aa = blake2b_init(key, key_len, dig_len);
- blake2b_update(inp, len, &aa);
- blake2b_final(&aa, out);
- free(aa.buffer);
- free(aa.hash);
- free(aa.key);
+ struct blake2b_hash aa = blake2b_init(key, key_len, dig_len);
+ blake2b_update(inp, len, &aa);
+ blake2b_final(&aa, out);
+ free(aa.buffer);
+ free(aa.hash);
+ free(aa.key);
}
struct blake2s_hash {
- uint8_t *buffer, *key;
- size_t bufflen, keylen;
- uint32_t total, *hash;
- uint64_t compressed, digest_len;
+ uint8_t *buffer, *key;
+ size_t bufflen, keylen;
+ uint32_t total, *hash;
+ uint64_t compressed, digest_len;
};
void blake2s_round(struct blake2s_hash* hash, int last){
@@ -273,87 +273,87 @@ void blake2s_round(struct blake2s_hash* hash, int last){ #define bs 64
struct blake2s_hash blake2s_init(char* key, int key_len, int digest_len){
- struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- a.key = calloc(sizeof* a.key, key_len);
- memcpy(a.key, key, key_len);
- memcpy(a.buffer, key, key_len);
-
- a.hash[0] = sha512_iv.h0 >> 32;
- a.hash[0] ^= digest_len;
- a.hash[0] ^= key_len << 8;
- a.hash[0] ^= 0x01010000;
- a.hash[1] = sha512_iv.h1 >> 32;
- a.hash[2] = sha512_iv.h2 >> 32;
- a.hash[3] = sha512_iv.h3 >> 32;
- a.hash[4] = sha512_iv.h4 >> 32;
- a.hash[5] = sha512_iv.h5 >> 32;
- a.hash[6] = sha512_iv.h6 >> 32;
- a.hash[7] = sha512_iv.h7 >> 32;
- if(key_len != 0){
- a.compressed = 64;
- blake2s_round(&a, 0);
- memset(a.buffer, 0, bs);
- a.bufflen = 0;
- }
- return a;
+ struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.key = calloc(sizeof* a.key, key_len);
+ memcpy(a.key, key, key_len);
+ memcpy(a.buffer, key, key_len);
+
+ a.hash[0] = sha512_iv.h0 >> 32;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1 >> 32;
+ a.hash[2] = sha512_iv.h2 >> 32;
+ a.hash[3] = sha512_iv.h3 >> 32;
+ a.hash[4] = sha512_iv.h4 >> 32;
+ a.hash[5] = sha512_iv.h5 >> 32;
+ a.hash[6] = sha512_iv.h6 >> 32;
+ a.hash[7] = sha512_iv.h7 >> 32;
+ if(key_len != 0){
+ a.compressed = 64;
+ blake2s_round(&a, 0);
+ memset(a.buffer, 0, bs);
+ a.bufflen = 0;
+ }
+ return a;
}
struct blake2s_hash blake2s_init_l(lua_State* L, char* key, int key_len, int digest_len){
- struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
- a.buffer = calloc(sizeof * a.buffer, bs);
- a.hash = calloc(sizeof * a.hash, 8);
- a.key = calloc(sizeof* a.key, key_len);
- memcpy(a.key, key, key_len);
+ struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ a.hash = calloc(sizeof * a.hash, 8);
+ a.key = calloc(sizeof* a.key, key_len);
+ memcpy(a.key, key, key_len);
+ memset(a.buffer, 0, bs);
+ memcpy(a.buffer, key, key_len);
+
+ a.hash[0] = sha512_iv.h0 >> 32;
+ a.hash[0] ^= digest_len;
+ a.hash[0] ^= key_len << 8;
+ a.hash[0] ^= 0x01010000;
+ a.hash[1] = sha512_iv.h1 >> 32;
+ a.hash[2] = sha512_iv.h2 >> 32;
+ a.hash[3] = sha512_iv.h3 >> 32;
+ a.hash[4] = sha512_iv.h4 >> 32;
+ a.hash[5] = sha512_iv.h5 >> 32;
+ a.hash[6] = sha512_iv.h6 >> 32;
+ a.hash[7] = sha512_iv.h7 >> 32;
+ if(key_len != 0){
+ a.compressed = 64; //TODO: allow for keys larger than 64 chars
+ blake2s_round(&a, 0);
memset(a.buffer, 0, bs);
- memcpy(a.buffer, key, key_len);
-
- a.hash[0] = sha512_iv.h0 >> 32;
- a.hash[0] ^= digest_len;
- a.hash[0] ^= key_len << 8;
- a.hash[0] ^= 0x01010000;
- a.hash[1] = sha512_iv.h1 >> 32;
- a.hash[2] = sha512_iv.h2 >> 32;
- a.hash[3] = sha512_iv.h3 >> 32;
- a.hash[4] = sha512_iv.h4 >> 32;
- a.hash[5] = sha512_iv.h5 >> 32;
- a.hash[6] = sha512_iv.h6 >> 32;
- a.hash[7] = sha512_iv.h7 >> 32;
- if(key_len != 0){
- a.compressed = 64; //TODO: allow for keys larger than 64 chars
- blake2s_round(&a, 0);
- memset(a.buffer, 0, bs);
- a.bufflen = 0;
- }
- return a;
+ a.bufflen = 0;
+ }
+ return a;
}
void blake2s_update(uint8_t* input, size_t len, struct blake2s_hash* hash){
- hash->total += len;
- size_t total_add = len + hash->bufflen;
- size_t read = 0;
- if(total_add < bs){
- memcpy(hash->buffer + hash->bufflen, input, len);
- hash->bufflen += len;
- return;
- }
+ hash->total += len;
+ size_t total_add = len + hash->bufflen;
+ size_t read = 0;
+ if(total_add < bs){
+ memcpy(hash->buffer + hash->bufflen, input, len);
+ hash->bufflen += len;
+ return;
+ }
- for(; total_add >= bs;){
- memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
- total_add -= bs;
- hash->bufflen = 0;
- read += bs;
- hash->compressed += 64;
- blake2s_round(hash, 0);
- }
+ for(; total_add >= bs;){
+ memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
+ total_add -= bs;
+ hash->bufflen = 0;
+ read += bs;
+ hash->compressed += 64;
+ blake2s_round(hash, 0);
+ }
- memset(hash->buffer, 0, bs);
+ memset(hash->buffer, 0, bs);
- if(0 != total_add){
- memcpy(hash->buffer, input + read, total_add);
- hash->bufflen = total_add;
- }
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
}
void blake2s_final(struct blake2s_hash* hash, char* out_stream){
@@ -364,7 +364,7 @@ void blake2s_final(struct blake2s_hash* hash, char* out_stream){ memcpy(old, hash->buffer, bs);
memcpy(hashh, hash->hash, 8 * sizeof * hashh);
- hash->compressed += hash->bufflen;
+ hash->compressed += hash->bufflen;
if(hash->bufflen > 55) {
//too large, needs another buffer
@@ -377,19 +377,19 @@ void blake2s_final(struct blake2s_hash* hash, char* out_stream){ blake2s_round(hash, 1);
for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i]));
-
+
memcpy(hash, &old_hash, sizeof * hash);
memcpy(hash->buffer, old, bs);
memcpy(hash->hash, hashh, 8 * sizeof * hashh);
}
void blake2s(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* out){
- struct blake2s_hash aa = blake2s_init(key, key_len, dig_len);
- blake2s_update(inp, len, &aa);
- blake2s_final(&aa, out);
- free(aa.buffer);
- free(aa.hash);
- free(aa.key);
+ struct blake2s_hash aa = blake2s_init(key, key_len, dig_len);
+ blake2s_update(inp, len, &aa);
+ blake2s_final(&aa, out);
+ free(aa.buffer);
+ free(aa.hash);
+ free(aa.key);
}
int l_blake2b_clone(lua_State* L){
@@ -404,7 +404,7 @@ int l_blake2b_clone(lua_State* L){ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-
+
b->keylen = a->keylen;
b->digest_len = a->digest_len;
b->keylen = a->keylen;
@@ -422,8 +422,8 @@ int l_blake2b_init(lua_State* L){ size_t keylen = 0, outlen = 64;
if(lua_gettop(L) > 1){
- key = (char*)luaL_checklstring(L, -1, &keylen);
- outlen = luaL_checkinteger(L, -2);
+ key = (char*)luaL_checklstring(L, -1, &keylen);
+ outlen = luaL_checkinteger(L, -2);
} else if(lua_gettop(L) > 0) outlen = luaL_checkinteger(L, -1);
struct blake2b_hash* a = (struct blake2b_hash*)lua_newuserdata(L, sizeof * a);
@@ -448,25 +448,25 @@ int l_blake2b_final(lua_State* L){ }
int l_blake2b(lua_State* L){
- if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2b_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- int argv = lua_gettop(L);
+ if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2b_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+
+ uint32_t out_len = 64;
+ if(argv > 1) out_len = luaL_checkinteger(L, 2);
- uint32_t out_len = 64;
- if(argv > 1) out_len = luaL_checkinteger(L, 2);
+ char* key = NULL;
+ size_t key_len = 0;
+ if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len);
- char* key = NULL;
- size_t key_len = 0;
- if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len);
-
- char digest[out_len * 8];
- memset(digest, 0, out_len * 8);
+ char digest[out_len * 8];
+ memset(digest, 0, out_len * 8);
- blake2b(a, len, key, key_len, out_len, digest);
- lua_pushstring(L, digest);
+ blake2b(a, len, key, key_len, out_len, digest);
+ lua_pushstring(L, digest);
- return 1;
+ return 1;
}
int l_blake2s_clone(lua_State* L){
@@ -481,7 +481,7 @@ int l_blake2s_clone(lua_State* L){ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-
+
b->keylen = a->keylen;
b->digest_len = a->digest_len;
b->keylen = a->keylen;
@@ -499,8 +499,8 @@ int l_blake2s_init(lua_State* L){ size_t keylen = 0, outlen = 32;
if(lua_gettop(L) > 1){
- key = (char*)luaL_checklstring(L, -1, &keylen);
- outlen = luaL_checkinteger(L, -2);
+ key = (char*)luaL_checklstring(L, -1, &keylen);
+ outlen = luaL_checkinteger(L, -2);
} else if(lua_gettop(L) > 0) outlen = luaL_checkinteger(L, -1);
struct blake2s_hash* a = (struct blake2s_hash*)lua_newuserdata(L, sizeof * a);
@@ -525,23 +525,23 @@ int l_blake2s_final(lua_State* L){ }
int l_blake2s(lua_State* L){
- if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2s_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- int argv = lua_gettop(L);
+ if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2s_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
- uint32_t out_len = 32;
- if(argv > 1) out_len = luaL_checkinteger(L, 2);
+ uint32_t out_len = 32;
+ if(argv > 1) out_len = luaL_checkinteger(L, 2);
- char* key = NULL;
- size_t key_len = 0;
- if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len);
-
- char digest[out_len * 8];
- memset(digest, 0, out_len * 8);
+ char* key = NULL;
+ size_t key_len = 0;
+ if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len);
- blake2s(a, len, key, key_len, out_len, digest);
- lua_pushstring(L, digest);
+ char digest[out_len * 8];
+ memset(digest, 0, out_len * 8);
- return 1;
+ blake2s(a, len, key, key_len, out_len, digest);
+ lua_pushstring(L, digest);
+
+ return 1;
}
diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c index 18bae0c..4fb0b90 100644 --- a/src/hash/bsdchecksum.c +++ b/src/hash/bsdchecksum.c @@ -12,10 +12,10 @@ int bsdchecksum_free_l(lua_State* L){ 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;
+ uint8_t a = aa[i];
+ hash->check = (hash->check >> 1) + ((hash->check & 1) << 15);
+ hash->check += a;
+ hash->check &= 0xffff;
}
}
@@ -46,7 +46,7 @@ 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);
diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c index 4016110..bb35f73 100644 --- a/src/hash/buzhash.c +++ b/src/hash/buzhash.c @@ -20,28 +20,28 @@ static uint8_t T[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, 252,253,254,255}; uint8_t buzhash8(uint8_t* in, size_t len){ - uint8_t hash = 0; + uint8_t hash = 0; - for(int i = 0; i != len; i++){ - hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1)); - } + for(int i = 0; i != len; i++){ + hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1)); + } - return hash; + return hash; } uint16_t buzhash16(uint8_t* in, size_t len){ - uint16_t hash = 0; + uint16_t hash = 0; - for(int i = 0; i != len; i++){ - hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1)); - } + for(int i = 0; i != len; i++){ + hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1)); + } - return hash; + return hash; } int l_setbuzhash(lua_State* L){ luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_objlen(L,1); - + if(len != 256) { p_error("new table must have a length of 256"); exit(0); @@ -51,7 +51,7 @@ int l_setbuzhash(lua_State* L){ lua_pushinteger(L,i+1); lua_gettable(L,1); - + T[i] = luaL_checknumber(L, -1); lua_pop(L,1); } @@ -64,7 +64,7 @@ int l_buzhash8(lua_State* L){ char digest[3]; uint8_t u = buzhash8(a, len); - + sprintf(digest,"%x",u); lua_pushstring(L, digest); @@ -78,7 +78,7 @@ int l_buzhash16(lua_State* L){ char digest[6]; uint16_t u = buzhash16(a, len); - + sprintf(digest,"%04x",u); lua_pushstring(L, digest); diff --git a/src/hash/crc.c b/src/hash/crc.c index e1433bd..c3be7f3 100644 --- a/src/hash/crc.c +++ b/src/hash/crc.c @@ -9,81 +9,81 @@ int crc16_free_l(lua_State* L){return 0;} int crc8_free_l(lua_State* L){return 0;}
struct crc32_hash crc32_init(){
- return (struct crc32_hash){.crc = 0xFFFFFFFF};
+ 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^hash->crc)&1;
- hash->crc>>=1;
- if(b) hash->crc^=0xEDB88320;
- extract>>=1;
- }
+ for(int i = 0; i < len; i++){
+ uint32_t extract = data[i];
+ for(int z = 0; z < 8; z++){
+ uint32_t b = (extract^hash->crc)&1;
+ hash->crc>>=1;
+ if(b) hash->crc^=0xEDB88320;
+ extract>>=1;
}
+ }
}
uint32_t crc32_final(struct crc32_hash* hash){
- return -(hash->crc+1);
+ 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 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};
+ 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];
- hash->crc ^= a;
- for (int z = 0; z < 8; z++){
- if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001;
- else hash->crc = (hash->crc >> 1);
- }
+ for(int i = 0; i != len; i++){
+ uint8_t a = aa[i];
+ hash->crc ^= a;
+ for (int z = 0; z < 8; z++){
+ if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001;
+ else hash->crc = (hash->crc >> 1);
}
+ }
}
uint16_t crc16_final(struct crc16_hash *hash){
- return hash->crc;
+ 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 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};
+ 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 = (hash->crc ^ a) & 1;
- hash->crc >>= 1;
- if(b) hash->crc ^= 0x8c;
- a >>=1;
- }
+ for(int i = 0; i != len; i++){
+ uint8_t a = aa[i];
+
+ for (int z = 0; z < 8; z++){
+ uint8_t b = (hash->crc ^ a) & 1;
+ hash->crc >>= 1;
+ if(b) hash->crc ^= 0x8c;
+ a >>=1;
}
+ }
}
uint8_t crc8_final(struct crc8_hash *hash){
- return hash->crc;
+ 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);
+ struct crc8_hash a = crc8_init();
+ crc8_update(aa, len, &a);
+ return crc8_final(&a);
}
common_hash_clone(crc32);
@@ -125,7 +125,7 @@ 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 = crc8(a, len);
@@ -139,7 +139,7 @@ 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 = crc16(a, len);
@@ -153,7 +153,7 @@ 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 = crc32(a, len);
diff --git a/src/hash/crc.h b/src/hash/crc.h index b3fdbf9..233debd 100644 --- a/src/hash/crc.h +++ b/src/hash/crc.h @@ -2,15 +2,15 @@ #include <stdint.h> struct crc32_hash { - uint32_t crc; + uint32_t crc; }; struct crc16_hash { - uint16_t crc; + uint16_t crc; }; struct crc8_hash { - uint8_t crc; + uint8_t crc; }; uint8_t crc8(uint8_t*, size_t len); diff --git a/src/hash/djb2.c b/src/hash/djb2.c index e75a0ed..a001e8f 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -45,7 +45,7 @@ int l_djb2(lua_State* L){ if(lua_gettop(L) == 0) return l_djb2_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
-
+
char digest[64];
uint32_t u = djb2(a, len);
diff --git a/src/hash/djb2.h b/src/hash/djb2.h index d944d11..5c6fe19 100644 --- a/src/hash/djb2.h +++ b/src/hash/djb2.h @@ -2,7 +2,7 @@ #include <stdint.h> struct djb2_hash { - uint32_t hash; + uint32_t hash; }; struct djb2_hash djb2_init(); diff --git a/src/hash/fletcher.c b/src/hash/fletcher.c index bfd3e7c..930d07b 100644 --- a/src/hash/fletcher.c +++ b/src/hash/fletcher.c @@ -12,8 +12,8 @@ struct fletcher8_hash fletcher8_init(){ void fletcher8_update(uint8_t *aa, size_t len, struct fletcher8_hash *hash){
for(int i = 0; i != len; i++){
- hash->s1 = (hash->s1 + aa[i]) % 15;
- hash->s2 = (hash->s2 + hash->s1) % 15;
+ hash->s1 = (hash->s1 + aa[i]) % 15;
+ hash->s2 = (hash->s2 + hash->s1) % 15;
}
}
@@ -33,8 +33,8 @@ struct fletcher16_hash fletcher16_init(){ void fletcher16_update(uint8_t *aa, size_t len, struct fletcher16_hash *hash){
for(int i = 0; i != len; i++){
- hash->s1 = (hash->s1 + aa[i]) % 255;
- hash->s2 = (hash->s2 + hash->s1) % 255;
+ hash->s1 = (hash->s1 + aa[i]) % 255;
+ hash->s2 = (hash->s2 + hash->s1) % 255;
}
}
@@ -54,8 +54,8 @@ struct fletcher32_hash fletcher32_init(){ void fletcher32_update(uint8_t *aa, size_t len, struct fletcher32_hash *hash){
for(int i = 0; i != len; i++){
- hash->s1 = (hash->s1 + aa[i]) % 65535;
- hash->s2 = (hash->s2 + hash->s1) % 65535;
+ hash->s1 = (hash->s1 + aa[i]) % 65535;
+ hash->s2 = (hash->s2 + hash->s1) % 65535;
}
}
@@ -108,7 +108,7 @@ int l_fletcher32(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher32_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
-
+
char digest[32];
uint32_t u = fletcher32(a, len);
@@ -122,7 +122,7 @@ int l_fletcher16(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher16_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
-
+
char digest[16];
uint16_t u = fletcher16(a, len);
@@ -136,7 +136,7 @@ int l_fletcher8(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher8_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
-
+
char digest[8];
uint8_t u = fletcher8(a, len);
diff --git a/src/hash/fletcher.h b/src/hash/fletcher.h index d66692f..cae169f 100644 --- a/src/hash/fletcher.h +++ b/src/hash/fletcher.h @@ -2,15 +2,15 @@ #include <stdint.h> struct fletcher8_hash { - uint8_t s1, s2; + uint8_t s1, s2; }; struct fletcher16_hash { - uint16_t s1, s2; + uint16_t s1, s2; }; struct fletcher32_hash { - uint32_t s1, s2; + uint32_t s1, s2; }; uint8_t fletcher8(uint8_t*,size_t); @@ -41,4 +41,4 @@ int l_fletcher16_final(lua_State*); int l_fletcher8(lua_State*); int l_fletcher8_init(lua_State*); int l_fletcher8_update(lua_State*); -int l_fletcher8_final(lua_State*);
\ No newline at end of file +int l_fletcher8_final(lua_State*); diff --git a/src/hash/fnv.c b/src/hash/fnv.c index 34623b7..19fc0a5 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -51,14 +51,14 @@ lua_common_hash_init_ni(fnv_1, fnv_0, fnv_1_init(v_0), fnv_1_free_l); lua_common_hash_init_ni(fnv_1, fnv_a, fnv_1_init(v_a), fnv_1_free_l);
#define aaa(v)\
-int l_fnv_##v##_final(lua_State* L){\
- struct fnv_1_hash* a = (struct fnv_1_hash*)lua_touserdata(L, 1);\
- uint64_t u = fnv_1_final(a);\
- char digest[64];\
- sprintf(digest,"%16"PRIx64,u);\
- lua_pushstring(L, digest);\
- return 1;\
-}
+ int l_fnv_##v##_final(lua_State* L){\
+ struct fnv_1_hash* a = (struct fnv_1_hash*)lua_touserdata(L, 1);\
+ uint64_t u = fnv_1_final(a);\
+ char digest[64];\
+ sprintf(digest,"%16"PRIx64,u);\
+ lua_pushstring(L, digest);\
+ return 1;\
+ }
aaa(0);
aaa(1);
diff --git a/src/hash/fnv.h b/src/hash/fnv.h index a4299c9..5d4491a 100644 --- a/src/hash/fnv.h +++ b/src/hash/fnv.h @@ -2,7 +2,7 @@ #include "stdint.h" enum fnv_version { - v_1, v_a, v_0 + v_1, v_a, v_0 }; struct fnv_1_hash { diff --git a/src/hash/md5.c b/src/hash/md5.c index f98e590..d1d78fd 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -4,20 +4,20 @@ #include <stdint.h>
static const uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf,
- 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
- 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51,
- 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6,
- 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942,
- 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
- 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8,
- 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
- 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
- 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
+ 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
+ 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51,
+ 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6,
+ 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942,
+ 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
+ 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8,
+ 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
+ 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
+ 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
static const uint32_t s[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
- 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
- 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
- 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
+ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
+ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
+ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
#define bs 64
@@ -49,7 +49,7 @@ void md5_round(struct md5_hash* hash){ for(int i = 0; i < 64; i++){
uint32_t F, g;
-
+
if(i < 16){
F = (B & C) | ((~B) & D);
g = i;
@@ -63,7 +63,7 @@ void md5_round(struct md5_hash* hash){ F = C ^ (B | (~D));
g = (7*i) % 16;
}
-
+
F = F + A + K[i] + M[g];
@@ -93,7 +93,7 @@ void md5_update(uint8_t* input, size_t len, struct md5_hash* hash){ for(; total_add >= bs;){
memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
-
+
total_add -= bs;
read += bs;
hash->bufflen = 0;
@@ -125,7 +125,7 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){ uint32_t lhhh = 8*hash->total;
memcpy(hash->buffer + 56, &lhhh, 4);
-
+
md5_round(hash);
sprintf(out_stream,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
@@ -145,7 +145,7 @@ lua_common_hash_clone_oargs(md5, md5, l_md5_init(L), { *b = *a;
b->buffer = old;
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-});
+ });
lua_common_hash_init_ni(md5, md5, md5_init_l(L), l_md5_free);
lua_common_hash_update(md5, md5);
diff --git a/src/hash/murmur.c b/src/hash/murmur.c index d191d75..c7ad143 100644 --- a/src/hash/murmur.c +++ b/src/hash/murmur.c @@ -3,70 +3,70 @@ #include <stdint.h> uint32_t murmur1_32(uint8_t* in, size_t len, uint32_t seed){ - uint32_t m = 0xc6a4a793; - uint32_t hash = seed ^ (len * m); - - for(;len >= 4; len-=4){ - hash+=*(uint32_t*)in; - hash*=m; - hash^=hash >> 16; - - in+=4; - } - - switch(len){ - case 3: - hash+=in[2]<<16; - case 2: - hash+=in[1]<<8; - case 1: - hash+=in[0]; - hash*=m; - hash^=hash>>16; - break; - } + uint32_t m = 0xc6a4a793; + uint32_t hash = seed ^ (len * m); + for(;len >= 4; len-=4){ + hash+=*(uint32_t*)in; hash*=m; - hash^=hash>>10; - hash*=m; - hash^=hash>>17; - - return hash; + hash^=hash >> 16; + + in+=4; + } + + switch(len){ + case 3: + hash+=in[2]<<16; + case 2: + hash+=in[1]<<8; + case 1: + hash+=in[0]; + hash*=m; + hash^=hash>>16; + break; + } + + hash*=m; + hash^=hash>>10; + hash*=m; + hash^=hash>>17; + + return hash; } uint32_t murmur2_32(uint8_t* in, size_t len, uint32_t seed){ - uint32_t m = 0x5bd1e995; - uint32_t hash = seed ^ len; - - for(;len >= 4; len-=4){ - uint32_t k = *(uint32_t*)in; - - k*=m; - k^=k>>24; - k*=m; - - hash*=m; - hash^=k; - - in+=4; - } - - switch(len){ - case 3: - hash+=in[2]<<16; - case 2: - hash+=in[1]<<8; - case 1: - hash+=in[0]; - hash*=m; - break; - } - - hash^=hash>>13; - hash*=m; - hash^=hash>>15; + uint32_t m = 0x5bd1e995; + uint32_t hash = seed ^ len; - return hash; + for(;len >= 4; len-=4){ + uint32_t k = *(uint32_t*)in; + + k*=m; + k^=k>>24; + k*=m; + + hash*=m; + hash^=k; + + in+=4; + } + + switch(len){ + case 3: + hash+=in[2]<<16; + case 2: + hash+=in[1]<<8; + case 1: + hash+=in[0]; + hash*=m; + break; + } + + hash^=hash>>13; + hash*=m; + hash^=hash>>15; + + return hash; } int l_murmur1_32(lua_State* L){ diff --git a/src/hash/pearson.c b/src/hash/pearson.c index d6a5d1b..74546a4 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -45,7 +45,7 @@ uint8_t pearson(uint8_t* aa, size_t len){ int l_setpearson(lua_State* L){
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
-
+
if(len != 256) {
p_error("new table must have a length of 256");
exit(0);
@@ -55,7 +55,7 @@ int l_setpearson(lua_State* L){ lua_pushinteger(L,i+1);
lua_gettable(L,1);
-
+
pearson_table[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
@@ -81,7 +81,7 @@ int l_pearson(lua_State* L){ char digest[8];
uint8_t u = pearson(a, len);
-
+
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/pearson.h b/src/hash/pearson.h index 4cdfda9..c10fea4 100644 --- a/src/hash/pearson.h +++ b/src/hash/pearson.h @@ -2,7 +2,7 @@ #include <stdint.h> struct pearson_hash { - uint8_t ret; + uint8_t ret; }; struct pearson_hash pearson_init(); diff --git a/src/hash/pjw.c b/src/hash/pjw.c index 188030f..93fc7d4 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -4,7 +4,7 @@ #include <stdint.h>
struct pjw_hash pjw_init(){
- return (struct pjw_hash){.hash = 0, .high = 0};
+ return (struct pjw_hash){.hash = 0, .high = 0};
}
int pjw_free_l(lua_State* L){
@@ -12,22 +12,22 @@ int pjw_free_l(lua_State* L){ }
void pjw_update(uint8_t* in, size_t len, struct pjw_hash* hash){
- for(int i = 0; i != len; i++){
- hash->hash = (hash->hash << 4) + *in++;
- if((hash->high = (hash->hash & 0xf0000000)))
- hash->hash ^= hash->high >> 24;
- hash->hash &= ~hash->high;
- }
+ for(int i = 0; i != len; i++){
+ hash->hash = (hash->hash << 4) + *in++;
+ if((hash->high = (hash->hash & 0xf0000000)))
+ hash->hash ^= hash->high >> 24;
+ hash->hash &= ~hash->high;
+ }
}
uint32_t pjw_final(struct pjw_hash* hash){
- return hash->hash;
+ return hash->hash;
}
uint32_t pjw(uint8_t* in, size_t len){
- struct pjw_hash a = pjw_init();
- pjw_update(in, len, &a);
- return pjw_final(&a);
+ struct pjw_hash a = pjw_init();
+ pjw_update(in, len, &a);
+ return pjw_final(&a);
}
common_hash_clone(pjw);
@@ -43,14 +43,14 @@ int l_pjw_final(lua_State* L){ }
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);
+ 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];
+ char digest[32];
- uint32_t u = pjw(a, len);
- sprintf(digest,"%08x",u);
- lua_pushstring(L, digest);
- return 1;
+ 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 a4e36e7..4ac45ce 100644 --- a/src/hash/pjw.h +++ b/src/hash/pjw.h @@ -2,7 +2,7 @@ #include <stdint.h> struct pjw_hash { - uint32_t hash, high; + uint32_t hash, high; }; struct pjw_hash pjw_init(); diff --git a/src/hash/sdbm.h b/src/hash/sdbm.h index a937c92..8d79511 100644 --- a/src/hash/sdbm.h +++ b/src/hash/sdbm.h @@ -2,7 +2,7 @@ #include <stdint.h> struct sdbm_hash { - uint64_t hash; + uint64_t hash; }; struct sdbm_hash sdbm_init(); diff --git a/src/hash/sha01.c b/src/hash/sha01.c index 4d05e7e..aa403a2 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -17,10 +17,10 @@ struct sha01_hash { #define sha1_hash sha01_hash
struct sha01_hash sha01_init(uint8_t ver){
- struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0,
- .total = 0, .bufflen = 0, .version = ver};
- a.buffer = calloc(sizeof * a.buffer, bs);
- return a;
+ struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0,
+ .total = 0, .bufflen = 0, .version = ver};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ return a;
}
int sha01_free_l(lua_State* L){
@@ -30,64 +30,64 @@ int sha01_free_l(lua_State* L){ }
struct sha01_hash sha01_init_l(uint8_t ver, lua_State* L){
- struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0,
- .total = 0, .bufflen = 0, .version = ver};
- a.buffer = calloc(sizeof * a.buffer, bs);
- memset(a.buffer, 0, bs);
- return a;
+ struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0,
+ .total = 0, .bufflen = 0, .version = ver};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ memset(a.buffer, 0, bs);
+ return a;
}
void sha01_round(struct sha01_hash* hash){
- int hat = 0;
- uint32_t W[80] = {0};
-
- for(int i = 0; i != 16; i++){
- int t = 24;
- for(;t>=0;){
- W[i] += (((uint32_t)hash->buffer[hat]) << t);
- hat++;
- t-=8;
- }
+ int hat = 0;
+ uint32_t W[80] = {0};
+
+ for(int i = 0; i != 16; i++){
+ int t = 24;
+ for(;t>=0;){
+ W[i] += (((uint32_t)hash->buffer[hat]) << t);
+ hat++;
+ t-=8;
}
- for(int i = 16; i != 80; i++)
- W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], hash->version);
-
- uint32_t a = hash->h0;
- uint32_t b = hash->h1;
- uint32_t c = hash->h2;
- uint32_t d = hash->h3;
- uint32_t e = hash->h4;
-
- for(int i = 0; i != 80; i++){
-
- uint32_t f,k;
- if(0 <= i && i <= 19){
- f = (b & c) | ((~b) & d);
- k = 0x5A827999;
- } else if(20 <= i && i <= 39){
- f = b ^ c ^ d;
- k = 0x6ED9EBA1;
- } else if(40 <= i && i <= 59){
- f = (b & c) | (b & d) | (c & d);
- k = 0x8F1BBCDC;
- } else {
- f = b ^ c ^ d;
- k = 0xCA62C1D6;
- }
-
- uint32_t temp = rotl32(a, 5) + f + e + k + W[i];
- e = d;
- d = c;
- c = rotl32(b, 30);
- b = a;
- a = temp;
+ }
+ for(int i = 16; i != 80; i++)
+ W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], hash->version);
+
+ uint32_t a = hash->h0;
+ uint32_t b = hash->h1;
+ uint32_t c = hash->h2;
+ uint32_t d = hash->h3;
+ uint32_t e = hash->h4;
+
+ for(int i = 0; i != 80; i++){
+
+ uint32_t f,k;
+ if(0 <= i && i <= 19){
+ f = (b & c) | ((~b) & d);
+ k = 0x5A827999;
+ } else if(20 <= i && i <= 39){
+ f = b ^ c ^ d;
+ k = 0x6ED9EBA1;
+ } else if(40 <= i && i <= 59){
+ f = (b & c) | (b & d) | (c & d);
+ k = 0x8F1BBCDC;
+ } else {
+ f = b ^ c ^ d;
+ k = 0xCA62C1D6;
}
-
- hash->h0 += a;
- hash->h1 += b;
- hash->h2 += c;
- hash->h3 += d;
- hash->h4 += e;
+
+ uint32_t temp = rotl32(a, 5) + f + e + k + W[i];
+ e = d;
+ d = c;
+ c = rotl32(b, 30);
+ b = a;
+ a = temp;
+ }
+
+ hash->h0 += a;
+ hash->h1 += b;
+ hash->h2 += c;
+ hash->h3 += d;
+ hash->h4 += e;
}
void sha01_update(uint8_t* input, size_t len, struct sha01_hash* hash){
@@ -133,7 +133,7 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){ size_t lhhh = 8*hash->total;
for(int i = 0; i != 8; i++)
- hash->buffer[63 - i] = (uint8_t) (lhhh >> (i * 8) & 0xFF);
+ hash->buffer[63 - i] = (uint8_t) (lhhh >> (i * 8) & 0xFF);
sha01_round(hash);
sprintf(out_stream,"%02x%02x%02x%02x%02x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4);
@@ -143,41 +143,41 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){ }
struct sha01_hash sha0_init(){
- return sha01_init(0);
+ return sha01_init(0);
}
struct sha01_hash sha1_init(){
- return sha01_init(1);
+ return sha01_init(1);
}
void sha0_update(uint8_t* input, size_t len, struct sha01_hash* hash){
- sha01_update(input, len, hash);
+ sha01_update(input, len, hash);
}
void sha1_update(uint8_t* input, size_t len, struct sha01_hash* hash){
- sha01_update(input, len, hash);
+ sha01_update(input, len, hash);
}
void sha0_final(struct sha01_hash* hash, char* out_stream){
- sha01_final(hash, out_stream);
+ sha01_final(hash, out_stream);
}
void sha1_final(struct sha01_hash* hash, char* out_stream){
- sha01_final(hash, out_stream);
+ sha01_final(hash, out_stream);
}
void sha0(uint8_t* a, size_t len, char* out_stream){
- struct sha01_hash aa = sha0_init();
- sha0_update(a, len, &aa);
- sha0_final(&aa, out_stream);
- free(aa.buffer);
+ struct sha01_hash aa = sha0_init();
+ sha0_update(a, len, &aa);
+ sha0_final(&aa, out_stream);
+ free(aa.buffer);
}
void sha1(uint8_t* a, size_t len, char* out_stream){
- struct sha01_hash aa = sha1_init();
- sha1_update(a, len, &aa);
- sha1_final(&aa, out_stream);
- free(aa.buffer);
+ struct sha01_hash aa = sha1_init();
+ sha1_update(a, len, &aa);
+ sha1_final(&aa, out_stream);
+ free(aa.buffer);
}
//common_hash_clone(sha1);
@@ -186,7 +186,7 @@ lua_common_hash_clone_oargs(sha1, sha1, l_sha1_init(L), { *b = *a;
b->buffer = old;
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-});
+ });
lua_common_hash_init_ni(sha1, sha1, sha01_init_l(1, L), sha01_free_l);
lua_common_hash_update(sha1, sha1);
@@ -197,7 +197,7 @@ lua_common_hash_clone_oargs(sha0, sha0, l_sha0_init(L), { *b = *a;
b->buffer = old;
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-});
+ });
lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L), sha01_free_l);
lua_common_hash_update(sha0, sha0);
@@ -212,7 +212,7 @@ int l_sha1_final(lua_State* L){ }
int l_sha0_final(lua_State* L){
- return l_sha1_final(L);
+ return l_sha1_final(L);
}
int l_sha1(lua_State* L){
@@ -232,7 +232,7 @@ int l_sha0(lua_State* L){ if(lua_gettop(L) == 0) return l_sha0_init(L);
size_t len = 0;
char* a = (char*)luaL_checklstring(L, 1, &len);
-
+
char digest[160];
sha0((uint8_t*)a, len, digest);
diff --git a/src/hash/sha01.h b/src/hash/sha01.h index f70a3e0..bb343c2 100644 --- a/src/hash/sha01.h +++ b/src/hash/sha01.h @@ -8,7 +8,7 @@ * @param {char*} output stream * @param {const char*} input bytes * @return {void} -*/ + */ void i_sha01(uint8_t, char*, int, const char*); int l_sha1(lua_State*); diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 3c33948..06841c5 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -6,106 +6,106 @@ #include <inttypes.h>
const uint64_t k[80] = {0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
- 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
- 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
- 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
- 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
- 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
- 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
- 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
- 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
- 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
- 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
- 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
- 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
- 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
- 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
- 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
+ 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
+ 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
+ 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
+ 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
+ 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
+ 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
+ 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
+ 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
+ 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
+ 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
+ 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
+ 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
+ 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
+ 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
+ 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
void endian_swap128(__uint128_t *x){
- uint8_t *y = (uint8_t*)x;
- for (size_t low = 0, high = sizeof(__uint128_t) - 1; high > low; low++, high--){
- y[low] ^= y[high];
- y[high] ^= y[low];
- y[low] ^= y[high];
- }
+ uint8_t *y = (uint8_t*)x;
+ for (size_t low = 0, high = sizeof(__uint128_t) - 1; high > low; low++, high--){
+ y[low] ^= y[high];
+ y[high] ^= y[low];
+ y[low] ^= y[high];
+ }
}
void endian_swap64(uint64_t *x){
- uint8_t *y = (uint8_t*)x;
- for (size_t low = 0, high = sizeof(uint64_t) - 1; high > low; low++, high--){
- y[low] ^= y[high];
- y[high] ^= y[low];
- y[low] ^= y[high];
- }
+ uint8_t *y = (uint8_t*)x;
+ for (size_t low = 0, high = sizeof(uint64_t) - 1; high > low; low++, high--){
+ y[low] ^= y[high];
+ y[high] ^= y[low];
+ y[low] ^= y[high];
+ }
}
#define bs 128
void sha512_round(struct sha512_hash* hash){
- uint64_t *msg = ((uint64_t*)&hash->buffer[0]);
- for(int i = 0; i < 16; i++)
- endian_swap64(msg++);
-
- uint64_t* M = ((uint64_t*)(hash->buffer));
- uint64_t W[80];
-
- //i dont really understand this 0->16 part
- int z = 0;
- uint64_t *m = &M[(z * 16)];
- for(int i = 0; i < 16; ++i){
- W[i] = *m;
- m++;
- }
-
- for(int i = 16; i != 80; i++){
- W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6))
- + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16];
- }
-
- uint64_t a = hash->h0;
- uint64_t b = hash->h1;
- uint64_t c = hash->h2;
- uint64_t d = hash->h3;
- uint64_t e = hash->h4;
- uint64_t f = hash->h5;
- uint64_t g = hash->h6;
- uint64_t h = hash->h7;
-
- for(int i = 0; i != 80; i++){
- uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41);
- uint64_t ch = (e & f) ^ ((~e) & g);
- uint64_t temp1 = h + S1 + ch + k[i] + W[i];
-
- uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39);
- uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
- uint64_t temp2 = S0 + maj;
-
- h = g;
- g = f;
- f = e;
- e = d + temp1;
- d = c;
- c = b;
- b = a;
- a = temp1 + temp2;
- }
-
- hash->h0 += a;
- hash->h1 += b;
- hash->h2 += c;
- hash->h3 += d;
- hash->h4 += e;
- hash->h5 += f;
- hash->h6 += g;
- hash->h7 += h;
+ uint64_t *msg = ((uint64_t*)&hash->buffer[0]);
+ for(int i = 0; i < 16; i++)
+ endian_swap64(msg++);
+
+ uint64_t* M = ((uint64_t*)(hash->buffer));
+ uint64_t W[80];
+
+ //i dont really understand this 0->16 part
+ int z = 0;
+ uint64_t *m = &M[(z * 16)];
+ for(int i = 0; i < 16; ++i){
+ W[i] = *m;
+ m++;
+ }
+
+ for(int i = 16; i != 80; i++){
+ W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6))
+ + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16];
+ }
+
+ uint64_t a = hash->h0;
+ uint64_t b = hash->h1;
+ uint64_t c = hash->h2;
+ uint64_t d = hash->h3;
+ uint64_t e = hash->h4;
+ uint64_t f = hash->h5;
+ uint64_t g = hash->h6;
+ uint64_t h = hash->h7;
+
+ for(int i = 0; i != 80; i++){
+ uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41);
+ uint64_t ch = (e & f) ^ ((~e) & g);
+ uint64_t temp1 = h + S1 + ch + k[i] + W[i];
+
+ uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39);
+ uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
+ uint64_t temp2 = S0 + maj;
+
+ h = g;
+ g = f;
+ f = e;
+ e = d + temp1;
+ d = c;
+ c = b;
+ b = a;
+ a = temp1 + temp2;
+ }
+
+ hash->h0 += a;
+ hash->h1 += b;
+ hash->h2 += c;
+ hash->h3 += d;
+ hash->h4 += e;
+ hash->h5 += f;
+ hash->h6 += g;
+ hash->h7 += h;
}
struct sha512_hash sha512_t_init(struct iv sha_iv){
- struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7,
- .total = 0, .bufflen = 0};
- a.buffer = calloc(sizeof * a.buffer, bs);
- return a;
+ struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7,
+ .total = 0, .bufflen = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ return a;
}
int sha512_t_free_l(lua_State* L){
@@ -115,19 +115,19 @@ int sha512_t_free_l(lua_State* L){ }
struct sha512_hash sha512_t_init_l(struct iv sha_iv, lua_State* L){
- struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7,
- .total = 0, .bufflen = 0};
- a.buffer = calloc((sizeof * a.buffer), bs);
- memset(a.buffer, 0, bs);
- return a;
+ struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7,
+ .total = 0, .bufflen = 0};
+ a.buffer = calloc((sizeof * a.buffer), bs);
+ memset(a.buffer, 0, bs);
+ return a;
}
struct sha512_hash sha512_init(){
- return sha512_t_init(sha512_iv);
+ return sha512_t_init(sha512_iv);
}
struct sha512_hash sha384_init(){
- return sha512_t_init(sha384_iv);
+ return sha512_t_init(sha384_iv);
}
char old[512];
@@ -167,11 +167,11 @@ void _sha512_t_final(struct sha512_hash* hash){ sha512_round(hash);
memset(hash->buffer, 0, bs);
}
-
+
__uint128_t bigL = hash->total*8;
endian_swap128(&bigL);
memcpy(&hash->buffer[128 - sizeof(__uint128_t)], &bigL, sizeof(__uint128_t));
-
+
sha512_round(hash);
}
@@ -186,12 +186,12 @@ void sha512_final(struct sha512_hash* hash, char* out_stream){ sprintf((char*)out_stream, "%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64
, hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5, hash->h6, hash->h7);
/*sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h6);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);*/
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h6);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);*/
memcpy(hash, &old_hash, sizeof * hash);
memcpy(hash->buffer, old, bs);
@@ -207,49 +207,49 @@ void sha384_final(struct sha512_hash* hash, char* out_stream){ sprintf((char*)out_stream, "%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64, hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5);
/*sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
- sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);*/
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4);
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);*/
memcpy(hash, &old_hash, sizeof * hash);
memcpy(hash->buffer, old, bs);
}
void sha512(uint8_t* in, size_t len, char* out){
- struct sha512_hash a = sha512_init();
- sha512_update(in, len, &a);
- sha512_final(&a, out);
- free(a.buffer);
+ struct sha512_hash a = sha512_init();
+ sha512_update(in, len, &a);
+ sha512_final(&a, out);
+ free(a.buffer);
}
void sha384(uint8_t* in, size_t len, char* out){
- struct sha512_hash a = sha384_init();
- sha384_update(in, len, &a);
- sha384_final(&a, out);
- free(a.buffer);
+ struct sha512_hash a = sha384_init();
+ sha384_update(in, len, &a);
+ sha384_final(&a, out);
+ free(a.buffer);
}
void sha512_t(uint8_t* in, size_t len, int t, char* out){
- struct sha512_hash a = sha512_t_init(sha_iv_gen(t));
- sha512_update(in, len, &a);
- sha512_final(&a, out);
- out[t/4] = '\0';
- free(a.buffer);
+ struct sha512_hash a = sha512_t_init(sha_iv_gen(t));
+ sha512_update(in, len, &a);
+ sha512_final(&a, out);
+ out[t/4] = '\0';
+ free(a.buffer);
}
struct iv sha_iv_gen(int i){
- struct iv oh = {.h0 = sha512_iv.h0 ^ 0xa5a5a5a5a5a5a5a5, .h1 = sha512_iv.h1 ^ 0xa5a5a5a5a5a5a5a5, .h2 = sha512_iv.h2 ^ 0xa5a5a5a5a5a5a5a5,
- .h3 = sha512_iv.h3 ^ 0xa5a5a5a5a5a5a5a5, .h4 = sha512_iv.h4 ^ 0xa5a5a5a5a5a5a5a5, .h5 = sha512_iv.h5 ^ 0xa5a5a5a5a5a5a5a5,
- .h6 = sha512_iv.h6 ^ 0xa5a5a5a5a5a5a5a5, .h7 = sha512_iv.h7 ^ 0xa5a5a5a5a5a5a5a5};
-
- uint8_t in[12];
- sprintf((char*)in, "SHA-512/%i", i);
- struct sha512_hash a = sha512_t_init(oh);
- sha512_update(in, strlen((char*)in), &a);
- _sha512_t_final(&a);
- free(a.buffer);
- return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7};
+ struct iv oh = {.h0 = sha512_iv.h0 ^ 0xa5a5a5a5a5a5a5a5, .h1 = sha512_iv.h1 ^ 0xa5a5a5a5a5a5a5a5, .h2 = sha512_iv.h2 ^ 0xa5a5a5a5a5a5a5a5,
+ .h3 = sha512_iv.h3 ^ 0xa5a5a5a5a5a5a5a5, .h4 = sha512_iv.h4 ^ 0xa5a5a5a5a5a5a5a5, .h5 = sha512_iv.h5 ^ 0xa5a5a5a5a5a5a5a5,
+ .h6 = sha512_iv.h6 ^ 0xa5a5a5a5a5a5a5a5, .h7 = sha512_iv.h7 ^ 0xa5a5a5a5a5a5a5a5};
+
+ uint8_t in[12];
+ sprintf((char*)in, "SHA-512/%i", i);
+ struct sha512_hash a = sha512_t_init(oh);
+ sha512_update(in, strlen((char*)in), &a);
+ _sha512_t_final(&a);
+ free(a.buffer);
+ return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7};
}
//common_hash_clone(sha512);
@@ -258,7 +258,7 @@ lua_common_hash_clone_oargs(sha512, sha512, l_sha512_init(L), { *b = *a;
b->buffer = old;
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-});
+ });
lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L), sha512_t_free_l);
lua_common_hash_update(sha512, sha512);
@@ -278,7 +278,7 @@ lua_common_hash_clone_oargs(sha384, sha384, l_sha384_init(L), { *b = *a;
b->buffer = old;
memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
-});
+ });
lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L), sha512_t_free_l);
lua_common_hash_update(sha384, sha384);
@@ -309,12 +309,12 @@ lua_common_hash_meta(sha512_t); int l_sha512_t_init(lua_State* L){
int tt = luaL_checkinteger(L, -1);
lua_newtable(L);
-
+
struct sha512_hash* a = (struct sha512_hash*)lua_newuserdata(L, sizeof * a);\
- int ud = lua_gettop(L);
+ int ud = lua_gettop(L);
*a = sha512_t_init_l(sha_iv_gen(tt), L);
a->t = tt;
-
+
lua_common_hash_meta_def(sha512_t, sha512_t_free_l);
lua_pushvalue(L, ud);
@@ -335,26 +335,26 @@ int l_sha512_t_final(lua_State* L){ }
int l_sha512(lua_State* L){
- if(lua_gettop(L) == 0) return l_sha512_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[512] = {0};
-
- sha512(a, len, digest);
- lua_pushstring(L, digest);
- return 1;
+ if(lua_gettop(L) == 0) return l_sha512_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ char digest[512] = {0};
+
+ sha512(a, len, digest);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_sha384(lua_State* L){
- if(lua_gettop(L) == 0) return l_sha384_init(L);
- size_t len = 0;
- uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ if(lua_gettop(L) == 0) return l_sha384_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
- char digest[384] = {0};
+ char digest[384] = {0};
- sha384(a, len, digest);
- lua_pushstring(L, digest);
- return 1;
+ sha384(a, len, digest);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_sha512_t(lua_State* L){
diff --git a/src/hash/sha2-256.h b/src/hash/sha2-256.h index 524693d..3a29b7c 100644 --- a/src/hash/sha2-256.h +++ b/src/hash/sha2-256.h @@ -2,23 +2,23 @@ #include <stdint.h> struct iv { - uint64_t h0, h1, h2, h3, h4, h5, h6, h7; + uint64_t h0, h1, h2, h3, h4, h5, h6, h7; }; static const struct iv sha512_iv = {.h0 = 0x6a09e667f3bcc908, .h1 = 0xbb67ae8584caa73b, .h2 = 0x3c6ef372fe94f82b, .h3 = 0xa54ff53a5f1d36f1, - .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; + .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; static const struct iv sha384_iv = {.h0 = 0xcbbb9d5dc1059ed8, .h1 = 0x629a292a367cd507, .h2 = 0x9159015a3070dd17, .h3 = 0x152fecd8f70e5939, - .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; + .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; #define sha384_update sha512_update #define sha384_hash sha512_hash struct sha512_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total, h0, h1, h2, h3, h4, h5, h6, h7; - uint16_t t; + uint8_t* buffer; + size_t bufflen; + uint64_t total, h0, h1, h2, h3, h4, h5, h6, h7; + uint16_t t; }; struct iv sha_iv_gen(int i); diff --git a/src/hash/sha2xx.c b/src/hash/sha2xx.c index c67e40d..531967e 100644 --- a/src/hash/sha2xx.c +++ b/src/hash/sha2xx.c @@ -6,10 +6,10 @@ #define bs 64 struct sha256_hash sha256_init(){ - struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, - .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - return a; + struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, + .total = 0, .bufflen = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + return a; } int sha256_free_l(lua_State* L){ @@ -21,96 +21,96 @@ int sha256_free_l(lua_State* L){ #define sha224_free_l sha256_free_l struct sha256_hash sha256_init_l(lua_State* L){ - struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, - .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - memset(a.buffer, 0, bs); - return a; + struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, + .total = 0, .bufflen = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + memset(a.buffer, 0, bs); + return a; } struct sha256_hash sha224_init(){ - struct sha256_hash a = sha256_init(); - a.h0 = 0xc1059ed8; - a.h1 = 0x367cd507; - a.h2 = 0x3070dd17; - a.h3 = 0xf70e5939; - a.h4 = 0xffc00b31; - a.h5 = 0x68581511; - a.h6 = 0x64f98fa7; - a.h7 = 0xbefa4fa4; - return a; + struct sha256_hash a = sha256_init(); + a.h0 = 0xc1059ed8; + a.h1 = 0x367cd507; + a.h2 = 0x3070dd17; + a.h3 = 0xf70e5939; + a.h4 = 0xffc00b31; + a.h5 = 0x68581511; + a.h6 = 0x64f98fa7; + a.h7 = 0xbefa4fa4; + return a; } struct sha256_hash sha224_init_l(lua_State* L){ - struct sha256_hash a = sha256_init_l(L); - a.h0 = 0xc1059ed8; - a.h1 = 0x367cd507; - a.h2 = 0x3070dd17; - a.h3 = 0xf70e5939; - a.h4 = 0xffc00b31; - a.h5 = 0x68581511; - a.h6 = 0x64f98fa7; - a.h7 = 0xbefa4fa4; - return a; + struct sha256_hash a = sha256_init_l(L); + a.h0 = 0xc1059ed8; + a.h1 = 0x367cd507; + a.h2 = 0x3070dd17; + a.h3 = 0xf70e5939; + a.h4 = 0xffc00b31; + a.h5 = 0x68581511; + a.h6 = 0x64f98fa7; + a.h7 = 0xbefa4fa4; + return a; } void sha256_round(struct sha256_hash* hash){ - const uint32_t k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - uint32_t W[64]; - memset(W, 0, 64 * sizeof (uint32_t)); - - for (int i = 0; i < 16; i++) - W[i] = (hash->buffer[i * 4] << 24) | (hash->buffer[i * 4 + 1] << 16) | (hash->buffer[i * 4 + 2] << 8) | (hash->buffer[i * 4 + 3]); - - for(int i = 16; i != 64; i++){ - uint32_t s0 = rotr32(W[i - 15], 7) ^ rotr32(W[i - 15], 18) ^ (W[i - 15] >> 3); - uint32_t s1 = rotr32(W[i - 2], 17) ^ rotr32(W[i - 2], 19) ^ (W[i - 2] >> 10); - W[i] = W[i - 16] + s0 + W[i - 7] + s1; - } - - uint32_t a = hash->h0; - uint32_t b = hash->h1; - uint32_t c = hash->h2; - uint32_t d = hash->h3; - uint32_t e = hash->h4; - uint32_t f = hash->h5; - uint32_t g = hash->h6; - uint32_t h = hash->h7; - - for(int i = 0; i != 64; i++){ - uint32_t S1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25); - uint32_t ch = (e & f) ^ ((~e) & g); - uint32_t temp1 = h + S1 + ch + k[i] + W[i]; - - uint32_t S0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22); - uint32_t maj = (a & b) ^ (a & c) ^ (b & c); - uint32_t temp2 = S0 + maj; - - h = g; - g = f; - f = e; - e = d + temp1; - d = c; - c = b; - b = a; - a = temp1 + temp2; - } - - hash->h0 += a; - hash->h1 += b; - hash->h2 += c; - hash->h3 += d; - hash->h4 += e; - hash->h5 += f; - hash->h6 += g; - hash->h7 += h; + const uint32_t k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + uint32_t W[64]; + memset(W, 0, 64 * sizeof (uint32_t)); + + for (int i = 0; i < 16; i++) + W[i] = (hash->buffer[i * 4] << 24) | (hash->buffer[i * 4 + 1] << 16) | (hash->buffer[i * 4 + 2] << 8) | (hash->buffer[i * 4 + 3]); + + for(int i = 16; i != 64; i++){ + uint32_t s0 = rotr32(W[i - 15], 7) ^ rotr32(W[i - 15], 18) ^ (W[i - 15] >> 3); + uint32_t s1 = rotr32(W[i - 2], 17) ^ rotr32(W[i - 2], 19) ^ (W[i - 2] >> 10); + W[i] = W[i - 16] + s0 + W[i - 7] + s1; + } + + uint32_t a = hash->h0; + uint32_t b = hash->h1; + uint32_t c = hash->h2; + uint32_t d = hash->h3; + uint32_t e = hash->h4; + uint32_t f = hash->h5; + uint32_t g = hash->h6; + uint32_t h = hash->h7; + + for(int i = 0; i != 64; i++){ + uint32_t S1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25); + uint32_t ch = (e & f) ^ ((~e) & g); + uint32_t temp1 = h + S1 + ch + k[i] + W[i]; + + uint32_t S0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22); + uint32_t maj = (a & b) ^ (a & c) ^ (b & c); + uint32_t temp2 = S0 + maj; + + h = g; + g = f; + f = e; + e = d + temp1; + d = c; + c = b; + b = a; + a = temp1 + temp2; + } + + hash->h0 += a; + hash->h1 += b; + hash->h2 += c; + hash->h3 += d; + hash->h4 += e; + hash->h5 += f; + hash->h6 += g; + hash->h7 += h; } #define sha224_update sha256_update @@ -149,7 +149,7 @@ void _sha256_final(struct sha256_hash* hash){ sha256_round(hash); memset(hash->buffer, 0, bs); } - + size_t blen = 8*hash->total; for(int i = 0; i != 8; i++) hash->buffer[63 - i] = (uint8_t) (blen >> (i * 8) & 0xFF); @@ -158,43 +158,43 @@ void _sha256_final(struct sha256_hash* hash){ } void sha256_final(struct sha256_hash* hash, char* out){ - uint8_t old[bs]; - struct sha256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); + uint8_t old[bs]; + struct sha256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); - _sha256_final(hash); - sprintf(out, "%x%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6,hash->h7); + _sha256_final(hash); + sprintf(out, "%x%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6,hash->h7); - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); } void sha224_final(struct sha256_hash* hash, char* out){ - uint8_t old[bs]; - struct sha256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); + uint8_t old[bs]; + struct sha256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); - _sha256_final(hash); - sprintf(out, "%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6); + _sha256_final(hash); + sprintf(out, "%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6); - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); } void sha256(uint8_t* in, size_t len, char* out){ - struct sha256_hash a = sha256_init(); - sha256_update(in, len, &a); - sha256_final(&a, out); - free(a.buffer); + struct sha256_hash a = sha256_init(); + sha256_update(in, len, &a); + sha256_final(&a, out); + free(a.buffer); } void sha224(uint8_t* in, size_t len, char* out){ - struct sha256_hash a = sha224_init(); - sha224_update(in, len, &a); - sha224_final(&a, out); - free(a.buffer); + struct sha256_hash a = sha224_init(); + sha224_update(in, len, &a); + sha224_final(&a, out); + free(a.buffer); } //common_hash_clone(sha256); @@ -203,7 +203,7 @@ lua_common_hash_clone_oargs(sha256, sha256, l_sha256_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_l(sha256, sha256); lua_common_hash_update(sha256, sha256); @@ -221,7 +221,7 @@ int l_sha256(lua_State* L){ if(lua_gettop(L) == 0) return l_sha256_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[256]; sha256(a, len, digest); @@ -237,7 +237,7 @@ lua_common_hash_clone_oargs(sha224, sha224, l_sha224_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_l(sha224, sha224); lua_common_hash_update(sha224, sha224); @@ -255,7 +255,7 @@ int l_sha224(lua_State* L){ if(lua_gettop(L) == 0) return l_sha224_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[224]; sha224(a, len, digest); diff --git a/src/hash/sha2xx.h b/src/hash/sha2xx.h index cd7dc83..8fe94a7 100644 --- a/src/hash/sha2xx.h +++ b/src/hash/sha2xx.h @@ -1,10 +1,10 @@ #include "../lua.h" struct sha256_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total; - uint32_t h0, h1, h2, h3, h4, h5, h6, h7; + uint8_t* buffer; + size_t bufflen; + uint64_t total; + uint32_t h0, h1, h2, h3, h4, h5, h6, h7; }; int l_sha256(lua_State*); diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c index 527bcc7..e283949 100644 --- a/src/hash/sysvchecksum.c +++ b/src/hash/sysvchecksum.c @@ -41,12 +41,12 @@ int l_sysvchecksum_final(lua_State* L){ 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[16];
uint16_t u = sysvchecksum(a, len);
diff --git a/src/hash/sysvchecksum.h b/src/hash/sysvchecksum.h index cae322d..8eee0b7 100644 --- a/src/hash/sysvchecksum.h +++ b/src/hash/sysvchecksum.h @@ -2,7 +2,7 @@ #include <stdint.h> struct sysvchecksum_hash { - uint32_t check; + uint32_t check; }; struct sysvchecksum_hash sysvchecksum_init(); diff --git a/src/hash/xor.c b/src/hash/xor.c index bca4d5b..3f7903b 100644 --- a/src/hash/xor.c +++ b/src/hash/xor.c @@ -26,22 +26,22 @@ uint8_t xor8(uint8_t* aa, size_t len){ }
common_hash_clone(xor8)
-common_hash_init_update(xor8);
+ common_hash_init_update(xor8);
-int l_xor8_final(lua_State* L){
- 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_final(lua_State* L){
+ 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 = xor8(a, len);
diff --git a/src/hash/xor.h b/src/hash/xor.h index 6065524..a05de5c 100644 --- a/src/hash/xor.h +++ b/src/hash/xor.h @@ -2,7 +2,7 @@ #include <stdint.h> struct xor8_hash { - uint8_t a; + uint8_t a; }; struct xor8_hash xor8_init(); @@ -13,4 +13,4 @@ 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 +int l_xor8_final(lua_State*); @@ -89,7 +89,7 @@ void i_pprint(lua_State* L, int indent, int skip_indent){ i_pprint(L,indent+1,1); printf(","); if(!skip) printf("\n"); - + lua_settop(L, tuwu); lua_pop(L,1); } @@ -123,7 +123,7 @@ void i_pprint(lua_State* L, int indent, int skip_indent){ if(!skip_indent) print_indentation(indent); printf(color_yellow"(lud,%p)"color_reset, lua_topointer(L,-1)); break; - //case LUA_TNUMBER: + //case LUA_TNUMBER: if(!skip_indent) print_indentation(indent); printf(color_yellow"%i"color_reset, (int)lua_tonumber(L,-1)); break; @@ -170,17 +170,17 @@ enum json_state { memset(buf, 0, value->len);\ switch(value->c[0]){\ case '{': case '[':\ - json_parse(L, value);\ - break;\ + json_parse(L, value);\ + break;\ case '"':\ - strncpy(buf, value->c + 1, value->len - 2);\ - lua_pushstring(L, buf);\ - break;\ + strncpy(buf, value->c + 1, value->len - 2);\ + lua_pushstring(L, buf);\ + break;\ default:\ - lua_pushnumber(L, atof(value->c));\ - break;\ - }\ - lua_settable(L, last_idx); + lua_pushnumber(L, atof(value->c));\ + break;\ + }\ + lua_settable(L, last_idx); void json_parse(lua_State* L, str* raw){ enum json_state state = normal; @@ -215,7 +215,7 @@ void json_parse(lua_State* L, str* raw){ if((last == '{' && topush[0] == '}') || (last == '[' && topush[0] == ']')) token_depth--; if((last == '\0' || (last == '"' && topush[0] == '"') - || (last == '{' && topush[0] == '}') || (last == '[' && topush[0] == ']'))){ + || (last == '{' && topush[0] == '}') || (last == '[' && topush[0] == ']'))){ if(token_depth == 0){ if(last == '\0'){ last = topush[0]; @@ -231,7 +231,7 @@ void json_parse(lua_State* L, str* raw){ case ',': if(state == normal){ push() - str_clear(key); + str_clear(key); str_clear(value); count = 0; iter_count++; @@ -252,7 +252,7 @@ void json_parse(lua_State* L, str* raw){ else str_push(value, topush); } push() - str_free(key); + str_free(key); str_free(value); //printf("key: %s, value : %s\n",key.c,value.c); } @@ -14,15 +14,15 @@ int luaI_nothing(lua_State* L){ }
void* __malloc_(size_t N){
- printf("hi");
- malloc_count++;
- return (malloc)(N);
+ printf("hi");
+ malloc_count++;
+ return (malloc)(N);
}
void __free_(void* p){
- malloc_count--;
- printf("%i\n",malloc_count);
- return (free)(p);
+ malloc_count--;
+ printf("%i\n",malloc_count);
+ return (free)(p);
}
int _stream_read(lua_State* L){
@@ -49,7 +49,7 @@ int _stream_read(lua_State* L){ if(ret == 0){
luaI_tsetb(L, 1, "more", 0);
}
-
+
lua_pushlstring(L, cont->c, cont->len);
str_free(cont);
return 1;
@@ -133,7 +133,7 @@ void luaI_newstream(lua_State* L, stream_read_function readf, stream_free_functi luaI_tsetcf(L, tidx, "close", _stream_free);
luaI_tsetb(L, tidx, "more", 1);
luaI_tsetcf(L, tidx, "file", _stream_file);
-
+
lua_newtable(L);
int midx = lua_gettop(L);
@@ -147,15 +147,15 @@ void luaI_newstream(lua_State* L, stream_read_function readf, stream_free_functi int writer(lua_State *L, const void* p, size_t sz, void* ud){
- char o[2] = {0};
- for (int i =0; i<sz; i++){
- //printf("%c\n",((char*)p)[i]);
- o[0] = ((char*)p)[i];
- str_pushl((str*)ud, o, 1);
- //printf("%s\n",((str*)ud)->c);
- }
-
- return 0;
+ char o[2] = {0};
+ for (int i =0; i<sz; i++){
+ //printf("%c\n",((char*)p)[i]);
+ o[0] = ((char*)p)[i];
+ str_pushl((str*)ud, o, 1);
+ //printf("%s\n",((str*)ud)->c);
+ }
+
+ return 0;
}
/**
@@ -165,130 +165,130 @@ int writer(lua_State *L, const void* p, size_t sz, void* ud){ * @param {lua_State*} dest
* @param {void*} items already visited, use NULL
* @param {int} whether or not to skip meta data
-*/
+ */
void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){
- //printf("%i\n",seen->len);
- int at, at2;
- //int *sp = malloc(1);
- //int *sp;
- double n;
- int old_top = lua_gettop(src);
- int modi = 0;
-
- int type;
- switch(type = lua_type(src, -1)){
- case LUA_TNUMBER:
- n = lua_tonumber(src, -1);
- if(n == (uint64_t)n) lua_pushinteger(dest, (uint64_t)lua_tonumber(src, -1));
- else lua_pushnumber(dest, n);
- break;
- case LUA_TBOOLEAN:
- lua_pushboolean(dest, lua_toboolean(src, -1));
- break;
- case LUA_TNIL:
- lua_pushnil(dest);
- break;
- case LUA_TSTRING:;
- size_t slen;
- const char* ss = lua_tolstring(src, -1, &slen);
- lua_pushlstring(dest, ss, slen);
- break;
- case LUA_TTABLE:
- modi = 1;
- lua_newtable(dest);
- at = lua_gettop(dest);
- at2 = lua_gettop(src);
- //printf("before\n");
- char* aauwu = calloc(sizeof * aauwu, 50);
- sprintf(aauwu, "%p", lua_topointer(src, at2));
- //lua_pushstring(dest, aauwu);
- //lua_gettable(dest, LUA_REGISTRYINDEX);
- lua_getfield(dest, LUA_REGISTRYINDEX, aauwu);
- if(lua_type(dest, -1) == LUA_TNIL){
- //printf("new %p\n", lua_topointer(src, at2));
- lua_pushstring(dest, aauwu);
- lua_pushvalue(dest, at);
- lua_settable(dest, LUA_REGISTRYINDEX);
- lua_pop(dest, 1);
- } else {
- //printf("use %p\n", lua_topointer(src, at2));
- //lua_pop(dest, 1);
- lua_remove(dest, -2);
- free(aauwu);
- return;
- }
- free(aauwu);
- //printf("after %i:%i\n", at, lua_gettop(dest));
-
- lua_pushnil(src);
- for(;lua_next(src, at2) != 0;){
- int first, second = first = lua_gettop(src);
- first -= 1;
- //this is a mess, skip if key is __gc (when SKIP_GC)
- //and skip _G (when SKIP__G)
- if(((flags & SKIP__G) && lua_type(src, first) == LUA_TSTRING
- && strcmp("_G", lua_tostring(src, first)) == 0)
- || ((flags & SKIP_GC) && lua_type(src, first) == LUA_TSTRING
- && strcmp("__gc", lua_tostring(src, first)) == 0)){
- //printf("found %s\n", lua_tostring(src, first));
- lua_pop(src, 1);
- continue;
- }
- lua_pushvalue(src, first);
- //l_pprint(src);
- //lua_pop(src, 1);
- luaI_deepcopy(src, dest, flags);
-
- lua_pushvalue(src, second);
- luaI_deepcopy(src, dest, flags);
- lua_settable(dest, at);
-
- lua_pop(src, 3);
- }
- break;
- case LUA_TFUNCTION:
- if(lua_iscfunction(src, old_top)){
- //kinda silly
- lua_pushcfunction(dest, lua_tocfunction(src, -1));
- break;
- }
-
- str* awa = str_init("");
- lua_dump(src, writer, (void*)awa, 0);
-
- luaL_loadbuffer(dest, awa->c, awa->len, "fun");
- if(!(flags & SKIP_LOCALS)) lua_assign_upvalues(dest, lua_gettop(dest));
- //lua_remove(dest, -2);
- str_free(awa);
- break;
- case LUA_TUSERDATA:
- modi = 1;
- size_t raw_len = lua_rawlen(src, -1);
- void* ud = lua_newuserdata(dest, raw_len);
- memcpy(ud, lua_touserdata(src, -1), raw_len);
- break;
- case LUA_TLIGHTUSERDATA:
- modi = 1;
- lua_pushlightuserdata(dest, lua_touserdata(src, -1));
- break;
- case LUA_TTHREAD:
- lua_pushnil(dest);
- break;
- default:
- printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type);
- //abort();
- lua_pushnil(dest);
- break;
- }
- int tidx = lua_gettop(dest);
+ //printf("%i\n",seen->len);
+ int at, at2;
+ //int *sp = malloc(1);
+ //int *sp;
+ double n;
+ int old_top = lua_gettop(src);
+ int modi = 0;
+
+ int type;
+ switch(type = lua_type(src, -1)){
+ case LUA_TNUMBER:
+ n = lua_tonumber(src, -1);
+ if(n == (uint64_t)n) lua_pushinteger(dest, (uint64_t)lua_tonumber(src, -1));
+ else lua_pushnumber(dest, n);
+ break;
+ case LUA_TBOOLEAN:
+ lua_pushboolean(dest, lua_toboolean(src, -1));
+ break;
+ case LUA_TNIL:
+ lua_pushnil(dest);
+ break;
+ case LUA_TSTRING:;
+ size_t slen;
+ const char* ss = lua_tolstring(src, -1, &slen);
+ lua_pushlstring(dest, ss, slen);
+ break;
+ case LUA_TTABLE:
+ modi = 1;
+ lua_newtable(dest);
+ at = lua_gettop(dest);
+ at2 = lua_gettop(src);
+ //printf("before\n");
+ char* aauwu = calloc(sizeof * aauwu, 50);
+ sprintf(aauwu, "%p", lua_topointer(src, at2));
+ //lua_pushstring(dest, aauwu);
+ //lua_gettable(dest, LUA_REGISTRYINDEX);
+ lua_getfield(dest, LUA_REGISTRYINDEX, aauwu);
+ if(lua_type(dest, -1) == LUA_TNIL){
+ //printf("new %p\n", lua_topointer(src, at2));
+ lua_pushstring(dest, aauwu);
+ lua_pushvalue(dest, at);
+ lua_settable(dest, LUA_REGISTRYINDEX);
+ lua_pop(dest, 1);
+ } else {
+ //printf("use %p\n", lua_topointer(src, at2));
+ //lua_pop(dest, 1);
+ lua_remove(dest, -2);
+ free(aauwu);
+ return;
+ }
+ free(aauwu);
+ //printf("after %i:%i\n", at, lua_gettop(dest));
+
+ lua_pushnil(src);
+ for(;lua_next(src, at2) != 0;){
+ int first, second = first = lua_gettop(src);
+ first -= 1;
+ //this is a mess, skip if key is __gc (when SKIP_GC)
+ //and skip _G (when SKIP__G)
+ if(((flags & SKIP__G) && lua_type(src, first) == LUA_TSTRING
+ && strcmp("_G", lua_tostring(src, first)) == 0)
+ || ((flags & SKIP_GC) && lua_type(src, first) == LUA_TSTRING
+ && strcmp("__gc", lua_tostring(src, first)) == 0)){
+ //printf("found %s\n", lua_tostring(src, first));
+ lua_pop(src, 1);
+ continue;
+ }
+ lua_pushvalue(src, first);
+ //l_pprint(src);
+ //lua_pop(src, 1);
+ luaI_deepcopy(src, dest, flags);
+
+ lua_pushvalue(src, second);
+ luaI_deepcopy(src, dest, flags);
+ lua_settable(dest, at);
+
+ lua_pop(src, 3);
+ }
+ break;
+ case LUA_TFUNCTION:
+ if(lua_iscfunction(src, old_top)){
+ //kinda silly
+ lua_pushcfunction(dest, lua_tocfunction(src, -1));
+ break;
+ }
+
+ str* awa = str_init("");
+ lua_dump(src, writer, (void*)awa, 0);
+
+ luaL_loadbuffer(dest, awa->c, awa->len, "fun");
+ if(!(flags & SKIP_LOCALS)) lua_assign_upvalues(dest, lua_gettop(dest));
+ //lua_remove(dest, -2);
+ str_free(awa);
+ break;
+ case LUA_TUSERDATA:
+ modi = 1;
+ size_t raw_len = lua_rawlen(src, -1);
+ void* ud = lua_newuserdata(dest, raw_len);
+ memcpy(ud, lua_touserdata(src, -1), raw_len);
+ break;
+ case LUA_TLIGHTUSERDATA:
+ modi = 1;
+ lua_pushlightuserdata(dest, lua_touserdata(src, -1));
+ break;
+ case LUA_TTHREAD:
+ lua_pushnil(dest);
+ break;
+ default:
+ printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type);
+ //abort();
+ lua_pushnil(dest);
+ break;
+ }
+ int tidx = lua_gettop(dest);
- if(modi && !(flags & SKIP_META) && lua_getmetatable(src, -1)){
- luaI_deepcopy(src, dest, flags | IS_META | SKIP_META);
- lua_setmetatable(dest, tidx);
+ if(modi && !(flags & SKIP_META) && lua_getmetatable(src, -1)){
+ luaI_deepcopy(src, dest, flags | IS_META | SKIP_META);
+ lua_setmetatable(dest, tidx);
- lua_settop(dest, tidx);
- }
- lua_settop(src, old_top);
+ lua_settop(dest, tidx);
+ }
+ lua_settop(src, old_top);
}
void luaI_deepcopy2(lua_State* src, lua_State* dest){
@@ -298,51 +298,51 @@ void luaI_deepcopy2(lua_State* src, lua_State* dest){ break;
case LUA_TSTRING:;
- size_t size = 0;
- const char* str = lua_tolstring(src, -1, &size);
- lua_pushlstring(dest, str, size);
- break;
-
- case LUA_TTABLE:;
- const void* p = lua_topointer(src, -1);
- char* p_string = calloc(80, sizeof * p_string);
- sprintf(p_string, "%p", p);
-
- //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
- lua_pushstring(dest, p_string);
- lua_gettable(dest, LUA_REGISTRYINDEX);
- if(!lua_isnil(dest, -1)){
- break;
- }
-
- lua_pop(dest, 1);
- lua_pushstring(dest, p_string);
- lua_newtable(dest);
- //lua_setfield(dest, LUA_REGISTRYINDEX, p_string);
- //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
- lua_settable(dest, LUA_REGISTRYINDEX);
-
- lua_pushstring(dest, p_string);
- lua_gettable(dest, LUA_REGISTRYINDEX);
-
- free(p_string);
+ size_t size = 0;
+ const char* str = lua_tolstring(src, -1, &size);
+ lua_pushlstring(dest, str, size);
+ break;
- int src_top = lua_gettop(src);
- int dest_top = lua_gettop(dest);
-
- lua_pushnil(src);
- for(;lua_next(src, src_top) != 0;){
- luaI_deepcopy2(src, dest);
- lua_pop(src, 1);
- luaI_deepcopy2(src, dest);
-
- lua_settable(dest, dest_top);
- }
- break;
+ case LUA_TTABLE:;
+ const void* p = lua_topointer(src, -1);
+ char* p_string = calloc(80, sizeof * p_string);
+ sprintf(p_string, "%p", p);
+
+ //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
+ lua_pushstring(dest, p_string);
+ lua_gettable(dest, LUA_REGISTRYINDEX);
+ if(!lua_isnil(dest, -1)){
+ break;
+ }
+
+ lua_pop(dest, 1);
+ lua_pushstring(dest, p_string);
+ lua_newtable(dest);
+ //lua_setfield(dest, LUA_REGISTRYINDEX, p_string);
+ //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
+ lua_settable(dest, LUA_REGISTRYINDEX);
+
+ lua_pushstring(dest, p_string);
+ lua_gettable(dest, LUA_REGISTRYINDEX);
+
+ free(p_string);
+
+ int src_top = lua_gettop(src);
+ int dest_top = lua_gettop(dest);
+
+ lua_pushnil(src);
+ for(;lua_next(src, src_top) != 0;){
+ luaI_deepcopy2(src, dest);
+ lua_pop(src, 1);
+ luaI_deepcopy2(src, dest);
+
+ lua_settable(dest, dest_top);
+ }
+ break;
default:
- lua_pushinteger(dest, 4);
- break;
+ lua_pushinteger(dest, 4);
+ break;
}
}
@@ -351,7 +351,7 @@ int env_table(lua_State* L, int provide_table){ lua_newtable(L);
}
int tidx = lua_gettop(L);
-
+
lua_Debug debug;
for(int i = 0;; i++){
if(lua_getstack(L, i, &debug) == 0) break;
@@ -426,7 +426,7 @@ void luaI_copyvars(lua_State* from, lua_State* to){ * @brief extracts a table to be global
*
* @param {lua_State*} source
-*/
+ */
void lua_set_global_table(lua_State* L){
lua_pushnil(L);
for(;lua_next(L, -2) != 0;){
@@ -9,7 +9,7 @@ int l_gcd(lua_State*); static const luaL_Reg math_function_list [] = { {"lcm",l_lcm}, //{"gcd",l_gcd}, - + {NULL,NULL} }; @@ -93,7 +93,7 @@ int chunked_encoding_round(char* input, int length, struct chunked_encoding_stat for(int i = 0; i < length; i++){
//printf("%i/%i\n", i, length);
if(state->reading_length){
- str_pushl(state->buffer, input + i, 1);
+ str_pushl(state->buffer, input + i, 1);
if(state->buffer->len >= 2 && memmem(state->buffer->c + state->buffer->len - 2, 2, "\r\n", 2)){
@@ -123,9 +123,9 @@ int chunked_encoding_round(char* input, int length, struct chunked_encoding_stat struct url {
str* proto,
- * domain,
- * port,
- * path;
+ * domain,
+ * port,
+ * path;
};
struct url parse_url(char* url, int len){
@@ -204,7 +204,7 @@ int i_ws_read(lua_State* L){ }
if(len < 0) luaI_error(L, len, "SSL_read error");
-
+
uint64_t payload = 0;
struct ws_frame_info frame_info = {.fin = (buffer[0] >> 7) & 1, .rsv1 = (buffer[0] >> 6) & 1,
.rsv2 = (buffer[0] >> 5) & 1, .rsv3 = (buffer[0] >> 4) & 1, .opcode = buffer[0] & 0b1111,
@@ -225,8 +225,8 @@ int i_ws_read(lua_State* L){ payload = (buffer[0] & 0xff) << 8 | (buffer[1] & 0xff);
} else {
for(; (len = SSL_read(data->ssl, buffer + total_len, 8 - total_len)) > 0;){
- total_len += len;
- if(total_len >= 8) break;
+ total_len += len;
+ if(total_len >= 8) break;
}
if(len < 0) luaI_error(L, -1, "SSL_read error");
@@ -248,7 +248,7 @@ int i_ws_read(lua_State* L){ str_free(message);
luaI_error(L, len, "SSL_read error");
}
-
+
lua_newtable(L);
int idx = lua_gettop(L);
luaI_tsetsl(L, idx, "content", message->c, message->len);
@@ -375,7 +375,7 @@ int l_wss(lua_State* L){ luaI_tsetcf(L, idx, "read", i_ws_read);
luaI_tsetcf(L, idx, "write", i_ws_write);
luaI_tsetcf(L, idx, "close", i_ws_close);
-
+
lua_newtable(L);
int meta_idx = lua_gettop(L);
@@ -385,11 +385,11 @@ int l_wss(lua_State* L){ lua_setmetatable(L, idx);
lua_pushvalue(L, idx);
-
+
//verify stuff here
//parray_t* owo = NULL;
//parse_header(a->c, header_eof - a->c, &owo);
-
+
return 1;
}
@@ -521,7 +521,7 @@ int _request(lua_State* L, struct request_state* state){ state->ssl = ssl_connect(state->ctx, state->sock, host);
if(state->ssl == NULL) luaI_error(L, -1, "ssl_connect error");
}
-
+
char* cont = "";
size_t cont_len = 0;
if(params >= 2){
@@ -556,7 +556,7 @@ int _request(lua_State* L, struct request_state* state){ str_push(header, lua_tostring(L, -1));
lua_pop(L, 1);
}
-
+
char* action = "GET";
if(params >= 4){
action = (char*)lua_tostring(L, 4);
@@ -619,7 +619,7 @@ int _request(lua_State* L, struct request_state* state){ lua_gettable(L, idx);
int code = atoi(lua_tostring(L, -1));
luaI_tseti(L, idx, "code", code);
-
+
void* encoding = parray_get(owo, "Transfer-Encoding");
//struct _srequest_state *read_state = calloc(sizeof * read_state, 1);
@@ -635,7 +635,7 @@ int _request(lua_State* L, struct request_state* state){ chunked_encoding_round(header_eof + 4, extra_len - 4, chunk_state);
memset(buffer, 0, BUFFER_LEN);
-
+
state->buffer = str_init("");
state->state = chunk_state;
}
@@ -654,11 +654,11 @@ int _request(lua_State* L, struct request_state* state){ str_free(a);
/*SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN | SSL_SENT_SHUTDOWN);
- SSL_shutdown(ssl);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
+ SSL_shutdown(ssl);
+ SSL_free(ssl);
+ SSL_CTX_free(ctx);
- close(sock);*/
+ close(sock);*/
return 1;
}
@@ -722,7 +722,7 @@ void* handle_client(void *_arg){ int64_t bite = recv_header(client_fd, &buffer, &header);
header_eof = header - buffer;
-
+
if(bite > 0){
parray_t* table;
@@ -749,7 +749,7 @@ void* handle_client(void *_arg){ str* aa = str_init(portc);
struct net_path_t parsed_path;
path_parse(&parsed_path, path);
-
+
str* decoded_path;
int decoded_err = percent_decode(parsed_path.path, &decoded_path);
larray_t* params = NULL;
@@ -762,7 +762,7 @@ void* handle_client(void *_arg){ params = larray_init();
v = route_match(args->paths, aa->c, ¶ms);
-
+
if(sT != NULL)
rolling_file_parse(L, &files_idx, &body_idx, header + 4, sT, bite - header_eof - 4, file_cont);
}
@@ -774,7 +774,7 @@ void* handle_client(void *_arg){ int req_idx = lua_gettop(L);
lua_newtable(L);
int res_idx = lua_gettop(L);
-
+
//handle cookies
if(sC != NULL){
lua_newtable(L);
@@ -824,7 +824,7 @@ void* handle_client(void *_arg){ luaI_tseti(L, req_idx, "_bytes", bite - header_eof - 4);
luaI_tseti(L, req_idx, "client_fd", client_fd);
luaI_tsetcf(L, req_idx, "roll", l_roll);
-
+
//functions
luaI_tsetcf(L, res_idx, "send", l_send);
luaI_tsetcf(L, res_idx, "sendfile", l_sendfile);
@@ -841,7 +841,7 @@ void* handle_client(void *_arg){ lua_newtable(L);
int header_idx = lua_gettop(L);
luaI_tseti(L, header_idx, "Code", 200);
-
+
luaI_tsetv(L, res_idx, "header", header_idx);
//get all function that kinda match
@@ -869,29 +869,29 @@ void* handle_client(void *_arg){ //if request is HEAD, it is valid for GET and HEAD listeners
if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, sR->c) == 0 ||
(strcmp(sR->c, "HEAD") == 0 && strcmp(wowa->req, "GET") == 0)){
-
- luaL_loadbuffer(L, wowa->c, wowa->len, "fun");
- int func = lua_gettop(L);
- lua_assign_upvalues(L, func);
-
- lua_pushvalue(L, res_idx); //push methods related to dealing with the request
- lua_pushvalue(L, req_idx); //push info about the request
-
- //call the function
- if(lua_pcall(L, 2, 0, 0) != 0){
- printf("(net thread) %s\n", lua_tostring(L, -1));
- //send an error message if send has not been called
- if(client_fd >= 0) net_error(client_fd, 500);
-
- goto net_end;
- }
-
- //check if res:stop() was called
- lua_pushstring(L, "_stop");
- lua_gettable(L, res_idx);
- if(!lua_isnil(L, -1))
- goto net_end;
-
+
+ luaL_loadbuffer(L, wowa->c, wowa->len, "fun");
+ int func = lua_gettop(L);
+ lua_assign_upvalues(L, func);
+
+ lua_pushvalue(L, res_idx); //push methods related to dealing with the request
+ lua_pushvalue(L, req_idx); //push info about the request
+
+ //call the function
+ if(lua_pcall(L, 2, 0, 0) != 0){
+ printf("(net thread) %s\n", lua_tostring(L, -1));
+ //send an error message if send has not been called
+ if(client_fd >= 0) net_error(client_fd, 500);
+
+ goto net_end;
+ }
+
+ //check if res:stop() was called
+ lua_pushstring(L, "_stop");
+ lua_gettable(L, res_idx);
+ if(!lua_isnil(L, -1))
+ goto net_end;
+
}
}
}
@@ -1033,7 +1033,7 @@ int start_serv(lua_State* L, int port, parray_t* paths, struct net_server_state* pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, (void*)args);
pthread_detach(thread_id);
-
+
//handle_client((void*)args);
free(client_fd);
}
@@ -1082,7 +1082,7 @@ int l_req_com(lua_State* L, char* req){ str* uwu = str_init("");
lua_pushvalue(L, 3);
lua_dump(L, writer, (void*)uwu, 0);
-
+
awa = malloc(sizeof * awa);
awa->c = uwu->c;
awa->len = uwu->len;
@@ -1108,10 +1108,10 @@ int l_req_com(lua_State* L, char* req){ }
#define gen_reqs(T)\
-int l_##T##q (lua_State* L){\
- l_req_com(L, #T);\
- return 1;\
-}
+ int l_##T##q (lua_State* L){\
+ l_req_com(L, #T);\
+ return 1;\
+ }
gen_reqs(GET);
gen_reqs(HEAD);
gen_reqs(POST);
@@ -1153,7 +1153,7 @@ int l_listen(lua_State* L){ state->event_fd = -1;
int port = luaL_checkinteger(L, 2);
-
+
lua_newtable(L);
int mt = lua_gettop(L);
luaI_tsetcf(L, mt, "GET", l_GETq);
@@ -1166,7 +1166,7 @@ int l_listen(lua_State* L){ luaI_tsetcf(L, mt, "TRACE", l_TRACEq);
luaI_tsetcf(L, mt, "PATCH", l_PATCHq);
luaI_tsetcf(L, mt, "all", l_allq);
-
+
luaI_tsetcf(L, mt, "close", l_net_close);
luaI_tsetv(L, mt, "port", 2);
@@ -1182,5 +1182,5 @@ int l_listen(lua_State* L){ if(state->event_fd == -2) luaI_error(L, -2, "closed");
return start_serv(L, port, paths, state);
-;
+ ;
}
@@ -1,11 +1,11 @@ #ifdef _WIN32 //add -lws2_32
- #include <winsock2.h>
- //#define socklen_t __socklen_t
- //#define close closesocket
- typedef int socklen_t;
+#include <winsock2.h>
+//#define socklen_t __socklen_t
+//#define close closesocket
+typedef int socklen_t;
#else
- #include <sys/socket.h>
- #include <arpa/inet.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
#define closesocket close
#endif
@@ -43,7 +43,7 @@ static const luaL_Reg net_function_list [] = { {"request",l_request},
{"srequest",l_srequest},
{"wss",l_wss},
-
+
{NULL,NULL}
};
diff --git a/src/net/lua.c b/src/net/lua.c index 2e46943..ab112fe 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -10,7 +10,7 @@ int l_write(lua_State* L){ lua_gettable(L, -2); int head = strcmp(luaL_checkstring(L, -1), "HEAD") == 0; - + lua_pushvalue(L, res_idx); lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); @@ -20,7 +20,7 @@ int l_write(lua_State* L){ size_t len; char* content = (char*)luaL_checklstring(L, 2, &len); - + lua_pushvalue(L, res_idx); lua_pushstring(L, "header"); lua_gettable(L, -2); @@ -54,7 +54,7 @@ int l_send(lua_State* L){ lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); int client_fd = luaL_checkinteger(L, -1); - + client_fd_errors(client_fd); size_t len; @@ -76,7 +76,7 @@ int l_send(lua_State* L){ i_write_header(L, header, &resp, content, len); send(client_fd, resp->c, resp->len, MSG_NOSIGNAL); - + // lua_pushstring(L, "client_fd"); lua_pushinteger(L, -1); @@ -89,7 +89,7 @@ int l_send(lua_State* L){ int l_close(lua_State* L){ int res_idx = 1; - + lua_pushvalue(L, res_idx); lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); @@ -143,7 +143,7 @@ int l_roll(lua_State* L){ lua_gettable(L, 1); int client_fd = luaL_checkinteger(L, -1); client_fd_errors(client_fd); - + fd_set rfd; FD_ZERO(&rfd); FD_SET(client_fd, &rfd); @@ -209,7 +209,7 @@ int l_sendfile(lua_State* L){ lua_gettable(L, 3); if(!lua_isnil(L, -1)) filename = (char*)lua_tostring(L, -1); } - + lua_pushvalue(L, res_idx); lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); @@ -236,7 +236,7 @@ int l_sendfile(lua_State* L){ char* content_type = map_get(mime_type, ext + 1); if(content_type) - {luaI_tsets(L, header, "Content-Type", content_type);} + {luaI_tsets(L, header, "Content-Type", content_type);} } char* buffer = calloc(sizeof* buffer, bsize + 1); @@ -244,7 +244,7 @@ int l_sendfile(lua_State* L){ fseek(fp, 0L, SEEK_END); size_t sz = ftell(fp); fseek(fp, 0L, SEEK_SET); - + char size[256]; sprintf(size, "%li", sz); luaI_tsets(L, header, "Content-Length", size); diff --git a/src/net/luai.c b/src/net/luai.c index ac9e090..0b63d18 100644 --- a/src/net/luai.c +++ b/src/net/luai.c @@ -8,23 +8,23 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si str* header_vs = str_init(""); lua_pushnil(L); - + for(;lua_next(L, header_top) != 0;){ - char* key = (char*)luaL_checklstring(L, -2, NULL); - if(strcmp(key, "Code") != 0){ - str_push(header_vs, key); - str_push(header_vs, ": "); - str_push(header_vs, (char*)luaL_checklstring(L, -1, NULL)); - str_push(header_vs, "\r\n"); - } - lua_pop(L, 1); + char* key = (char*)luaL_checklstring(L, -2, NULL); + if(strcmp(key, "Code") != 0){ + str_push(header_vs, key); + str_push(header_vs, ": "); + str_push(header_vs, (char*)luaL_checklstring(L, -1, NULL)); + str_push(header_vs, "\r\n"); + } + lua_pop(L, 1); } lua_pushvalue(L, header_top); lua_pushstring(L, "Code"); lua_gettable(L, header_top); int code = luaL_checkinteger(L, -1); - + const char* code_det = http_code(code); http_build(&resp, code, code_det, header_vs->c, content, len); @@ -39,16 +39,16 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si * @param {char*} response buffer * @param {str*} response header Content-Type value * @return {int} lua index of table -*/ + */ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer, str* content_type, size_t blen, struct file_parse* _content){ struct file_parse content = *_content; /*enum file_status* status = (enum file_status*)parray_get(content, "_status"); - str* current = (str*)parray_get(content, "_current"); - str* old = (str*)parray_get(content, "_old"); - str* boundary = (str*)parray_get(content, "_boundary"); - str* boundary_id = (str*)parray_get(content, "_boundary_id"); - int* dash_count = (int*)parray_get(content, "_dash_count"); - int* table_idx = (int*)parray_get(content, "_table_idx");*/ + str* current = (str*)parray_get(content, "_current"); + str* old = (str*)parray_get(content, "_old"); + str* boundary = (str*)parray_get(content, "_boundary"); + str* boundary_id = (str*)parray_get(content, "_boundary_id"); + int* dash_count = (int*)parray_get(content, "_dash_count"); + int* table_idx = (int*)parray_get(content, "_table_idx");*/ //time_start(start) if(content.status == _ignore){ @@ -68,11 +68,11 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer content.current = str_init(""); content.table_idx = lua_gettop(L);//malloc(sizeof * table_idx); *table_idx = lua_gettop(L); - //parray_set(content, "_table_idx", (void*)(table_idx)); - //parray_set(content, "_status", (void*)(status)); - //parray_set(content, "_dash_count", (void*)(dash_count)); - //parray_set(content, "_current", (void*)(current)); - + //parray_set(content, "_table_idx", (void*)(table_idx)); + //parray_set(content, "_status", (void*)(status)); + //parray_set(content, "_dash_count", (void*)(dash_count)); + //parray_set(content, "_current", (void*)(current)); + content.boundary_id = str_init(""); //quick fix? @@ -81,7 +81,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer //parray_set(content, "_boundary", (void*)boundary); //parray_set(content, "_boundary_id", (void*)boundary_id); - + } //time_end("start", start) //printf("hi\n"); @@ -95,101 +95,101 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer lua_concat(L, 2); *body_idx = lua_gettop(L); } else { - file_start:; - //time_start(barrier_read) - if(content.status == BARRIER_READ){ - //printf("read %llu\n", blen); - for(int i = 0; i != blen; i++){ - //printf("%c",buffer[i]); - //printf("\n"); - if(*buffer == '\r'){ - content.status = FILE_HEADER; - buffer += 2; - blen -= i + 2; - - content.table_idx = lua_rawlen(L, *files_idx) + 1; - lua_pushinteger(L, content.table_idx); - lua_newtable(L); - lua_settable(L, *files_idx); - break; - } - str_pushl(content.boundary_id, buffer, 1); - buffer++; - } - } - //time_end("barrier_read", barrier_read) - lua_pushvalue(L, *files_idx); - lua_pushinteger(L, content.table_idx); - lua_gettable(L, -2); - int rfiles_idx = lua_gettop(L); - //time_start(file_header) - if(content.status == FILE_HEADER){ - //printf("header\n"); - for(int i = 0; i < blen; i++){ - - if(buffer[i] == ':'){ - content.old = content.current; - content.current = str_init(""); - } else if(buffer[i] == '\n'){ - if(content.current->len == 0){ - content.status = FILE_BODY; - - buffer += i + 1; - blen -= i + 1; - - content.old = NULL; - str_free(content.current); - content.current = str_init(""); - break; - } - //printf("%i '%s' : '%s'\n",*table_idx, old->c, current->c); - - luaI_tsets(L, rfiles_idx, content.old->c, content.current->c); - - str_free(content.old); - content.old = NULL; - str_clear(content.current); - } else if(buffer[i] != '\r' && !(buffer[i] == ' ' && content.current->len == 0)) str_pushl(content.current, buffer + i, 1); - } - } - //time_end("file_header", file_header) - //time_start(file_body) - if(content.status == FILE_BODY){ - char* barrier_end = memmem(buffer, blen, content.boundary->c, content.boundary->len); - if(barrier_end == NULL){ - str* temp = str_initl(content.current->c, content.current->len); - str_pushl(temp, buffer, blen); - barrier_end = memmem(temp->c, temp->len, content.boundary->c, content.boundary->len); - if(barrier_end != NULL) abort(); // todo - - str* temp2 = content.current; - content.current = temp; - str_free(temp2); - - } else { - char* start = barrier_end, *end = barrier_end; - for(; *start != '\n'; start--); - for(; *end != '\n'; end++); - int clen = start - buffer; - str_pushl(content.current, buffer, clen); - luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len - 1); - str_clear(content.current); - blen-= end - buffer; - buffer = end; - content.status = BARRIER_READ; - goto file_start; - //printf("%s\n",content.current->c); - } - - } - //time_end("file_body", file_body) +file_start:; + //time_start(barrier_read) + if(content.status == BARRIER_READ){ + //printf("read %llu\n", blen); + for(int i = 0; i != blen; i++){ + //printf("%c",buffer[i]); + //printf("\n"); + if(*buffer == '\r'){ + content.status = FILE_HEADER; + buffer += 2; + blen -= i + 2; + + content.table_idx = lua_rawlen(L, *files_idx) + 1; + lua_pushinteger(L, content.table_idx); + lua_newtable(L); + lua_settable(L, *files_idx); + break; + } + str_pushl(content.boundary_id, buffer, 1); + buffer++; + } + } + //time_end("barrier_read", barrier_read) + lua_pushvalue(L, *files_idx); + lua_pushinteger(L, content.table_idx); + lua_gettable(L, -2); + int rfiles_idx = lua_gettop(L); + //time_start(file_header) + if(content.status == FILE_HEADER){ + //printf("header\n"); + for(int i = 0; i < blen; i++){ + + if(buffer[i] == ':'){ + content.old = content.current; + content.current = str_init(""); + } else if(buffer[i] == '\n'){ + if(content.current->len == 0){ + content.status = FILE_BODY; + + buffer += i + 1; + blen -= i + 1; + + content.old = NULL; + str_free(content.current); + content.current = str_init(""); + break; + } + //printf("%i '%s' : '%s'\n",*table_idx, old->c, current->c); + + luaI_tsets(L, rfiles_idx, content.old->c, content.current->c); + + str_free(content.old); + content.old = NULL; + str_clear(content.current); + } else if(buffer[i] != '\r' && !(buffer[i] == ' ' && content.current->len == 0)) str_pushl(content.current, buffer + i, 1); + } + } + //time_end("file_header", file_header) + //time_start(file_body) + if(content.status == FILE_BODY){ + char* barrier_end = memmem(buffer, blen, content.boundary->c, content.boundary->len); + if(barrier_end == NULL){ + str* temp = str_initl(content.current->c, content.current->len); + str_pushl(temp, buffer, blen); + barrier_end = memmem(temp->c, temp->len, content.boundary->c, content.boundary->len); + if(barrier_end != NULL) abort(); // todo + + str* temp2 = content.current; + content.current = temp; + str_free(temp2); + + } else { + char* start = barrier_end, *end = barrier_end; + for(; *start != '\n'; start--); + for(; *end != '\n'; end++); + int clen = start - buffer; + str_pushl(content.current, buffer, clen); + luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len - 1); + str_clear(content.current); + blen-= end - buffer; + buffer = end; + content.status = BARRIER_READ; + goto file_start; + //printf("%s\n",content.current->c); + } + + } + //time_end("file_body", file_body) } /*parray_set(content, "_dash_count", dash_count); - parray_set(content, "_boundary_id", boundary_id); - parray_set(content, "_boundary", boundary); - parray_set(content, "_status", status); - parray_set(content, "_current", current); - parray_set(content, "_old", old);*/ + parray_set(content, "_boundary_id", boundary_id); + parray_set(content, "_boundary", boundary); + parray_set(content, "_status", status); + parray_set(content, "_current", current); + parray_set(content, "_old", old);*/ *_content = content; diff --git a/src/net/luai.h b/src/net/luai.h index 5452670..7e5fb07 100644 --- a/src/net/luai.h +++ b/src/net/luai.h @@ -9,6 +9,6 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si * @param {char*} response buffer * @param {str*} response header Content-Type value * @return {int} lua index of table -*/ + */ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer, str* content_type, size_t blen, struct file_parse* _content); diff --git a/src/net/util.c b/src/net/util.c index 61cf1b7..866e931 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -37,7 +37,7 @@ int64_t recv_header(int client_fd, char** _buffer, char** header_eof){ /** * @brief calls recv into buffer until everything is read * -*/ + */ // deprecated!! replaced by recv_header (above) int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state){ char* header, *buffer = malloc(BUFFER_SIZE * sizeof * buffer); @@ -59,13 +59,13 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st if(*header_eof == -1 && (header = strstr(buffer, "\r\n\r\n")) != NULL){ *header_eof = header - buffer; char* cont_len_raw = strstr(buffer, "Content-Length: "); - + if(cont_len_raw == NULL) { len += n; *_buffer = buffer; return len; } - + str* cont_len_str = str_init(""); if(cont_len_raw == NULL) abort(); //i is length of 'Content-Length: ' @@ -92,7 +92,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st buffer = realloc(buffer, len + BUFFER_SIZE + 1); memset(buffer + len, 0, n + 1); } - + if(content_len != -1 && len - *header_eof - 4 >= con_len_full) break; } @@ -104,7 +104,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st /** * @brief converts the request buffer into a parray_t * -*/ + */ int parse_header(char* buffer, int header_eof, parray_t** _table){ if(header_eof == -1) return -1; @@ -117,7 +117,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ if(buffer[oi] == ' ' || buffer[oi] == '\n'){ if(buffer[oi] == '\n') current->c[current->len - 1] = 0; if(item < 3) parray_set(table, item == 0 ? "Request" : - item == 1 ? "Path" : "Version", (void*)str_init(current->c)); + item == 1 ? "Path" : "Version", (void*)str_init(current->c)); str_clear(current); item++; if(buffer[oi] == '\n') break; @@ -174,16 +174,16 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ /** * @brief contructs an http request * -*/ + */ void http_build(str** _dest, int code, const char* code_det, char* header_vs, char* content, size_t len){ char* dest = malloc(HTTP_BUFFER_SIZE); memset(dest, 0, HTTP_BUFFER_SIZE); sprintf(dest, - "HTTP/1.1 %i %s\r\n" - "%s" - "\r\n" - , code, code_det, header_vs); + "HTTP/1.1 %i %s\r\n" + "%s" + "\r\n" + , code, code_det, header_vs); *_dest = str_init(dest); str_pushl(*_dest, content, len); @@ -193,7 +193,7 @@ void http_build(str** _dest, int code, const char* code_det, char* header_vs, ch /** * @brief gets a string representation of a http code * -*/ + */ const char* http_code(int code){ switch(code){ case 100: return "Continue"; break; @@ -384,9 +384,9 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){ for(int i = 0; i != paths->len; i++){ //if(match_param(paths->P[i].key->c, request)) //*if(strcmp(request, paths->P[i].key->c) == 0)*/{ - //printf("pass!\n"); + //printf("pass!\n"); //printf("%i\n", i); - + temp = parray_init(); if(match_param(paths->P[i].key->c, request, temp)){ @@ -478,7 +478,7 @@ void _parse_mimetypes(){ if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n') break; } type[type_len] = '\0'; - + //check if the type has an associated file type if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n'){ free(type); diff --git a/src/net/util.h b/src/net/util.h index b8bc824..a54ac95 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -11,7 +11,7 @@ * @param {char**} pointer to a unallocated buffer * @param {int*} pointer to an int, will be where the header ends * @return {int64_t} bytes read, -1 if the body was damaged, -2 if the header was -*/ + */ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state); int64_t recv_header(int client_fd, char** _buffer, char** header_eof); @@ -22,7 +22,7 @@ int64_t recv_header(int client_fd, char** _buffer, char** header_eof); * @param {int} where the header ends * @param {parray_t**} pointer to a unallocated parray_t * @return {int} returns 0 or -1 on failure -*/ + */ int parse_header(char* buffer, int header_eof, parray_t** _table); /** @@ -34,7 +34,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table); * @param {char*} all other header values * @param {char*} response content * @param {size_t} content length -*/ + */ void http_build(str** _dest, int code, const char* code_det, char* header_vs, char* content, size_t len); /** @@ -42,7 +42,7 @@ void http_build(str** _dest, int code, const char* code_det, char* header_vs, ch * * @param {int} http response code * @param {char*} allocated destination string -*/ + */ const char* http_code(int code); void client_fd_errors(int client_fd); @@ -32,8 +32,8 @@ open_common(test, test_config); #define push(T, name)\
lua_pushstring(L, #name);\
- luaopen_lullaby_##name(L);\
- lua_settable(L, T);
+luaopen_lullaby_##name(L);\
+lua_settable(L, T);
int luaopen_lullaby(lua_State* L) {
@@ -2,125 +2,125 @@ #include <stdlib.h> int i_hoarepartition(double* arr, int low, int high){ - double pivot = arr[((int)((high - low) / 2)) + low]; - int i = low - 1; - int j = high + 1; - - for(;;){ - i++; j--; - - while(arr[i] > pivot) i++; - while(arr[j] < pivot) j--; - if(i >= j) return j; - - i_swap(arr[i],arr[j]); - } + double pivot = arr[((int)((high - low) / 2)) + low]; + int i = low - 1; + int j = high + 1; + + for(;;){ + i++; j--; + + while(arr[i] > pivot) i++; + while(arr[j] < pivot) j--; + if(i >= j) return j; + + i_swap(arr[i],arr[j]); + } } void i_quicksort(double* arr, int low, int high){ - if(low >= 0 && high >= 0 && low < high){ - int p = i_hoarepartition(arr, low, high); - i_quicksort(arr, low, p); - i_quicksort(arr, p + 1, high); - } + if(low >= 0 && high >= 0 && low < high){ + int p = i_hoarepartition(arr, low, high); + i_quicksort(arr, low, p); + i_quicksort(arr, p + 1, high); + } } int l_quicksort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - - i_quicksort(nums, 0, len - 1); - - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ + + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } + + i_quicksort(nums, 0, len - 1); - free(nums); - return 1; + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } void i_merge(double* arr, int b, int m, int e){ - int n1 = m - b + 1; - int n2 = e - m; - - double* left = malloc(sizeof * left * n1); - double* right = malloc(sizeof * right * n2); - - for(int i = 0; i < n1; i++) left[i] = arr[b + i]; - for(int i = 0; i < n2; i++) right[i] = arr[m + 1 + i]; - - int l_ind = 0; - int r_ind = 0; - int k = b; - - for(; l_ind < n1 && r_ind < n2; k++){ - if(left[l_ind] >= right[r_ind]){ - arr[k] = left[l_ind]; - l_ind++; - } else { - arr[k] = right[r_ind]; - r_ind++; - } - } + int n1 = m - b + 1; + int n2 = e - m; + + double* left = malloc(sizeof * left * n1); + double* right = malloc(sizeof * right * n2); + + for(int i = 0; i < n1; i++) left[i] = arr[b + i]; + for(int i = 0; i < n2; i++) right[i] = arr[m + 1 + i]; - for(; l_ind < n1; k++){ + int l_ind = 0; + int r_ind = 0; + int k = b; + + for(; l_ind < n1 && r_ind < n2; k++){ + if(left[l_ind] >= right[r_ind]){ arr[k] = left[l_ind]; l_ind++; - } - for(; r_ind < n2; k++){ + } else { arr[k] = right[r_ind]; r_ind++; } + } + + for(; l_ind < n1; k++){ + arr[k] = left[l_ind]; + l_ind++; + } + for(; r_ind < n2; k++){ + arr[k] = right[r_ind]; + r_ind++; + } - free(left); - free(right); + free(left); + free(right); } void i_mergesort(double* arr, int b, int e){ - if(b < e){ - int mid = (b + e) /2; - i_mergesort(arr, b, mid); - i_mergesort(arr, mid + 1, e); - i_merge(arr, b, mid, e); - } + if(b < e){ + int mid = (b + e) /2; + i_mergesort(arr, b, mid); + i_mergesort(arr, mid + 1, e); + i_merge(arr, b, mid, e); + } } int l_mergesort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - - i_mergesort(nums, 0, len - 1); - - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ + + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } + + i_mergesort(nums, 0, len - 1); - free(nums); - return 1; + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } void i_heapify(double* arr, int n, int i){ @@ -141,286 +141,286 @@ void i_heapify(double* arr, int n, int i){ } int l_heapsort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - for(int i = len / 2 - 1; i >= 0; i--) - i_heapify(nums,len,i); + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ - for(int i = len - 1; i >= 0; i--){ - i_swap(nums[i],nums[0]); - i_heapify(nums, i, 0); - } - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } - free(nums); - return 1; + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } + for(int i = len / 2 - 1; i >= 0; i--) + i_heapify(nums,len,i); + + for(int i = len - 1; i >= 0; i--){ + i_swap(nums[i],nums[0]); + i_heapify(nums, i, 0); + } + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + free(nums); + return 1; } int l_shellsort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); + lua_pushinteger(L,i+1); + lua_gettable(L,1); - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } - for(int interval = len/2; interval > 0; interval /=2){ - for(int i = interval; i < len; i++){ - double temp = nums[i]; - int j; - for(j = i; j >= interval && nums[j - interval] < temp; j -= interval){ - nums[j] = nums[j - interval]; - } - nums[j] = temp; + for(int interval = len/2; interval > 0; interval /=2){ + for(int i = interval; i < len; i++){ + double temp = nums[i]; + int j; + for(j = i; j >= interval && nums[j - interval] < temp; j -= interval){ + nums[j] = nums[j - interval]; } + nums[j] = temp; } + } - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } - free(nums); - return 1; + free(nums); + return 1; } int l_bubblesort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); + lua_pushinteger(L,i+1); + lua_gettable(L,1); - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } - int n = len; - for(;n > 0;){ - int new = 0; - - for(int i = 0; i != n-1; i++){ - if(nums[i+1]>nums[i]){ - double temp = nums[i]; - nums[i] = nums[i+1]; - nums[i+1] = temp; - - new = i+1; - } - } - - n = new; - } + int n = len; + for(;n > 0;){ + int new = 0; + + for(int i = 0; i != n-1; i++){ + if(nums[i+1]>nums[i]){ + double temp = nums[i]; + nums[i] = nums[i+1]; + nums[i+1] = temp; - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); + new = i+1; + } } - - free(nums); - return 1; + + n = new; + } + + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } int l_countingsort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - int* nums = malloc(sizeof * nums * len); - int* out = malloc(sizeof * nums * len); - int max = 0; - for(int i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[i] = luaL_checknumber(L, -1); - out[i] = 0; - - if(nums[i]<0) p_fatal("array.countingsort(<table>) requires all indices to be >= 0"); - max = max < nums[i] ? nums[i] : max; - - lua_pop(L,1); - } - - int* count = calloc(max + 1, sizeof * count); - - for(size_t i = 0; i < len; i++){ - count[nums[i]]++; - } - - for(size_t i = 1; i <= max; i++){ - count[i] += count[i - 1]; - } + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + int* nums = malloc(sizeof * nums * len); + int* out = malloc(sizeof * nums * len); + int max = 0; + for(int i = 0; i <= len-1; i++){ - for(int i = len - 1; i >= 0; i--){ - out[count[nums[i]] - 1] = nums[i]; - count[nums[i]]--; - } + lua_pushinteger(L,i+1); + lua_gettable(L,1); - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,out[i]); - lua_settable(L, -3); - } - - free(count); - free(nums); - free(out); - return 1; + nums[i] = luaL_checknumber(L, -1); + out[i] = 0; + + if(nums[i]<0) p_fatal("array.countingsort(<table>) requires all indices to be >= 0"); + max = max < nums[i] ? nums[i] : max; + + lua_pop(L,1); + } + + int* count = calloc(max + 1, sizeof * count); + + for(size_t i = 0; i < len; i++){ + count[nums[i]]++; + } + + for(size_t i = 1; i <= max; i++){ + count[i] += count[i - 1]; + } + + for(int i = len - 1; i >= 0; i--){ + out[count[nums[i]] - 1] = nums[i]; + count[nums[i]]--; + } + + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,out[i]); + lua_settable(L, -3); + } + + free(count); + free(nums); + free(out); + return 1; } int i_sorted(double* arr, size_t len){ - for(size_t i = 0; i != len - 1; i++) - if(arr[i] > arr[i+1]) return 0; - return 1; + for(size_t i = 0; i != len - 1; i++) + if(arr[i] > arr[i+1]) return 0; + return 1; } int l_miraclesort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); + lua_pushinteger(L,i+1); + lua_gettable(L,1); - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } - for(;!i_sorted(nums,len);); + for(;!i_sorted(nums,len);); - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } - - free(nums); - return 1; + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } int l_stalinsort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - size_t rlen = 0; - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - double n = luaL_checknumber(L, -1); - if(rlen == 0 || nums[rlen - 1] <= n){ - nums[rlen] = n; - rlen++; - } - - lua_pop(L,1); - } + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + size_t rlen = 0; + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ + lua_pushinteger(L,i+1); + lua_gettable(L,1); - lua_newtable(L); - for(size_t i = 0; i != rlen; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); + double n = luaL_checknumber(L, -1); + if(rlen == 0 || nums[rlen - 1] <= n){ + nums[rlen] = n; + rlen++; } - free(nums); - return 1; + lua_pop(L,1); + } + + + lua_newtable(L); + for(size_t i = 0; i != rlen; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } void i_slowsort(double* arr, int i, int j){ - if(i >= j) return; + if(i >= j) return; - int m = (i + j) /2; + int m = (i + j) /2; - i_slowsort(arr, i, m); - i_slowsort(arr, m + 1, j); + i_slowsort(arr, i, m); + i_slowsort(arr, m + 1, j); - if(arr[j] < arr[m]){ - i_swap(arr[j], arr[m]); - } + if(arr[j] < arr[m]){ + i_swap(arr[j], arr[m]); + } - i_slowsort(arr, i, j - 1); + i_slowsort(arr, i, j - 1); } int l_slowsort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(int i = 0; i <= len-1; i++){ + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(int i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); + lua_pushinteger(L,i+1); + lua_gettable(L,1); - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } - i_slowsort(nums, 0, len - 1); + i_slowsort(nums, 0, len - 1); - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } - free(nums); - return 1; + free(nums); + return 1; } int l_bogosort(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); + lua_pushinteger(L,i+1); + lua_gettable(L,1); - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } - for(;!i_sorted(nums, len);){ - i_shuffle(nums, len); - } + for(;!i_sorted(nums, len);){ + i_shuffle(nums, len); + } - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } - free(nums); - return 1; + free(nums); + return 1; } @@ -9,7 +9,7 @@ int l_bubblesort(lua_State*); //[double+int] -> arr[N] (greatest -> least) int l_heapsort(lua_State*); //[double+int] -> arr[N] (greatest -> least) //non-comparison sorts - //good for large arrays filled with small values +//good for large arrays filled with small values int l_countingsort(lua_State*); //[int] (arr[N] >= 0) -> arr[N] (least -> greatest) //esoteric sorts diff --git a/src/table.c b/src/table.c index 418da3a..bf14892 100644 --- a/src/table.c +++ b/src/table.c @@ -41,7 +41,7 @@ int l_split(lua_State* L){ int l_unpack(lua_State* L){ int top = lua_gettop(L); lua_pushnil(L); - + for(;lua_next(L, top);){ lua_pushvalue(L, -2); lua_remove(L, -3); @@ -60,170 +60,170 @@ uint64_t i_len(lua_State* L, int pos){ return i; } int l_len(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnumber(L,i_len(L,1)); - return 1; + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushnumber(L,i_len(L,1)); + return 1; } int l_reverse(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - - size_t len = lua_objlen(L,1); - lua_newtable(L); - for(size_t i = 0; i <= len - 1; i++){ - lua_pushnumber(L,len - i - 1); - lua_gettable(L,1); - - lua_pushnumber(L, i+1); - lua_pushvalue(L, -2); - - lua_settable(L,2); - } + luaL_checktype(L, 1, LUA_TTABLE); + + size_t len = lua_objlen(L,1); + lua_newtable(L); + for(size_t i = 0; i <= len - 1; i++){ + lua_pushnumber(L,len - i - 1); + lua_gettable(L,1); + + lua_pushnumber(L, i+1); + lua_pushvalue(L, -2); + + lua_settable(L,2); + } - lua_pushvalue(L, 2); - return 1; + lua_pushvalue(L, 2); + return 1; } int l_greatest(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - int touched = 0; - double cur = 0; - - for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - double t = luaL_checknumber(L, -1); - if(!touched || t > cur) cur = t; - touched = 1; - lua_pop(L,1); - } - - lua_pushnumber(L, cur); - return 1; + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + int touched = 0; + double cur = 0; + + for(size_t i = 0; i <= len-1; i++){ + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + double t = luaL_checknumber(L, -1); + if(!touched || t > cur) cur = t; + touched = 1; + lua_pop(L,1); + } + + lua_pushnumber(L, cur); + return 1; } int l_least(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - int touched = 0; - double cur = 0; - - for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - double t = luaL_checknumber(L, -1); - if(!touched || t < cur) cur = t; - touched = 1; - lua_pop(L,1); - } - - lua_pushnumber(L, cur); - return 1; + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + int touched = 0; + double cur = 0; + + for(size_t i = 0; i <= len-1; i++){ + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + double t = luaL_checknumber(L, -1); + if(!touched || t < cur) cur = t; + touched = 1; + lua_pop(L,1); + } + + lua_pushnumber(L, cur); + return 1; } void i_shuffle(double* arr, size_t len){ - for(size_t i = 0; i < len; i++){ - int i2 = rand()%len; - int d = rand()%len; - i_swap(arr[i2], arr[d]); - } + for(size_t i = 0; i < len; i++){ + int i2 = rand()%len; + int d = rand()%len; + i_swap(arr[i2], arr[d]); + } } int l_shuffle(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(int i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - - i_shuffle(nums, len); - - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); - } + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double* nums = malloc(sizeof * nums * len); + for(int i = 0; i <= len-1; i++){ - free(nums); - return 1; + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + nums[i] = luaL_checknumber(L, -1); + lua_pop(L,1); + } + + i_shuffle(nums, len); + + lua_newtable(L); + for(size_t i = 0; i != len; i++){ + lua_pushnumber(L,i+1); + lua_pushnumber(L,nums[i]); + lua_settable(L, -3); + } + + free(nums); + return 1; } int l_sum(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - double s = 0; - for(int i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - s += luaL_checknumber(L, -1); - lua_pop(L,1); - } - - lua_pushnumber(L, s); - return 1; + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + double s = 0; + for(int i = 0; i <= len-1; i++){ + + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + s += luaL_checknumber(L, -1); + lua_pop(L,1); + } + + lua_pushnumber(L, s); + return 1; } int l_indexof(lua_State* L) { - int argc = lua_gettop(L); - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - - //get optional 3rd argument, if its >0 set it to 0 - size_t start = argc == 3 ? luaL_checknumber(L,3) : 0; - start = start > 0 ? start : start; - - for(size_t i = 0; i <= len-1; i++){ - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - if(lua_rawequal(L, -1, 2)){ - lua_pushnumber(L, i); - return 1; - } - lua_pop(L,1); + int argc = lua_gettop(L); + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + + //get optional 3rd argument, if its >0 set it to 0 + size_t start = argc == 3 ? luaL_checknumber(L,3) : 0; + start = start > 0 ? start : start; + + for(size_t i = 0; i <= len-1; i++){ + lua_pushinteger(L,i+1); + lua_gettable(L,1); + + if(lua_rawequal(L, -1, 2)){ + lua_pushnumber(L, i); + return 1; } - - lua_pushnumber(L, -1); - return 1; + lua_pop(L,1); + } + + lua_pushnumber(L, -1); + return 1; } int l_sindexof(lua_State* L) { - double target = luaL_checknumber(L, 2); - luaL_checktype(L, 1, LUA_TTABLE); - size_t len = lua_objlen(L,1); - int l = 0; - int r = len - 1; - - for(; l<=r;){ - int m = l + (r - l) /2; - lua_pushinteger(L,m+1); - lua_gettable(L,1); - - double t = luaL_checknumber(L, -1); - if(t==target){ - lua_pushnumber(L, m); - return 1; - } - if(t > target) l = m + 1; - else r = m - 1; - - lua_pop(L,1); + double target = luaL_checknumber(L, 2); + luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); + int l = 0; + int r = len - 1; + + for(; l<=r;){ + int m = l + (r - l) /2; + lua_pushinteger(L,m+1); + lua_gettable(L,1); + + double t = luaL_checknumber(L, -1); + if(t==target){ + lua_pushnumber(L, m); + return 1; } - - lua_pushnumber(L, -1); - return 1; + if(t > target) l = m + 1; + else r = m - 1; + + lua_pop(L,1); + } + + lua_pushnumber(L, -1); + return 1; } /*int l_split(lua_State* L){ @@ -239,34 +239,34 @@ int l_sindexof(lua_State* L) { memset(current, 0, input_len); for(size_t i = 0; i <= (input_len - 1) - (split_len - 1); i++){ - int match = 1; - for(size_t z = 0; z <= split_len - 1 && match; z++){ - if(split[z] != input[z + i]) match = 0; - } - if(match){ - lua_pushnumber(L, table_len++); - lua_pushstring(L, current); - lua_settable(L, -3); - - memset(current, 0, input_len); - current_len = 0; - } else { - current[current_len] = input[i]; - current_len++; - } + int match = 1; + for(size_t z = 0; z <= split_len - 1 && match; z++){ + if(split[z] != input[z + i]) match = 0; + } + if(match){ + lua_pushnumber(L, table_len++); + lua_pushstring(L, current); + lua_settable(L, -3); + + memset(current, 0, input_len); + current_len = 0; + } else { + current[current_len] = input[i]; + current_len++; + } } lua_pushnumber(L, table_len++); lua_pushstring(L, current); lua_settable(L, -3); return 1; -}*/ + }*/ int l_to_char_array(lua_State* L){ size_t input_len = 0; char* input = (char*)luaL_checklstring(L, 1, &input_len); lua_newtable(L); - + for(size_t i = 0; i <= input_len - 1; i++){ lua_pushnumber(L, i + 1); char uwu = input[i]; diff --git a/src/table.h b/src/table.h index 51ef993..6d872c3 100644 --- a/src/table.h +++ b/src/table.h @@ -24,34 +24,34 @@ int l_split(lua_State*); #define clean_lullaby_table luaI_nothing static const luaL_Reg table_function_list [] = { - {"len",l_len}, - {"reverse",l_reverse}, - {"greatest",l_greatest}, - {"least",l_least}, - {"shuffle",l_shuffle}, - {"sum",l_sum}, - {"split",l_split}, - {"to_char_array", l_to_char_array}, - - {"index",l_indexof}, - {"sindex",l_sindexof}, - - {"quicksort",l_quicksort}, - {"mergesort",l_mergesort}, - {"shellsort",l_shellsort}, - {"bubblesort",l_bubblesort}, - {"heapsort",l_heapsort}, - - {"countingsort",l_countingsort}, - - {"miraclesort",l_miraclesort}, - {"stalinsort",l_stalinsort}, - {"slowsort",l_slowsort}, - {"bogosort",l_bogosort}, - - {"unpack", l_unpack}, - - {NULL,NULL} + {"len",l_len}, + {"reverse",l_reverse}, + {"greatest",l_greatest}, + {"least",l_least}, + {"shuffle",l_shuffle}, + {"sum",l_sum}, + {"split",l_split}, + {"to_char_array", l_to_char_array}, + + {"index",l_indexof}, + {"sindex",l_sindexof}, + + {"quicksort",l_quicksort}, + {"mergesort",l_mergesort}, + {"shellsort",l_shellsort}, + {"bubblesort",l_bubblesort}, + {"heapsort",l_heapsort}, + + {"countingsort",l_countingsort}, + + {"miraclesort",l_miraclesort}, + {"stalinsort",l_stalinsort}, + {"slowsort",l_slowsort}, + {"bogosort",l_bogosort}, + + {"unpack", l_unpack}, + + {NULL,NULL} }; static struct config table_config[] = { @@ -5,7 +5,7 @@ int ld_match(lua_State* L){ parray_t* a = parray_init(); int o = match_param((char*)lua_tostring(L, 1), (char*)lua_tostring(L, 2), a); - + if(o == 0){ lua_pushinteger(L, o); return 1; @@ -25,9 +25,9 @@ int ld_match(lua_State* L){ int l_stack_dump(lua_State* L){ /*StkId a = L->top.p-2; - printf("%i %i\n", ttype(s2v(a)), LUA_TSTRING); - printf("is string? %i\n", ttisstring(&a->val)); - printf("%s\n", tsvalue(&a->val)->contents);*/ + printf("%i %i\n", ttype(s2v(a)), LUA_TSTRING); + printf("is string? %i\n", ttisstring(&a->val)); + printf("%s\n", tsvalue(&a->val)->contents);*/ //int level = 0; //lua_lock(L); //for(CallInfo* ci = L->ci; ci != &L->base_ci; ci = ci->previous) level++; diff --git a/src/thread.c b/src/thread.c index 48ddba1..b113915 100644 --- a/src/thread.c +++ b/src/thread.c @@ -11,11 +11,11 @@ #include "table.h"
struct thread_info {
- str* function;
- lua_State* L;
- int return_count, done;
- pthread_t tid;
- pthread_mutex_t* lock;
+ str* function;
+ lua_State* L;
+ int return_count, done;
+ pthread_t tid;
+ pthread_mutex_t* lock;
};
#include "io.h"
@@ -34,55 +34,55 @@ void lib_thread_clean(){ free(thread_locks->arr[i].value);
}
}
-
+
larray_clear(thread_locks);
}
int l_tlock(lua_State* L){
- int idx = luaL_checkinteger(L, 1);
+ int idx = luaL_checkinteger(L, 1);
+
+ pthread_mutex_lock(&thread_lock_lock);
+ //pthread_mutex_lock(&thread_priority_lock);
+ //pthread_mutex_unlock(&thread_priority_lock);
+ pthread_mutex_t mutex;
+ if(thread_locks == NULL) thread_locks = larray_init();
+ int i = 0;
+ if((i = larray_geti(thread_locks, idx)) == -1){
+ pthread_mutex_init(&mutex, NULL);
+ pthread_mutex_lock(&mutex);
+ pthread_mutex_t* mp = malloc(sizeof * mp);
+ *mp = mutex;
+ larray_set(&thread_locks, idx, (void*)mp);
+ } else {
+ pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
+ pthread_mutex_lock(&thread_priority_lock);
+ pthread_mutex_unlock(&thread_lock_lock);
+ pthread_mutex_lock(m);
pthread_mutex_lock(&thread_lock_lock);
- //pthread_mutex_lock(&thread_priority_lock);
- //pthread_mutex_unlock(&thread_priority_lock);
- pthread_mutex_t mutex;
- if(thread_locks == NULL) thread_locks = larray_init();
- int i = 0;
- if((i = larray_geti(thread_locks, idx)) == -1){
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_lock(&mutex);
- pthread_mutex_t* mp = malloc(sizeof * mp);
- *mp = mutex;
- larray_set(&thread_locks, idx, (void*)mp);
- } else {
- pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
- pthread_mutex_lock(&thread_priority_lock);
-
- pthread_mutex_unlock(&thread_lock_lock);
- pthread_mutex_lock(m);
- pthread_mutex_lock(&thread_lock_lock);
-
- pthread_mutex_unlock(&thread_priority_lock);
- thread_locks->arr[i].value = (void*)m;
- }
-
- pthread_mutex_unlock(&thread_lock_lock);
- return 0;
+ pthread_mutex_unlock(&thread_priority_lock);
+ thread_locks->arr[i].value = (void*)m;
+
+ }
+
+ pthread_mutex_unlock(&thread_lock_lock);
+ return 0;
}
int l_tunlock(lua_State* L){
- int idx = luaL_checkinteger(L, 1);
+ int idx = luaL_checkinteger(L, 1);
- pthread_mutex_lock(&thread_lock_lock);
- int i = 0;
- if(thread_locks != NULL && (i = larray_geti(thread_locks, idx)) != -1){
- pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
+ pthread_mutex_lock(&thread_lock_lock);
+ int i = 0;
+ if(thread_locks != NULL && (i = larray_geti(thread_locks, idx)) != -1){
+ pthread_mutex_t *m = (pthread_mutex_t*)thread_locks->arr[i].value;
- pthread_mutex_unlock(m);
- thread_locks->arr[i].value = (void*)m;
- }
+ pthread_mutex_unlock(m);
+ thread_locks->arr[i].value = (void*)m;
+ }
- pthread_mutex_unlock(&thread_lock_lock);
- return 0;
+ pthread_mutex_unlock(&thread_lock_lock);
+ return 0;
}
int _mutex_lock(lua_State* L){
@@ -145,27 +145,27 @@ int l_mutex(lua_State* L){ }
int l_res(lua_State* L){
- int return_count = lua_gettop(L) - 1;
- lua_pushstring(L, "_");
- lua_gettable(L, 1);
- struct thread_info* info = lua_touserdata(L, -1);
- info->return_count = return_count;
+ int return_count = lua_gettop(L) - 1;
+ lua_pushstring(L, "_");
+ lua_gettable(L, 1);
+ struct thread_info* info = lua_touserdata(L, -1);
+ info->return_count = return_count;
- for(int i = info->return_count - 1; i != -1; i--){
- int ot = lua_gettop(L);
+ for(int i = info->return_count - 1; i != -1; i--){
+ int ot = lua_gettop(L);
- lua_pushvalue(L, 2 + i);
- luaI_deepcopy(L, info->L, 0);
+ lua_pushvalue(L, 2 + i);
+ luaI_deepcopy(L, info->L, 0);
- lua_settop(L, ot);
- }
-
- pthread_mutex_unlock(&*info->lock);
+ lua_settop(L, ot);
+ }
+
+ pthread_mutex_unlock(&*info->lock);
- pthread_exit(NULL);
- p_error("thread did not exit");
+ pthread_exit(NULL);
+ p_error("thread did not exit");
- return 1;
+ return 1;
}
void* handle_thread(void* _args){
@@ -204,7 +204,7 @@ int _thread_await(lua_State* L){ struct thread_info* info = lua_touserdata(L, -1);
if(info->L == NULL) luaI_error(L, -1, "thread was already closed")
- if(!info->done) pthread_mutex_lock(&*info->lock);
+ if(!info->done) pthread_mutex_lock(&*info->lock);
info->done = 1;
env_table(info->L, 0);
@@ -216,20 +216,20 @@ int _thread_await(lua_State* L){ lua_setglobal(L, "_locals");
for(int i = 0; i != info->return_count; i++){
- int ot = lua_gettop(info->L);
+ int ot = lua_gettop(info->L);
- lua_pushvalue(info->L, ot - info->return_count + i);
+ lua_pushvalue(info->L, ot - info->return_count + i);
- luaI_deepcopy(info->L, L, 0);
-
- int type = lua_type(info->L, ot - info->return_count + i);
- if(type == LUA_TTABLE || type == LUA_TUSERDATA){
- lua_getmetatable(info->L, ot - info->return_count + i);
- int idx = lua_gettop(info->L);
- luaI_tsetnil(info->L, idx, "__gc");
- }
+ luaI_deepcopy(info->L, L, 0);
+
+ int type = lua_type(info->L, ot - info->return_count + i);
+ if(type == LUA_TTABLE || type == LUA_TUSERDATA){
+ lua_getmetatable(info->L, ot - info->return_count + i);
+ int idx = lua_gettop(info->L);
+ luaI_tsetnil(info->L, idx, "__gc");
+ }
- lua_settop(info->L, ot);
+ lua_settop(info->L, ot);
}
lua_pushnil(L);
@@ -251,7 +251,7 @@ int _thread_clean(lua_State* L){ lua_close(info->L);
info->L = NULL;
-
+
pthread_mutex_destroy(&*info->lock);
free(info->lock);
pthread_cancel(info->tid);
@@ -276,18 +276,18 @@ int l_async(lua_State* oL){ luaL_openlibs(L);
luaI_copyvars(oL, L);
-
+
struct thread_info* args = calloc(1, sizeof * args);
args->L = L;
args->lock = malloc(sizeof * args->lock);
pthread_mutex_init(&*args->lock, NULL);
pthread_mutex_lock(&*args->lock);
args->return_count = 0;
-
+
args->function = str_init("");
lua_pushvalue(oL, 1);
lua_dump(oL, writer, (void*)args->function, 0);
-
+
pthread_create(&args->tid, NULL, handle_thread, (void*)args);
lua_newtable(oL);
@@ -343,7 +343,7 @@ int _buffer_mod(lua_State* L){ //printf("%p\n", &*buffer->lock);
assert(used == 0);
used = 1;
-
+
luaI_deepcopy(buffer->L, L, SKIP_GC | SKIP_LOCALS);
int item = lua_gettop(L);
lua_pushvalue(L, 2);
@@ -412,7 +412,7 @@ int meta_proxy(lua_State* L){ lua_getmetatable(buffer->L, 1);
lua_pushstring(buffer->L, lua_tostring(L, 2));
lua_gettable(buffer->L, 2);
-
+
lua_pushvalue(buffer->L, 1);
int count = 0;
@@ -438,7 +438,7 @@ void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, in lua_pushcfunction(L, meta_proxy);
lua_setglobal(L, "__proxy_call");
-
+
lua_pushlightuserdata(L, buffer);
lua_setglobal(L, "__this_obj");
@@ -453,7 +453,7 @@ void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, in char* fn = calloc(128, sizeof * fn);
const char* key = lua_tostring(L, k);
sprintf(fn, "return function(...)\
-return __proxy_call(__this_obj,'%s',...);end", key);
+ return __proxy_call(__this_obj,'%s',...);end", key);
luaL_dostring(L, fn);
free(fn);
@@ -508,7 +508,7 @@ int l_buffer(lua_State* L){ }
void _lua_getfenv(lua_State* L){
-
+
}
int l_testcopy(lua_State* L){
diff --git a/src/types/larray.c b/src/types/larray.c index 2e9a5b4..776a088 100644 --- a/src/types/larray.c +++ b/src/types/larray.c @@ -7,74 +7,74 @@ #define inc 4
larray_t* larray_initl(int len){
- larray_t* l = calloc(1, sizeof * l);
- l->size = len;
- l->arr = calloc(len, sizeof * l->arr);
- return l;
+ larray_t* l = calloc(1, sizeof * l);
+ l->size = len;
+ l->arr = calloc(len, sizeof * l->arr);
+ return l;
}
larray_t* larray_init(){
- return larray_initl(inc);
+ return larray_initl(inc);
}
void larray_expand(larray_t** _l){
- larray_t* l = *_l;
- larray_t* remade = larray_initl(l->size * 4);
- for(int i = 0; i != l->size; i++){
- //what happens if the map_set calls map_regraph??? idk
- if(l->arr[i].used)
- larray_set(&remade, l->arr[i].idx, l->arr[i].value);
- }
-
- *_l = remade;
+ larray_t* l = *_l;
+ larray_t* remade = larray_initl(l->size * 4);
+ for(int i = 0; i != l->size; i++){
+ //what happens if the map_set calls map_regraph??? idk
+ if(l->arr[i].used)
+ larray_set(&remade, l->arr[i].idx, l->arr[i].value);
+ }
+
+ *_l = remade;
}
int larray_set(larray_t** _l, uint64_t idx, void* value){
- larray_t* l = *_l;
+ larray_t* l = *_l;
- if(l->len + 1 >= l->size){
- expand:
- larray_expand(&l);
- }
+ if(l->len + 1 >= l->size){
+expand:
+ larray_expand(&l);
+ }
- uint64_t oind, ind = oind = idx % l->size;
+ uint64_t oind, ind = oind = idx % l->size;
- for(int count = 0; l->arr[ind].used && l->arr[ind].idx != idx; count++){
- ind++;
- if(ind >= l->size) ind = 0;
- if(ind == oind || count > 10) goto expand;
- }
+ for(int count = 0; l->arr[ind].used && l->arr[ind].idx != idx; count++){
+ ind++;
+ if(ind >= l->size) ind = 0;
+ if(ind == oind || count > 10) goto expand;
+ }
- l->arr[ind].idx = idx;
- l->arr[ind].value = value;
- l->arr[ind].used = 1;
- l->len++;
+ l->arr[ind].idx = idx;
+ l->arr[ind].value = value;
+ l->arr[ind].used = 1;
+ l->len++;
- *_l = l;
- return ind;
+ *_l = l;
+ return ind;
}
int larray_geti(larray_t* l, uint64_t idx){
- uint64_t ind = idx % l->size;
-
- for(uint64_t initial = ind; ind != initial - 1;){
- if(!l->arr[ind].used) return -1;
- //printf("%s\n",M->M[ind].key->c);
- if(l->arr[ind].idx == idx) return ind;
- ind++;
- if(ind >= l->size) ind = 0;
- }
- return -1;
+ uint64_t ind = idx % l->size;
+
+ for(uint64_t initial = ind; ind != initial - 1;){
+ if(!l->arr[ind].used) return -1;
+ //printf("%s\n",M->M[ind].key->c);
+ if(l->arr[ind].idx == idx) return ind;
+ ind++;
+ if(ind >= l->size) ind = 0;
+ }
+ return -1;
}
void* larray_get(larray_t* l, uint64_t idx){
- int r = larray_geti(l, idx);
+ int r = larray_geti(l, idx);
- return r == -1 ? NULL : l->arr[r].value;
+ return r == -1 ? NULL : l->arr[r].value;
}
void larray_clear(larray_t* l){
- free(l->arr);
- free(l);
+ free(l->arr);
+ free(l);
}
diff --git a/src/types/larray.h b/src/types/larray.h index 1213758..51fea80 100644 --- a/src/types/larray.h +++ b/src/types/larray.h @@ -2,14 +2,14 @@ #include <stdlib.h>
struct larray_item {
- uint64_t idx;
- void* value;
- int used;
+ uint64_t idx;
+ void* value;
+ int used;
};
typedef struct {
- struct larray_item* arr;
- size_t len, size;
+ struct larray_item* arr;
+ size_t len, size;
} larray_t;
larray_t* larray_initl(int len);
diff --git a/src/types/map.c b/src/types/map.c index 80e072c..10ece92 100644 --- a/src/types/map.c +++ b/src/types/map.c @@ -7,114 +7,114 @@ #define mod_inc 4
uint64_t hash(char* c, size_t len){
- return fnv_1((uint8_t*)c, len, v_a);
+ return fnv_1((uint8_t*)c, len, v_a);
}
void map_dump(map_t* M){
- printf("---\n%i %i\n- **\n",M->mod, M->len);
- for(int i = 0; i != M->mod; i++){
- if(M->M[i].used){
- printf("%i | %s : %p\n",i,M->M[i].key->c, M->M[i].value);
- }
- }
+ printf("---\n%i %i\n- **\n",M->mod, M->len);
+ for(int i = 0; i != M->mod; i++){
+ if(M->M[i].used){
+ printf("%i | %s : %p\n",i,M->M[i].key->c, M->M[i].value);
+ }
+ }
}
map_t* map_initl(size_t len){
- map_t* awa = calloc(sizeof * awa, 1);
- awa->M = calloc(sizeof * awa->M, len);
- //for(int i = 0; i != len; i++) awa->M[i].used = 0;
- awa->len = 0;
- awa->mod = len;
- return awa;
+ map_t* awa = calloc(sizeof * awa, 1);
+ awa->M = calloc(sizeof * awa->M, len);
+ //for(int i = 0; i != len; i++) awa->M[i].used = 0;
+ awa->len = 0;
+ awa->mod = len;
+ return awa;
}
map_t* map_init(){
- return map_initl(4);
+ return map_initl(mod_inc);
}
//TODO: make this better:3
void map_expand(map_t** _M){
- map_t* M = *_M;
- map_t* remade = map_initl(M->mod * 4);
- for(int i = 0; i != M->mod; i++){
- //what happens if the map_set calls map_regraph??? idk
- if(M->M[i].used){
- map_set(&remade, M->M[i].key->c, M->M[i].value);
- str_free(M->M[i].key);
- }
+ map_t* M = *_M;
+ map_t* remade = map_initl(M->mod * mod_inc);
+ for(int i = 0; i != M->mod; i++){
+ //what happens if the map_set calls map_regraph??? idk
+ if(M->M[i].used){
+ map_set(&remade, M->M[i].key->c, M->M[i].value);
+ str_free(M->M[i].key);
}
+ }
- map_lclear(M);
- *_M = remade;
+ map_lclear(M);
+ *_M = remade;
}
void map_set(map_t** _M, char* key, void* value){
- map_t* M = *_M;
- uint64_t h = hash(key, strlen(key));
-
- if(M->len + 1 >= M->mod){
- expand:
- map_expand(&M);
- }
- uint64_t ind = h % M->mod;
- uint64_t oind = ind;
-
- //iterates until there is a free space
- for(int count = 0; M->M[ind].used && M->M[ind].hash != h && strcmp(M->M[ind].key->c, key) != 0; count++){
- ind++;
- if(ind >= M->mod) ind = 0;
- if(ind == oind || count > 10) goto expand;
- }
-
- M->M[ind].hash = h;
- M->M[ind].key = str_init(key);
- M->M[ind].value = value;
- M->M[ind].used = 1;
- M->len++;
-
- *_M = M;
+ map_t* M = *_M;
+ uint64_t h = hash(key, strlen(key));
+
+ if(M->len + 1 >= M->mod){
+expand:
+ map_expand(&M);
+ }
+ uint64_t ind = h % M->mod;
+ uint64_t oind = ind;
+
+ //iterates until there is a free space
+ for(int count = 0; M->M[ind].used && M->M[ind].hash != h && strcmp(M->M[ind].key->c, key) != 0; count++){
+ ind++;
+ if(ind >= M->mod) ind = 0;
+ if(ind == oind || count > 10) goto expand;
+ }
+
+ M->M[ind].hash = h;
+ M->M[ind].key = str_init(key);
+ M->M[ind].value = value;
+ M->M[ind].used = 1;
+ M->len++;
+
+ *_M = M;
}
int map_geti(map_t* M, char* key){
- uint64_t h = hash(key, strlen(key));
- uint64_t ind = h % M->mod;
-
- for(uint64_t initial = ind; ind != initial - 1;){
- if(M->M[ind].key == NULL) return -1;
- //printf("%s\n",M->M[ind].key->c);
- 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;
+ uint64_t h = hash(key, strlen(key));
+ uint64_t ind = h % M->mod;
+
+ for(uint64_t initial = ind; ind != initial - 1;){
+ if(M->M[ind].key == NULL) return -1;
+ //printf("%s\n",M->M[ind].key->c);
+ 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;
+ 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){
- int ind = map_geti(p, key);
- if(ind == -1) return;
- p->M[ind].used = 0;
- p->M[ind].hash = 0;
- str_free(p->M[ind].key);
- free_method(p->M[ind].value, free);
+ int ind = map_geti(p, key);
+ if(ind == -1) return;
+ p->M[ind].used = 0;
+ p->M[ind].hash = 0;
+ str_free(p->M[ind].key);
+ free_method(p->M[ind].value, free);
}
void map_lclear(map_t* M){
- free(M->M);
- free(M);
+ free(M->M);
+ free(M);
}
void map_clear(map_t* M, enum free_type free){
- for(int i = 0; i != M->mod; i++){
- if(M->M[i].used){
- str_free(M->M[i].key);
- free_method(M->M[i].value, free);
- }
+ for(int i = 0; i != M->mod; i++){
+ if(M->M[i].used){
+ str_free(M->M[i].key);
+ free_method(M->M[i].value, free);
}
- map_lclear(M);
+ }
+ map_lclear(M);
}
diff --git a/src/types/map.h b/src/types/map.h index 2603f47..279bed1 100644 --- a/src/types/map.h +++ b/src/types/map.h @@ -7,16 +7,16 @@ #include "parray.h"
typedef struct {
- void* value;
- str* key;
- uint64_t hash;
- int used;
+ void* value;
+ str* key;
+ uint64_t hash;
+ int used;
} melem_t;
typedef struct {
- melem_t* M;
- int len;
- int mod;
+ melem_t* M;
+ int len;
+ int mod;
} map_t;
map_t* map_init();
@@ -27,4 +27,4 @@ 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 +#endif //_MAP_H
diff --git a/src/types/parray.c b/src/types/parray.c index b1e41a2..22f6be9 100644 --- a/src/types/parray.c +++ b/src/types/parray.c @@ -11,24 +11,24 @@ *
* @param {void*} value to be free'd
* @param {enum free_type} type of free
-*/
+ */
void free_method(void* v, enum free_type free_meth){
- if(v != NULL){
- if(free_meth == FREE) free(v);
- else if(free_meth == STR) str_free(v);
- }
+ if(v != NULL){
+ if(free_meth == FREE) free(v);
+ else if(free_meth == STR) str_free(v);
+ }
}
/**
* @brief defines a parray_t
*
* @return {parray_t*} allocated parray_t
-*/
+ */
parray_t* parray_init(){
- parray_t* awa = malloc(sizeof * awa);
- awa->P = malloc(sizeof * awa->P);
- awa->len = 0;
- return awa;
+ parray_t* awa = malloc(sizeof * awa);
+ awa->P = malloc(sizeof * awa->P);
+ awa->len = 0;
+ return awa;
}
parray_t* parray_initl(int len){
@@ -44,19 +44,19 @@ parray_t* parray_initl(int len){ * {parray_t*} the parray to update
* {char*} key
* {void*} value
-*/
+ */
void parray_set(parray_t* p, char* key, void* value){
- for(int i = 0; i != p->len; i++){
- if(strcmp(p->P[i].key->c, key) == 0){
- p->P[i].value = value;
- return;
- }
+ for(int i = 0; i != p->len; i++){
+ if(strcmp(p->P[i].key->c, key) == 0){
+ p->P[i].value = value;
+ return;
}
+ }
- p->len++;
- p->P = realloc(p->P, sizeof * p->P * (p->len + 1));
- p->P[p->len - 1].key = str_init(key);
- p->P[p->len - 1].value = value;
+ p->len++;
+ p->P = realloc(p->P, sizeof * p->P * (p->len + 1));
+ p->P[p->len - 1].key = str_init(key);
+ p->P[p->len - 1].value = value;
}
/**
@@ -65,14 +65,14 @@ void parray_set(parray_t* p, char* key, void* value){ * @param {parray_t*} the parray to search
* @param {char*} key
* @return value at index, or NULL
-*/
+ */
void* parray_get(parray_t* p, char* key){
- for(int i = 0; i != p->len; i++){
- if(strcmp(p->P[i].key->c, key) == 0){
- return p->P[i].value;
- }
+ for(int i = 0; i != p->len; i++){
+ if(strcmp(p->P[i].key->c, key) == 0){
+ return p->P[i].value;
}
- return NULL;
+ }
+ return NULL;
}
/**
@@ -81,14 +81,14 @@ void* parray_get(parray_t* p, char* key){ * @param {parray_t*} the parray to search
* @param {char*} key
* @return index, or -1 if not found
-*/
+ */
int parray_geti(parray_t* p, char* key){
- for(int i = 0; i != p->len; i++){
- if(strcmp(p->P[i].key->c, key) == 0){
- return i;
- }
+ for(int i = 0; i != p->len; i++){
+ if(strcmp(p->P[i].key->c, key) == 0){
+ return i;
}
- return -1;
+ }
+ return -1;
}
/**
@@ -97,27 +97,27 @@ int parray_geti(parray_t* p, char* key){ * @param {parray_t*} the parray to modify
* @param {char*} key
* @param {enum free_type} method to free value
-*/
+ */
void parray_remove(parray_t* p, char* key, enum free_type free){
- int ind = parray_geti(p, key);
- if(ind == -1) return;
+ int ind = parray_geti(p, key);
+ if(ind == -1) return;
- str_free(p->P[ind].key);
- free_method(p->P[ind].value, free);
+ str_free(p->P[ind].key);
+ free_method(p->P[ind].value, free);
- for(int i = ind; i < p->len - 1; i++) p->P[i] = p->P[i+1];
- p->len--;
- p->P = realloc(p->P, sizeof * p->P * (p->len + 1));
+ for(int i = ind; i < p->len - 1; i++) p->P[i] = p->P[i+1];
+ p->len--;
+ p->P = realloc(p->P, sizeof * p->P * (p->len + 1));
}
/**
* @brief frees base of parray_t, leaving the values
*
* @param {parray_t*} the parray free
-*/
+ */
void parray_lclear(parray_t* p){
- free(p->P);
- free(p);
+ free(p->P);
+ free(p);
}
/**
@@ -125,36 +125,36 @@ void parray_lclear(parray_t* p){ *
* @param {parray_t*} the parray free
* @param {enum free_type} the method to free values
-*/
+ */
void parray_clear(parray_t* p, enum free_type clear_val){
- for(int i = 0; i != p->len; i++){
- str_free(p->P[i].key);
- free_method(p->P[i].value, clear_val);
- }
- parray_lclear(p);
+ for(int i = 0; i != p->len; i++){
+ str_free(p->P[i].key);
+ free_method(p->P[i].value, clear_val);
+ }
+ parray_lclear(p);
}
int fmatch(char* s, char* p) {
- int slen = strlen(s);
- int plen = strlen(p);
- int sidx = 0, pidx = 0, lastWildcardIdx = -1, sBacktrackIdx = -1, nextToWildcardIdx = -1;
- for(;sidx < slen;) {
- if (pidx < plen && (p[pidx] == '?' || p[pidx] == s[sidx])) {
- sidx++;
- pidx++;
- } else if (pidx < plen && p[pidx] == '*') {
- lastWildcardIdx = pidx;
- nextToWildcardIdx = ++pidx;
- sBacktrackIdx = sidx;
- } else if (lastWildcardIdx == -1) {
- return 0;
- } else {
- pidx = nextToWildcardIdx;
- sidx = sBacktrackIdx++;
- }
+ int slen = strlen(s);
+ int plen = strlen(p);
+ int sidx = 0, pidx = 0, lastWildcardIdx = -1, sBacktrackIdx = -1, nextToWildcardIdx = -1;
+ for(;sidx < slen;) {
+ if (pidx < plen && (p[pidx] == '?' || p[pidx] == s[sidx])) {
+ sidx++;
+ pidx++;
+ } else if (pidx < plen && p[pidx] == '*') {
+ lastWildcardIdx = pidx;
+ nextToWildcardIdx = ++pidx;
+ sBacktrackIdx = sidx;
+ } else if (lastWildcardIdx == -1) {
+ return 0;
+ } else {
+ pidx = nextToWildcardIdx;
+ sidx = sBacktrackIdx++;
}
- for(int i = pidx; i < plen; i++) if(p[i] != '*') return 0;
- return 1;
+ }
+ for(int i = pidx; i < plen; i++) if(p[i] != '*') return 0;
+ return 1;
}
/**
@@ -163,16 +163,16 @@ int fmatch(char* s, char* p) { * @param {parray_t*} the parray to search
* @param {char*} the string to search for
* @return {parray_t*} populated array of matches
-*/
+ */
parray_t* parray_find(parray_t* p, char* match){
- parray_t* ret = malloc(sizeof * ret);
- ret->P = malloc(sizeof * ret->P * p->len);
- ret->len = 0;
- for(int i = 0; i != p->len; i++){
- if(fmatch(match, p->P[i].key->c)){
- ret->P[ret->len] = p->P[i];
- ret->len++;
- }
+ parray_t* ret = malloc(sizeof * ret);
+ ret->P = malloc(sizeof * ret->P * p->len);
+ ret->len = 0;
+ for(int i = 0; i != p->len; i++){
+ if(fmatch(match, p->P[i].key->c)){
+ ret->P[ret->len] = p->P[i];
+ ret->len++;
}
- return ret;
+ }
+ return ret;
}
diff --git a/src/types/parray.h b/src/types/parray.h index 7972cc1..374a073 100644 --- a/src/types/parray.h +++ b/src/types/parray.h @@ -2,18 +2,20 @@ #ifndef __PARRAY_H
#define __PARRAY_H
+#include "str.h"
+
typedef struct {
- void* value;
- str* key;
+ void* value;
+ str* key;
} pelem_t;
typedef struct {
- pelem_t* P;
- int len;
+ pelem_t* P;
+ int len;
} parray_t;
enum free_type {
- NONE = 0, FREE = 1, STR = 2
+ NONE = 0, FREE = 1, STR = 2
};
parray_t* parray_init();
diff --git a/src/types/str.c b/src/types/str.c index e1818a2..9799367 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -11,7 +11,7 @@ str* str_initl(const char* init, size_t len){ s->c = malloc(s->_bytes);
if(s->c == NULL) p_fatal("failed to allocate string\n");
s->len = len ;
-
+
memcpy(s->c, init, (len + 1) * sizeof * init);
return s;
}
@@ -23,7 +23,7 @@ str* str_initfl(const char* init, size_t len){ s->c = malloc(s->_bytes);
if(s->c == NULL) p_fatal("failed to allocate string\n");
s->len = len ;
-
+
memcpy(s->c, init, (len) * sizeof * init);
s->c[len] = '\0';
return s;
@@ -59,8 +59,8 @@ char* strnstr(const char *s1, const char *s2, size_t n) { void _p_fatal(const char* m, int line, const char* file, const char* function){
fprintf(stderr, "%s[fatal] %s \n"
- "\tthread: %zu\n"
- "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), file, function, line, color_reset);
+ "\tthread: %zu\n"
+ "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), file, function, line, color_reset);
exit(EXIT_FAILURE);
}
@@ -26,10 +26,10 @@ #define lesser(A,B) ((A)>(B)?(B):(A)) #define inter(V,I) (I * ceil((double)V / I)) #define time_start(name)\ - clock_t _begin##name = clock(); + clock_t _begin##name = clock(); #define time_end(desc, name)\ - clock_t _end##name = clock();\ - printf("%s took %f\n",desc, (double)(_end##name - _begin##name) / CLOCKS_PER_SEC); + clock_t _end##name = clock();\ + printf("%s took %f\n",desc, (double)(_end##name - _begin##name) / CLOCKS_PER_SEC); int gen_parse(char*,int, parray_t**); |
