diff --git a/src/encoding.c b/src/encoding.c index 4e162b0..f20fef8 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -19,6 +19,7 @@ */ #include +#include static const int32_t hextable[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, @@ -43,6 +44,20 @@ int hex2buf(char *dst, const char *hex) { return(i); } +// takes binary buffer and its bytes length, requires pre-allocation +// of dst string +static const char hexes[] = "0123456789abcdef"; +void buf2hex(char *dst, const char *buf, const size_t len) { + register size_t i; + register unsigned char ch; + for (i=0; i>4]; + dst[(i<<1)+1] = hexes[ch & 0xf]; + } + dst[len<<1] = 0x0; // null termination +} + static const unsigned char asciitable[256] = { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, diff --git a/src/encoding.h b/src/encoding.h index 2e01d99..148fc48 100644 --- a/src/encoding.h +++ b/src/encoding.h @@ -21,7 +21,10 @@ #ifndef __ENCODING_H__ #define __ENCODING_H__ +#include + int hex2buf(char *dst, const char *hex); +void buf2hex(char *dst, const char *buf, const size_t len); int is_url64(const char *in); diff --git a/src/zen_octet.c b/src/zen_octet.c index 8cb3049..c9e3359 100644 --- a/src/zen_octet.c +++ b/src/zen_octet.c @@ -116,15 +116,9 @@ int is_base64(const char *in) { return c; } -inline void push_octet_to_hex_string(lua_State *L, octet *o) { - int odlen = o->len<<1; - char *s = zen_memory_alloc(odlen+1); - register int i; - register unsigned char ch; - for (i=0; ilen; i++) { - ch=o->val[i]; z_sprintf(&s[i<<1],"%02x", ch); - } - s[odlen] = '\0'; +void push_octet_to_hex_string(lua_State *L, octet *o) { + char *s = zen_memory_alloc((o->len<<1)+1); // string len = double +1 + buf2hex(s, o->val, o->len); lua_pushstring(L,s); zen_memory_free(s); return; @@ -509,25 +503,18 @@ static int from_string(lua_State *L) { } static int from_hex(lua_State *L) { - // const long hextable[] = { - // [0 ... 255] = -1, - // ['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - // ['A'] = 10, 11, 12, 13, 14, 15, - // ['a'] = 10, 11, 12, 13, 14, 15 - // }; const char *s = lua_tostring(L, 1); if(!s) { error(L, "%s :: invalid argument",__func__); lua_pushboolean(L,0); return 1; } - // luaL_argcheck(L, s != NULL, 1, "hex string sequence expected"); int len = is_hex(s); func(L,"hex string sequence length: %u",len); if(!len || len>MAX_FILE<<1) { // *2 hex tuples error(L, "hex sequence too long: %u bytes", len<<1); lua_pushboolean(L,0); return 1; } - octet *o = o_new(L, len); // TODO: can be halved + octet *o = o_new(L, len>>1); o->len = hex2buf(o->val,s); return 1; }