aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/encodings.c
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2015-03-23 14:27:30 +0100
committerKim Alvefur <zash@zash.se>2015-03-23 14:27:30 +0100
commitb9f798f83a4dd10e44ccb2e6fa680f3911e67764 (patch)
treeb862ffc9baf72931734f7489d0e11cdc2c64f2fa /util-src/encodings.c
parent7bf2c14e9f702d8bcb0aa0db64fabad272d19d5d (diff)
downloadprosody-b9f798f83a4dd10e44ccb2e6fa680f3911e67764.tar.gz
prosody-b9f798f83a4dd10e44ccb2e6fa680f3911e67764.zip
util.encodings: Expose UTF-8 validation and length checking functions
Diffstat (limited to 'util-src/encodings.c')
-rw-r--r--util-src/encodings.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/util-src/encodings.c b/util-src/encodings.c
index 898add1a..91826ca4 100644
--- a/util-src/encodings.c
+++ b/util-src/encodings.c
@@ -175,6 +175,29 @@ const char* check_utf8 (lua_State *L, int idx, size_t *l) {
return s;
}
+static int Lutf8_valid(lua_State *L) {
+ lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
+ return 1;
+}
+
+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_utf8[] =
+{
+ { "valid", Lutf8_valid },
+ { "length", Lutf8_length },
+ { NULL, NULL }
+};
+
/***************** STRINGPREP *****************/
#ifdef USE_STRINGPREP_ICU
@@ -452,6 +475,11 @@ LUALIB_API int luaopen_util_encodings(lua_State *L)
luaL_register(L, NULL, Reg_idna);
lua_settable(L,-3);
+ lua_pushliteral(L, "utf8");
+ lua_newtable(L);
+ luaL_register(L, NULL, Reg_utf8);
+ lua_settable(L, -3);
+
lua_pushliteral(L, "version"); /** version */
lua_pushliteral(L, "-3.14");
lua_settable(L,-3);