Merge 0.9->0.10 again
[prosody.git] / util-src / encodings.c
index 2a4653fbf15979ca525046dc48d112686ecf9063..5fa9706b63bbe306c09ed96fe0cfd3417f85528b 100644 (file)
@@ -1,6 +1,7 @@
 /* Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 1994-2015 Lua.org, PUC-Rio.
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 #include "lua.h"
 #include "lauxlib.h"
 
+#if (LUA_VERSION_NUM == 502)
+#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
+#endif
+
 /***************** BASE64 *****************/
 
 static const char code[]=
@@ -116,56 +121,91 @@ static const luaL_Reg Reg_base64[] =
        { NULL,         NULL    }
 };
 
-/***************** STRINGPREP *****************/
-#ifndef USE_STRINGPREP_ICU
-/****************** libidn ********************/
+/******************* UTF-8 ********************/
 
-#include <stringprep.h>
+/*
+ * Adapted from Lua 5.3
+ * Needed because libidn does not validate that input is valid UTF-8
+ */
 
-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;
+#define MAXUNICODE     0x10FFFF
+
+/*
+ * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
+ */
+static const char *utf8_decode (const char *o, int *val) {
+       static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
+       const unsigned char *s = (const unsigned char *)o;
+       unsigned int c = s[0];
+       unsigned int res = 0;  /* final result */
+       if (c < 0x80)  /* ascii? */
+               res = c;
+       else {
+               int count = 0;  /* to count number of continuation bytes */
+               while (c & 0x40) {  /* still have continuation bytes? */
+                       int cc = s[++count];  /* read next byte */
+                       if ((cc & 0xC0) != 0x80)  /* not a continuation byte? */
+                               return NULL;  /* invalid byte sequence */
+                       res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
+                       c <<= 1;  /* to test next bit */
+               }
+               res |= ((c & 0x7F) << (count * 5));  /* add first byte */
+               if (count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff) )
+                       return NULL;  /* invalid byte sequence */
+               s += count;  /* skip continuation bytes read */
        }
-       s = lua_tolstring(L, 1, &len);
-       if (len >= 1024) {
-               lua_pushnil(L);
-               return 1; /* TODO return error message */
+       if (val) *val = res;
+       return (const char *)s + 1;  /* +1 to include first byte */
+}
+
+/*
+ * Check that a string is valid UTF-8
+ * Returns NULL if not
+ */
+const char* check_utf8 (lua_State *L, int idx, size_t *l) {
+       size_t pos, len;
+       const char *s = luaL_checklstring(L, 1, &len);
+       pos = 0;
+       while (pos <= len) {
+               const char *s1 = utf8_decode(s + pos, NULL);
+               if (s1 == NULL) {  /* conversion error? */
+                       return NULL;
+               }
+               pos = s1 - s;
        }
-       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 */
+       if(l != NULL) {
+               *l = len;
        }
+       return s;
 }
 
-#define MAKE_PREP_FUNC(myFunc, prep) \
-static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
+static int Lutf8_valid(lua_State *L) {
+       lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
+       return 1;
+}
 
-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 int Lutf8_length(lua_State *L) {
+       size_t len;
+       if(!check_utf8(L, 1, &len)) {
+               lua_pushnil(L);
+               lua_pushliteral(L, "invalid utf8");
+               return 2;
+       }
+       lua_pushinteger(L, len);
+       return 1;
+}
 
-static const luaL_Reg Reg_stringprep[] =
+static const luaL_Reg Reg_utf8[] =
 {
-       { "nameprep",   Lstringprep_nameprep    },
-       { "nodeprep",   Lstringprep_nodeprep    },
-       { "resourceprep",       Lstringprep_resourceprep        },
-       { "saslprep",   Lstringprep_saslprep    },
+       { "valid",      Lutf8_valid     },
+       { "length",     Lutf8_length    },
        { NULL,         NULL    }
 };
 
-#else
+
+/***************** STRINGPREP *****************/
+#ifdef USE_STRINGPREP_ICU
+
 #include <unicode/usprep.h>
 #include <unicode/ustring.h>
 #include <unicode/utrace.h>
@@ -192,13 +232,17 @@ 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)) {
+               lua_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(output_len < 1024)
+               if (U_SUCCESS(err) && output_len < 1024)
                        lua_pushlstring(L, output, output_len);
                else
                        lua_pushnil(L);
@@ -239,49 +283,58 @@ static const luaL_Reg Reg_stringprep[] =
        { "saslprep",   Lstringprep_saslprep    },
        { NULL,         NULL    }
 };
-#endif
+#else /* USE_STRINGPREP_ICU */
 
-/***************** IDNA *****************/
-#ifndef USE_STRINGPREP_ICU
 /****************** libidn ********************/
 
-#include <idna.h>
-#include <idn-free.h>
+#include <stringprep.h>
 
-static int Lidna_to_ascii(lua_State *L)                /** idna.to_ascii(s) */
+static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
 {
        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);
+       const char *s;
+       char string[1024];
+       int ret;
+       if(!lua_isstring(L, 1)) {
+               lua_pushnil(L);
                return 1;
-       } else {
+       }
+       s = check_utf8(L, 1, &len);
+       if (s == NULL || len >= 1024 || len != strlen(s)) {
                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);
+       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);
-               idn_free(output);
                return 1; /* TODO return error message */
        }
 }
-#else
+
+#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
 #include <unicode/ustdio.h>
 #include <unicode/uidna.h>
 /* IDNA2003 or IDNA2008 ? ? ? */
@@ -296,13 +349,18 @@ 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(output_len < 1024)
+               if (U_SUCCESS(err) && output_len < 1024)
                        lua_pushlstring(L, output, output_len);
                else
                        lua_pushnil(L);
@@ -315,25 +373,74 @@ 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;
+       UChar ustr[1024];
        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(output_len < 1024)
+               if (U_SUCCESS(err) && 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 = check_utf8(L, 1, &len);
+       if (s == NULL || len != strlen(s)) {
+               lua_pushnil(L);
+               return 1; /* TODO return error message */
+       }
+       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[] =
@@ -345,35 +452,30 @@ static const luaL_Reg Reg_idna[] =
 
 /***************** end *****************/
 
-static const luaL_Reg Reg[] =
-{
-       { NULL,         NULL    }
-};
-
 LUALIB_API int luaopen_util_encodings(lua_State *L)
 {
 #ifdef USE_STRINGPREP_ICU
        init_icu();
 #endif
-       luaL_register(L, "encodings", Reg);
+       lua_newtable(L);
 
-       lua_pushliteral(L, "base64");
        lua_newtable(L);
        luaL_register(L, NULL, Reg_base64);
-       lua_settable(L,-3);
+       lua_setfield(L, -2, "base64");
 
-       lua_pushliteral(L, "stringprep");
        lua_newtable(L);
        luaL_register(L, NULL, Reg_stringprep);
-       lua_settable(L,-3);
+       lua_setfield(L, -2, "stringprep");
 
-       lua_pushliteral(L, "idna");
        lua_newtable(L);
        luaL_register(L, NULL, Reg_idna);
-       lua_settable(L,-3);
+       lua_setfield(L, -2, "idna");
+
+       lua_newtable(L);
+       luaL_register(L, NULL, Reg_utf8);
+       lua_setfield(L, -2, "utf8");
 
-       lua_pushliteral(L, "version");                  /** version */
        lua_pushliteral(L, "-3.14");
-       lua_settable(L,-3);
+       lua_setfield(L, -2, "version");
        return 1;
 }