Merge 0.7->0.8
[prosody.git] / util-src / encodings.c
index 9b6c6cf4c2cf8d22a9937af55977954e8e706297..2a4653fbf15979ca525046dc48d112686ecf9063 100644 (file)
@@ -117,8 +117,55 @@ static const luaL_Reg Reg_base64[] =
 };
 
 /***************** STRINGPREP *****************/
-#ifdef USE_STRINGPREP_ICU
+#ifndef USE_STRINGPREP_ICU
+/****************** libidn ********************/
+
+#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, (Stringprep_profile_flags)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    }
+};
 
+#else
 #include <unicode/usprep.h>
 #include <unicode/ustring.h>
 #include <unicode/utrace.h>
@@ -145,17 +192,13 @@ static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
                return 1;
        }
        u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
-       if (U_FAILURE(err)) {
-               luah_pushnil(L);
-               return 1;
-       }
        prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
        if (U_FAILURE(err)) {
                lua_pushnil(L);
                return 1;
        } else {
                u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
-               if (U_SUCCESS(err) && output_len < 1024)
+               if(output_len < 1024)
                        lua_pushlstring(L, output, output_len);
                else
                        lua_pushnil(L);
@@ -196,58 +239,49 @@ static const luaL_Reg Reg_stringprep[] =
        { "saslprep",   Lstringprep_saslprep    },
        { NULL,         NULL    }
 };
-#else /* USE_STRINGPREP_ICU */
+#endif
 
+/***************** IDNA *****************/
+#ifndef USE_STRINGPREP_ICU
 /****************** libidn ********************/
 
-#include <stringprep.h>
+#include <idna.h>
+#include <idn-free.h>
 
-static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
+static int Lidna_to_ascii(lua_State *L)                /** idna.to_ascii(s) */
 {
        size_t len;
-       const char *s;
-       char string[1024];
-       int ret;
-       if(!lua_isstring(L, 1)) {
-               lua_pushnil(L);
+       const char *s = luaL_checklstring(L, 1, &len);
+       char* output = NULL;
+       int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
+       if (ret == IDNA_SUCCESS) {
+               lua_pushstring(L, output);
+               idn_free(output);
                return 1;
-       }
-       s = lua_tolstring(L, 1, &len);
-       if (len >= 1024) {
+       } else {
                lua_pushnil(L);
+               idn_free(output);
                return 1; /* TODO return error message */
        }
-       strcpy(string, s);
-       ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
-       if (ret == STRINGPREP_OK) {
-               lua_pushstring(L, string);
+}
+
+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 */
        }
 }
-
-#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    }
-};
-#endif
-
-/***************** IDNA *****************/
-#ifdef USE_STRINGPREP_ICU
+#else
 #include <unicode/ustdio.h>
 #include <unicode/uidna.h>
 /* IDNA2003 or IDNA2008 ? ? ? */
@@ -262,18 +296,13 @@ static int Lidna_to_ascii(lua_State *L)           /** idna.to_ascii(s) */
        char output[1024];
 
        u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
-       if (U_FAILURE(err)) {
-               lua_pushnil(L);
-               return 1;
-       }
-
        dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
        if (U_FAILURE(err)) {
                lua_pushnil(L);
                return 1;
        } else {
                u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
-               if (U_SUCCESS(err) && output_len < 1024)
+               if(output_len < 1024)
                        lua_pushlstring(L, output, output_len);
                else
                        lua_pushnil(L);
@@ -286,70 +315,25 @@ static int Lidna_to_unicode(lua_State *L)         /** idna.to_unicode(s) */
        size_t len;
        int32_t ulen, dest_len, output_len;
        const char *s = luaL_checklstring(L, 1, &len);
-       UChar ustr[1024];
+       UChar* ustr;
        UErrorCode err = U_ZERO_ERROR;
        UChar dest[1024];
        char output[1024];
 
        u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
-       if (U_FAILURE(err)) {
-               lua_pushnil(L);
-               return 1;
-       }
-
        dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
        if (U_FAILURE(err)) {
                lua_pushnil(L);
                return 1;
        } else {
                u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
-               if (U_SUCCESS(err) && output_len < 1024)
+               if(output_len < 1024)
                        lua_pushlstring(L, output, output_len);
                else
                        lua_pushnil(L);
                return 1;
        }
 }
-
-#else /* USE_STRINGPREP_ICU */
-/****************** libidn ********************/
-
-#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, IDNA_USE_STD3_ASCII_RULES);
-       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 */
-       }
-}
 #endif
 
 static const luaL_Reg Reg_idna[] =