aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-03-27 13:34:34 -0500
committerame <[email protected]>2024-03-27 13:34:34 -0500
commit57d67eb910aa765df2495df9f625bfc00436014b (patch)
tree11db0e99732c9851c4a2c94f3e318da1cdec7716
parentb1f57a05f31a04b6d0dfbdfaf358ce8adfd45dda (diff)
fixed a few hashes
-rw-r--r--docs/crypto.md15
-rw-r--r--src/crypto.c8
-rw-r--r--src/crypto.h42
-rw-r--r--src/hash/adler.c31
-rw-r--r--src/hash/bsdchecksum.c50
-rw-r--r--src/hash/bsdchecksum.h12
-rw-r--r--src/hash/buzhash.c20
-rw-r--r--src/hash/buzhash.h4
-rw-r--r--src/hash/crc.c126
-rw-r--r--src/hash/crc.h63
-rw-r--r--src/net.c17
-rw-r--r--tests/hash.lua18
-rw-r--r--tests/net.lua2
13 files changed, 275 insertions, 133 deletions
diff --git a/docs/crypto.md b/docs/crypto.md
index 1f71f43..660bc18 100644
--- a/docs/crypto.md
+++ b/docs/crypto.md
@@ -8,11 +8,12 @@ sadly i didnt think about being able to update hashes, using the common init-upd
this is a pretty big problem meaning the input must be given at once, this is better for passwords,
but bad for big files. because of this, i decided not to support inputs over 2^64 characters (which is an
insane amount anyways). i likely will go back and rewrite all of these to fix both of these issues.
-anything marked with % is fixed
+anything marked with % is fixed,
|name|out len|other args|extra|
|--|--|--|--|
| % adler32 | 32 | nil | |
+| % bsdchecksum | 16 | nil | |
| sha0 | 160 | nil | insecure, use sha1|
| sha1 | 160 | nil | |
| sha256 | 256 | nil | |
@@ -28,11 +29,11 @@ anything marked with % is fixed
| fletcher32 | 32 | nil | |
| sysvchecksum | 32 | nil | |
| xor8 | 8 | nil | |
-| buzhash8 | 8 | nil | use setbuzhash(table) to change table (will affect all buzhash functions) |
+| buzhash8 | 8 | nil | use setbuzhash(table) to change table (will affect all buzhash functions), does not support updating |
| buzhash16 | 16 | nil | ^ |
-| cityhash32 | 32 | nil | |
-| cityhash64 | 64 | nil | |
-| cityhash128 | 128 | nil | |
+| cityhash32 | 32 | nil | does not support updating|
+| cityhash64 | 64 | nil | ^ |
+| cityhash128 | 128 | nil | ^ |
| md5 | 128 | nil | |
| djb2 | 64 | nil | |
| farmhash32 | 32 | nil | |
@@ -78,12 +79,12 @@ llib.crypto.sha512_t("meow", 224) -- would be sha512/224 - ad5e403e0d74532187f4e
functions supporting updates (listed with %, see note above) can be used like so:
```lua
-obj = llib.crypto.adler32_init()
+obj = llib.crypto.adler32() --adler32_init is equivilant to adler32 with no params
llib.crypto.adler32_update(obj, "meow")
local hash = llib.crypto.adler32_final(obj) --043c01b9
--or you can chain them!
-obj = llib.crypto.adler32_init()
+obj = llib.crypto.adler32()
obj:update("meow")
hash = obj:final() --043c01b9s (the same)
```
diff --git a/src/crypto.c b/src/crypto.c
index 8070eee..4985e0d 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -1,5 +1,13 @@
#include "crypto.h"
+uint8_t rotl8(uint8_t y, uint8_t offset){
+ return ( y << offset ) | ( y >> (8 - offset));
+}
+
+uint16_t rotl16(uint16_t y, uint16_t offset){
+ return ( y << offset ) | ( y >> (16 - offset));
+}
+
unsigned rotl32(unsigned y, unsigned offset){
return ( y << offset ) | ( y >> (32 - offset));
}
diff --git a/src/crypto.h b/src/crypto.h
index 2d3dcca..8bda7c5 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -32,20 +32,51 @@
#include "encode/base64.h"
#include "encode/baseN.h"
+uint8_t rotl8(uint8_t, uint8_t);
+uint16_t rotl16(uint16_t, uint16_t);
unsigned rotl32(unsigned, unsigned);
unsigned rotr32(unsigned, unsigned);
uint64_t rotl64(uint64_t, uint64_t);
uint64_t rotr64(uint64_t, uint64_t);
+#define common_hash_init_update(hashname)\
+ int l_##hashname##_init(lua_State* L){\
+ lua_newtable(L);\
+ int t = lua_gettop(L);\
+ \
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_newuserdata(L, sizeof * a);\
+ int ud = lua_gettop(L);\
+ *a = hashname##_init();\
+ \
+ luaI_tsetv(L, t, "ud", ud);\
+ luaI_tsetcf(L, t, "update", l_##hashname##_update);\
+ luaI_tsetcf(L, t, "final", l_##hashname##_final);\
+ \
+ lua_pushvalue(L, t);\
+ return 1;\
+}\
+\
+int l_##hashname##_update(lua_State* L){\
+ lua_pushstring(L, "ud");\
+ lua_gettable(L, 1);\
+ \
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ size_t len = 0;\
+ uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len);\
+ \
+ hashname##_update(b, len, a);\
+ \
+ lua_pushvalue(L, 1);\
+ return 1;\
+}
static const luaL_Reg crypto_function_list [] = {
{"sha0",l_sha0}, {"sha1",l_sha1}, {"sha256",l_sha256}, {"sha224",l_sha224},
{"setpearson",l_setpearson}, {"pearson",l_pearson}, {"xxh64",l_xxh64},
- {"xxh32",l_xxh32}, {"bsdchecksum",l_bsdchecksum},
- {"crc8",l_crc8}, {"crc16",l_crc16}, {"crc32",l_crc32}, {"fletcher8",l_fletcher8},
+ {"xxh32",l_xxh32}, {"fletcher8",l_fletcher8},
{"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
{"sysvchecksum",l_sysvchecksum}, {"xor8",l_xor8}, {"setbuzhash",l_setbuzhash},
- {"buzhash8",l_buzhash8}, {"buzhash16",l_buzhash16}, {"cityhash32", l_cityhash32},
+ {"cityhash32", l_cityhash32},
{"cityhash64", l_cityhash64}, {"cityhash128", l_cityhash128}, {"md5",l_md5},
{"djb2", l_djb2}, {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64},
{"fasthash32", l_fasthash32}, {"fasthash64", l_fasthash64}, {"fnv_0", l_fnv_0},
@@ -61,6 +92,11 @@ static const luaL_Reg crypto_function_list [] = {
{"blake224", l_blake224}, {"blake512", l_blake512}, {"blake384", l_blake384},
{"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},
{"uuencode",l_uuencode},
{"uudecode",l_uudecode},
diff --git a/src/hash/adler.c b/src/hash/adler.c
index 7d922e9..c6b4d7f 100644
--- a/src/hash/adler.c
+++ b/src/hash/adler.c
@@ -23,35 +23,7 @@ uint32_t adler32(uint8_t* aa, size_t len){
return adler32_final(&dig);
}
-int l_adler32_init(lua_State* L){
- lua_newtable(L);
- int t = lua_gettop(L);
-
- struct adler32_hash* a = (struct adler32_hash*)lua_newuserdata(L, sizeof * a);
- int ud = lua_gettop(L);
- *a = adler32_init();
-
- luaI_tsetv(L, t, "ud", ud);
- luaI_tsetcf(L, t, "update", l_adler32_update);
- luaI_tsetcf(L, t, "final", l_adler32_final);
-
- lua_pushvalue(L, t);
- return 1;
-}
-
-int l_adler32_update(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, -1);
- size_t len = 0;
- uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len);
-
- adler32_update(b, len, a);
-
- lua_pushvalue(L, 1);
- return 1;
-}
+common_hash_init_update(adler32);
int l_adler32_final(lua_State* L){
lua_pushstring(L, "ud");
@@ -66,6 +38,7 @@ int l_adler32_final(lua_State* L){
}
int l_adler32(lua_State* L){
+ if(lua_gettop(L) == 0) return l_adler32_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c
index 9904352..a816b5c 100644
--- a/src/hash/bsdchecksum.c
+++ b/src/hash/bsdchecksum.c
@@ -2,26 +2,52 @@
#include <stdio.h>
#include <stdint.h>
-uint16_t i_bsdchecksum(uint8_t *aa, size_t len){
- uint16_t check = 0x0;
-
- for(int i = 0; i != len; i++){
- uint8_t a = aa[i];
- check = (check >> 1) + ((check & 1) << 15);
- check += a;
- check &= 0xffff;
- }
- return check;
+struct bsdchecksum_hash bsdchecksum_init(){
+ return (struct bsdchecksum_hash){.check = 0x0};
+}
+
+void bsdchecksum_update(uint8_t* aa, size_t len, struct bsdchecksum_hash* hash){
+ for(int i = 0; i != len; i++){
+ uint8_t a = aa[i];
+ hash->check = (hash->check >> 1) + ((hash->check & 1) << 15);
+ hash->check += a;
+ hash->check &= 0xffff;
+ }
+}
+
+uint16_t bsdchecksum_final(struct bsdchecksum_hash* hash){
+ return hash->check;
+}
+
+uint16_t bsdchecksum(uint8_t* a, size_t len){
+ struct bsdchecksum_hash b = bsdchecksum_init();
+ bsdchecksum_update(a, len, &b);
+ return bsdchecksum_final(&b);
+}
+
+common_hash_init_update(bsdchecksum);
+
+int l_bsdchecksum_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, -1);
+ uint32_t u = bsdchecksum_final(a);
+ char digest[32];
+ sprintf(digest,"%i",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_bsdchecksum(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_bsdchecksum_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[16];
- uint16_t u = i_bsdchecksum(a, len);
+ //uint16_t u = i_bsdchecksum(a, len);
+ uint16_t u = bsdchecksum(a, len);
sprintf(digest,"%i",u);
lua_pushstring(L, digest);
diff --git a/src/hash/bsdchecksum.h b/src/hash/bsdchecksum.h
index 8e41893..9344e9a 100644
--- a/src/hash/bsdchecksum.h
+++ b/src/hash/bsdchecksum.h
@@ -1,6 +1,10 @@
#include "../lua.h"
#include <stdint.h>
+struct bsdchecksum_hash {
+ uint16_t check;
+};
+
/**
* calculates a bsdchecksum of (len) bytes
*
@@ -8,6 +12,12 @@
* @param {size_t} input length
* @return {uint16_t} 16 bit checksum
*/
-uint16_t i_bsdchecksum(uint8_t*, size_t);
+uint16_t bsdchecksum(uint8_t*, size_t);
+struct bsdchecksum_hash bsdchecksum_init();
+void bsdchecksum_update(uint8_t*, size_t, struct bsdchecksum_hash*);
+uint16_t bsdchecksum_final(struct bsdchecksum_hash*);
int l_bsdchecksum(lua_State*);
+int l_bsdchecksum_init(lua_State*);
+int l_bsdchecksum_update(lua_State*);
+int l_bsdchecksum_final(lua_State*);
diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c
index 9e1b560..ec2705d 100644
--- a/src/hash/buzhash.c
+++ b/src/hash/buzhash.c
@@ -19,28 +19,20 @@ static uint8_t T[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
252,253,254,255};
-uint8_t i_lr8(uint8_t y, uint8_t offset){
- return ( y << offset ) | ( y >> (8 - offset));
-}
-
-uint16_t i_lr16(uint16_t y, uint16_t offset){
- return ( y << offset ) | ( y >> (16 - offset));
-}
-
-uint8_t i_buzhash8(uint8_t* in, size_t len){
+uint8_t buzhash8(uint8_t* in, size_t len){
uint8_t hash = 0;
for(int i = 0; i != len; i++){
- hash ^= i_lr8(T[(uint8_t)in[i]],len - (i + 1));
+ hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1));
}
return hash;
}
-uint16_t i_buzhash16(uint8_t* in, size_t len){
+uint16_t buzhash16(uint8_t* in, size_t len){
uint16_t hash = 0;
for(int i = 0; i != len; i++){
- hash ^= i_lr16(T[(uint8_t)in[i]],len - (i + 1));
+ hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1));
}
return hash;
@@ -72,7 +64,7 @@ int l_buzhash8(lua_State* L){
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[3];
- uint8_t u = i_buzhash8(a, len);
+ uint8_t u = buzhash8(a, len);
sprintf(digest,"%x",u);
@@ -86,7 +78,7 @@ int l_buzhash16(lua_State* L){
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[6];
- uint16_t u = i_buzhash16(a, len);
+ uint16_t u = buzhash16(a, len);
sprintf(digest,"%04x",u);
diff --git a/src/hash/buzhash.h b/src/hash/buzhash.h
index abc7fea..98f4929 100644
--- a/src/hash/buzhash.h
+++ b/src/hash/buzhash.h
@@ -1,8 +1,8 @@
#include "../lua.h"
#include <stdint.h>
-uint8_t i_buzhash8(uint8_t*, size_t);
-uint16_t i_buzhash16(uint8_t*, size_t);
+uint8_t buzhash8(uint8_t*, size_t);
+uint16_t buzhash16(uint8_t*, size_t);
int l_setbuzhash(lua_State*);
int l_buzhash8(lua_State*);
diff --git a/src/hash/crc.c b/src/hash/crc.c
index 5e4f8d7..7d9c41f 100644
--- a/src/hash/crc.c
+++ b/src/hash/crc.c
@@ -4,60 +4,132 @@
#include <stdio.h>
#include <stdint.h>
-uint32_t i_crc32(uint8_t *data, size_t len){
- uint32_t crc = 0xFFFFFFFF;
+struct crc32_hash crc32_init(){
+ return (struct crc32_hash){.crc = 0xFFFFFFFF};
+}
+void crc32_update(uint8_t* data, size_t len, struct crc32_hash* hash){
for(int i = 0; i < len; i++){
uint32_t extract = data[i];
for(int z = 0; z < 8; z++){
- uint32_t b = (extract^crc)&1;
- crc>>=1;
- if(b) crc^=0xEDB88320;
+ uint32_t b = (extract^hash->crc)&1;
+ hash->crc>>=1;
+ if(b) hash->crc^=0xEDB88320;
extract>>=1;
}
}
- return -(crc+1);
}
-uint16_t i_crc16(uint8_t *aa, size_t len){
- uint16_t crc = 0x0;
-
+uint32_t crc32_final(struct crc32_hash* hash){
+ return -(hash->crc+1);
+}
+
+uint32_t crc32(uint8_t* data, size_t len){
+ struct crc32_hash a = crc32_init();
+ crc32_update(data, len, &a);
+ return crc32_final(&a);
+}
+
+struct crc16_hash crc16_init(){
+ return (struct crc16_hash){.crc = 0x0};
+}
+
+void crc16_update(uint8_t *aa, size_t len, struct crc16_hash *hash){
for(int i = 0; i != len; i++){
uint8_t a = aa[i];
- crc ^= a;
+ hash->crc ^= a;
for (int z = 0; z < 8; z++){
- if (crc & 1) crc = (crc >> 1) ^ 0xA001;
- else crc = (crc >> 1);
+ if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001;
+ else hash->crc = (hash->crc >> 1);
}
}
- return crc;
}
-uint8_t i_crc8(uint8_t *aa, size_t len){
- //crc8 maxim
- uint8_t crc = 0x00;
-
+uint16_t crc16_final(struct crc16_hash *hash){
+ return hash->crc;
+}
+
+uint16_t crc16(uint8_t *aa, size_t len){
+ struct crc16_hash a = crc16_init();
+ crc16_update(aa, len, &a);
+ return crc16_final(&a);
+}
+
+struct crc8_hash crc8_init(){
+ return (struct crc8_hash){.crc = 0x00};
+}
+
+void crc8_update(uint8_t *aa, size_t len, struct crc8_hash *hash){
for(int i = 0; i != len; i++){
uint8_t a = aa[i];
for (int z = 0; z < 8; z++){
- uint8_t b = (crc ^ a) & 1;
- crc >>= 1;
- if(b) crc ^= 0x8c;
+ uint8_t b = (hash->crc ^ a) & 1;
+ hash->crc >>= 1;
+ if(b) hash->crc ^= 0x8c;
a >>=1;
}
}
- return crc;
+}
+
+uint8_t crc8_final(struct crc8_hash *hash){
+ return hash->crc;
+}
+
+uint8_t crc8(uint8_t *aa, size_t len){
+ struct crc8_hash a = crc8_init();
+ crc8_update(aa, len, &a);
+ return crc8_final(&a);
+}
+
+common_hash_init_update(crc32);
+common_hash_init_update(crc16);
+common_hash_init_update(crc8);
+
+int l_crc8_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, -1);
+ uint32_t u = crc8_final(a);
+ char digest[8];
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_crc16_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, -1);
+ uint32_t u = crc16_final(a);
+ char digest[16];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_crc32_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, -1);
+ uint32_t u = crc32_final(a);
+ char digest[8];
+ sprintf(digest,"%08lx",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_crc8(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_crc8_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[8];
- uint8_t u = i_crc8(a, len);
+ uint8_t u = crc8(a, len);
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
@@ -65,13 +137,13 @@ int l_crc8(lua_State* L){
}
int l_crc16(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_crc16_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[16];
- uint16_t u = i_crc16(a, len);
+ uint16_t u = crc16(a, len);
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
@@ -79,13 +151,13 @@ int l_crc16(lua_State* L){
}
int l_crc32(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_crc32_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[32];
- uint32_t u = i_crc32(a, len);
+ uint32_t u = crc32(a, len);
sprintf(digest,"%x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/crc.h b/src/hash/crc.h
index de61efd..b3fdbf9 100644
--- a/src/hash/crc.h
+++ b/src/hash/crc.h
@@ -1,33 +1,44 @@
#include "../lua.h"
#include <stdint.h>
-/**
- * calculates a crc of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint8_t} 8 bit checksum
-*/
-uint8_t i_crc8(uint8_t*, size_t);
-
-/**
- * calculates a crc of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint16_t} 16 bit checksum
-*/
-uint16_t i_crc16(uint8_t*, size_t);
-
-/**
- * calculates a crc of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint32_t} 32 bit checksum
-*/
-uint32_t i_crc32(uint8_t*, size_t);
+struct crc32_hash {
+ uint32_t crc;
+};
+
+struct crc16_hash {
+ uint16_t crc;
+};
+
+struct crc8_hash {
+ uint8_t crc;
+};
+
+uint8_t crc8(uint8_t*, size_t len);
+struct crc8_hash crc8_init();
+void crc8_update(uint8_t*, size_t, struct crc8_hash*);
+uint8_t crc8_final(struct crc8_hash*);
+
+uint16_t crc16(uint8_t*, size_t len);
+struct crc16_hash crc16_init();
+void crc16_update(uint8_t*, size_t, struct crc16_hash*);
+uint16_t crc16_final(struct crc16_hash*);
+
+uint32_t crc32(uint8_t*, size_t len);
+struct crc32_hash crc32_init();
+void crc32_update(uint8_t*, size_t, struct crc32_hash*);
+uint32_t crc32_final(struct crc32_hash*);
int l_crc8(lua_State*);
+int l_crc8_init(lua_State*);
+int l_crc8_update(lua_State*);
+int l_crc8_final(lua_State*);
+
int l_crc16(lua_State*);
+int l_crc16_init(lua_State*);
+int l_crc16_update(lua_State*);
+int l_crc16_final(lua_State*);
+
int l_crc32(lua_State*);
+int l_crc32_init(lua_State*);
+int l_crc32_update(lua_State*);
+int l_crc32_final(lua_State*);
diff --git a/src/net.c b/src/net.c
index 4f5e2b5..c829ead 100644
--- a/src/net.c
+++ b/src/net.c
@@ -68,7 +68,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
//printf("&_\n");
for(;;){
n = recv(client_fd, buffer + len, BUFFER_SIZE, 0);
-
+ printf("hi");
if(n < 0){
*_buffer = buffer;
printf("%s %i\n",strerror(errno),errno);
@@ -185,14 +185,15 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
void http_build(str** _dest, int code, 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);
-
*_dest = str_init(dest);
str_pushl(*_dest, content, len);
+
free(dest);
}
@@ -305,7 +306,6 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si
http_build(&resp, code, code_det, header_vs->c, content, len);
str_free(header_vs);
-
*_resp = resp;
}
@@ -394,13 +394,14 @@ int l_send(lua_State* L){
i_write_header(L, header, &resp, "", 0);
} else
i_write_header(L, header, &resp, content, len);
+
int a = send(client_fd, resp->c, resp->len, 0);
//
- //lua_pushstring(L, "client_fd");
- //lua_pushinteger(L, -1);
- //lua_settable(L, res_idx);
- //sclosesocket(client_fd);
+ lua_pushstring(L, "client_fd");
+ lua_pushinteger(L, -1);
+ lua_settable(L, res_idx);
+ closesocket(client_fd);
//printf("%i | %i\n'%s'\n%i\n",client_fd,a,resp->c,resp->len);
str_free(resp);
return 0;
@@ -514,7 +515,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
str_popb(boundary, 4);
parray_set(content, "_boundary", (void*)boundary);
parray_set(content, "_boundary_id", (void*)boundary_id);
-
+
}
if(*status == NORMAL){
diff --git a/tests/hash.lua b/tests/hash.lua
index 67155d3..ca96f34 100644
--- a/tests/hash.lua
+++ b/tests/hash.lua
@@ -3,6 +3,7 @@ require "llib"
function test(name,b,exp,oargs)
local hash
local hash2
+ local hash3
local add = ""
if oargs == nil then
hash = llib.crypto[name](b)
@@ -12,12 +13,24 @@ function test(name,b,exp,oargs)
end
if(llib.crypto[name.."_init"] ~= nil) then
- hash2 = llib.crypto[name.."_init"]():update(b):final()
+ hash2 = llib.crypto[name]():update(b):final()
+
+ hash3 = llib.crypto[name]()
+ b:gsub(".", function(c) hash3:update(c) end)
+ hash3 = hash3:final()
+
if(hash2 ~= hash) then
- llib.io.error(name.." init-update-final method not working, got:\n\t"..hash2.." other was:\n\t"..hash)
+ llib.io.error(name.." alt method not working, got:\n\t"..hash2.." other was:\n\t"..hash)
else
llib.io.log(name.." alt method working "..hash2.." == "..hash)
end
+
+ if(hash3 ~= hash) then
+ llib.io.error(name.." alt char-b-char method not working, got:\n\t"..hash3.." other was:\n\t"..hash)
+ else
+ llib.io.log(name.." alt char-b-char method working "..hash3.." == "..hash)
+ end
+
end
if not (hash == exp) then
@@ -32,7 +45,6 @@ test("bsdchecksum","meow","24789")
test("crc8","meow","a4")
test("crc16","meow","6561")
test("crc32","meow","8a106afe")
-test("crc32","meow","8a106afe")
test("fletcher8","meow","05")
test("fletcher16","meow","3cb9")
test("fletcher32","meow","043801b8")
diff --git a/tests/net.lua b/tests/net.lua
index 80a2315..53bcbe1 100644
--- a/tests/net.lua
+++ b/tests/net.lua
@@ -55,7 +55,7 @@ llib.net.listen(
--_G.llib.io.pprint(req)
--_G.llib.io.pprint(req)
--_G.llib.io.pprint("hi")
- res:send("")
+ --res:send("")
end)
server:GET("/aa", function(res, req)