- break;\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
+ break;
+ case 0:
+ luaL_pushresult(&b);
+ return 1;
+ case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
+ break;
+ }
+ }
+}
+
+static const luaL_Reg Reg_base64[] =
+{
+ { "encode", Lbase64_encode },
+ { "decode", Lbase64_decode },
+ { NULL, NULL }
+};
+
+/***************** STRINGPREP *****************/
+
+#include <stringprep.h>
+
+static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
+{
+ size_t len;
+ const char *s;
+ char string[1024];
+ int ret;
+ if(!lua_isstring(L, 1)) {
+ lua_pushnil(L);
+ return 1;
+ }
+ s = lua_tolstring(L, 1, &len);
+ if (len >= 1024) {
+ lua_pushnil(L);
+ return 1; // TODO return error message
+ }
+ strcpy(string, s);
+ ret = stringprep(string, 1024, 0, profile);
+ if (ret == STRINGPREP_OK) {
+ lua_pushstring(L, string);
+ return 1;
+ } else {
+ lua_pushnil(L);
+ return 1; // TODO return error message
+ }
+}
+
+#define MAKE_PREP_FUNC(myFunc, prep) \
+static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
+
+MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
+MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
+MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
+MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
+
+static const luaL_Reg Reg_stringprep[] =
+{
+ { "nameprep", Lstringprep_nameprep },
+ { "nodeprep", Lstringprep_nodeprep },
+ { "resourceprep", Lstringprep_resourceprep },
+ { "saslprep", Lstringprep_saslprep },
+ { NULL, NULL }
+};
+
+/***************** IDNA *****************/
+
+#include <idna.h>
+#include <idn-free.h>
+
+static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
+{
+ size_t len;
+ const char *s = luaL_checklstring(L, 1, &len);
+ char* output = NULL;
+ int ret = idna_to_ascii_8z(s, &output, 0);
+ if (ret == IDNA_SUCCESS) {
+ lua_pushstring(L, output);
+ idn_free(output);
+ return 1;
+ } else {
+ lua_pushnil(L);
+ idn_free(output);
+ return 1; // TODO return error message
+ }
+}
+
+static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
+{
+ size_t len;
+ const char *s = luaL_checklstring(L, 1, &len);
+ char* output = NULL;
+ int ret = idna_to_unicode_8z8z(s, &output, 0);
+ if (ret == IDNA_SUCCESS) {
+ lua_pushstring(L, output);
+ idn_free(output);
+ return 1;
+ } else {
+ lua_pushnil(L);
+ idn_free(output);
+ return 1; // TODO return error message
+ }
+}
+
+static const luaL_Reg Reg_idna[] =
+{
+ { "to_ascii", Lidna_to_ascii },
+ { "to_unicode", Lidna_to_unicode },
+ { NULL, NULL }
+};
+
+/***************** end *****************/
+
+static const luaL_Reg Reg[] =
+{
+ { NULL, NULL }
+};
+
+LUALIB_API int luaopen_util_encodings(lua_State *L)
+{
+ luaL_register(L, "encodings", Reg);
+
+ lua_pushliteral(L, "base64");
+ lua_newtable(L);
+ luaL_register(L, NULL, Reg_base64);
+ lua_settable(L,-3);
+
+ lua_pushliteral(L, "stringprep");
+ lua_newtable(L);
+ luaL_register(L, NULL, Reg_stringprep);
+ lua_settable(L,-3);
+
+ lua_pushliteral(L, "idna");
+ lua_newtable(L);
+ luaL_register(L, NULL, Reg_idna);
+ lua_settable(L,-3);
+
+ lua_pushliteral(L, "version"); /** version */
+ lua_pushliteral(L, "-3.14");
+ lua_settable(L,-3);
+ return 1;
+}