-/*\r
-* encodings.c\r
-* Lua library for base64, stringprep and idna encodings\r
-*/\r
-\r
-// Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way\r
-#define _CRT_SECURE_NO_DEPRECATE\r
-\r
-#include <string.h>\r
-#include <stdlib.h>\r
-\r
-#include "lua.h"\r
-#include "lauxlib.h"\r
-\r
-/***************** BASE64 *****************/\r
-\r
-static const char code[]=\r
-"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
-\r
-static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)\r
-{\r
- unsigned long tuple=c3+256UL*(c2+256UL*c1);\r
- int i;\r
- char s[4];\r
- for (i=0; i<4; i++) {\r
- s[3-i] = code[tuple % 64];\r
- tuple /= 64;\r
- }\r
- for (i=n+1; i<4; i++) s[i]='=';\r
- luaL_addlstring(b,s,4);\r
-}\r
-\r
-static int Lbase64_encode(lua_State *L) /** encode(s) */\r
-{\r
- size_t l;\r
- const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);\r
- luaL_Buffer b;\r
- int n;\r
- luaL_buffinit(L,&b);\r
- for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);\r
- switch (l%3)\r
- {\r
- case 1: base64_encode(&b,s[0],0,0,1); break;\r
- case 2: base64_encode(&b,s[0],s[1],0,2); break;\r
- }\r
- luaL_pushresult(&b);\r
- return 1;\r
-}\r
-\r
-static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)\r
-{\r
- unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));\r
- char s[3];\r
- switch (--n)\r
- {\r
- case 3: s[2]=(char) tuple;\r
- case 2: s[1]=(char) (tuple >> 8);\r
- case 1: s[0]=(char) (tuple >> 16);\r
- }\r
- luaL_addlstring(b,s,n);\r
-}\r
-\r
-static int Lbase64_decode(lua_State *L) /** decode(s) */\r
-{\r
- size_t l;\r
- const char *s=luaL_checklstring(L,1,&l);\r
- luaL_Buffer b;\r
- int n=0;\r
- char t[4];\r
- luaL_buffinit(L,&b);\r
- for (;;)\r
- {\r
- int c=*s++;\r
- switch (c)\r
- {\r
- const char *p;\r
- default:\r
- p=strchr(code,c); if (p==NULL) return 0;\r
- t[n++]= (char) (p-code);\r
- if (n==4)\r
- {\r
- base64_decode(&b,t[0],t[1],t[2],t[3],4);\r
- n=0;\r
- }\r
- break;\r
- case '=':\r
- switch (n)\r
- {\r
- case 1: base64_decode(&b,t[0],0,0,0,1); break;\r
- case 2: base64_decode(&b,t[0],t[1],0,0,2); break;\r
- case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;\r
- }\r
- case 0:\r
- luaL_pushresult(&b);\r
- return 1;\r
- case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':\r
- break;\r
- }\r
- }\r
- return 0;\r
-}\r
-\r
-static const luaL_Reg Reg_base64[] =\r
-{\r
- { "encode", Lbase64_encode },\r
- { "decode", Lbase64_decode },\r
- { NULL, NULL }\r
-};\r
-\r
-/***************** STRINGPREP *****************/\r
-\r
-#include <stringprep.h>\r
-\r
-static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)\r
-{\r
- size_t len;\r
- const char *s = luaL_checklstring(L, 1, &len);\r
- char string[1024];\r
- int ret;\r
- if (len >= 1024) {\r
- lua_pushnil(L);\r
- return 1; // TODO return error message\r
- }\r
- strcpy(string, s);\r
- ret = stringprep(string, 1024, 0, profile);\r
- if (ret == STRINGPREP_OK) {\r
- lua_pushstring(L, string);\r
- return 1;\r
- } else {\r
- lua_pushnil(L);\r
- return 1; // TODO return error message\r
- }\r
-}\r
-\r
-#define MAKE_PREP_FUNC(myFunc, prep) \\r
-static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }\r
-\r
-MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */\r
-MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */\r
-MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */\r
-MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */\r
-\r
-static const luaL_Reg Reg_stringprep[] =\r
-{\r
- { "nameprep", Lstringprep_nameprep },\r
- { "nodeprep", Lstringprep_nodeprep },\r
- { "resourceprep", Lstringprep_resourceprep },\r
- { "saslprep", Lstringprep_saslprep },\r
- { NULL, NULL }\r
-};\r
-\r
-/***************** IDNA *****************/\r
-\r
-#include <idna.h>\r
-\r
-static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */\r
-{\r
- size_t len;\r
- const char *s = luaL_checklstring(L, 1, &len);\r
- char* output = NULL;\r
- int ret = idna_to_ascii_8z(s, &output, 0);\r
- if (ret == IDNA_SUCCESS) {\r
- lua_pushstring(L, output);\r
- if (output) free(output);\r
- return 1;\r
- } else {\r
- lua_pushnil(L);\r
- if (output) free(output);\r
- return 1; // TODO return error message\r
- }\r
-}\r
-\r
-static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */\r
-{\r
- size_t len;\r
- const char *s = luaL_checklstring(L, 1, &len);\r
- char* output = NULL;\r
- int ret = idna_to_unicode_8z8z(s, &output, 0);\r
- if (ret == IDNA_SUCCESS) {\r
- lua_pushstring(L, output);\r
- if (output) free(output);\r
- return 1;\r
- } else {\r
- lua_pushnil(L);\r
- if (output) free(output);\r
- return 1; // TODO return error message\r
- }\r
-}\r
-\r
-static const luaL_Reg Reg_idna[] =\r
-{\r
- { "to_ascii", Lidna_to_ascii },\r
- { "to_unicode", Lidna_to_unicode },\r
- { NULL, NULL }\r
-};\r
-\r
-/***************** end *****************/\r
-\r
-static const luaL_Reg Reg[] =\r
-{\r
- { NULL, NULL }\r
-};\r
-\r
-LUALIB_API int luaopen_util_encodings(lua_State *L)\r
-{\r
- luaL_register(L, "encodings", Reg);\r
-\r
- lua_pushliteral(L, "base64");\r
- lua_newtable(L);\r
- luaL_register(L, NULL, Reg_base64);\r
- lua_settable(L,-3);\r
-\r
- lua_pushliteral(L, "stringprep");\r
- lua_newtable(L);\r
- luaL_register(L, NULL, Reg_stringprep);\r
- lua_settable(L,-3);\r
-\r
- lua_pushliteral(L, "idna");\r
- lua_newtable(L);\r
- luaL_register(L, NULL, Reg_idna);\r
- lua_settable(L,-3);\r
-\r
- lua_pushliteral(L, "version"); /** version */\r
- lua_pushliteral(L, "-3.14");\r
- lua_settable(L,-3);\r
- return 1;\r
-}\r