2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 1994-2015 Lua.org, PUC-Rio.
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
13 * Lua library for base64, stringprep and idna encodings
16 /* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
17 #define _CRT_SECURE_NO_DEPRECATE
24 #if (LUA_VERSION_NUM == 502)
25 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
28 /***************** BASE64 *****************/
30 static const char code[]=
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
35 unsigned long tuple=c3+256UL*(c2+256UL*c1);
39 s[3-i] = code[tuple % 64];
42 for (i=n+1; i<4; i++) s[i]='=';
43 luaL_addlstring(b,s,4);
46 static int Lbase64_encode(lua_State *L) /** encode(s) */
49 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
53 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
56 case 1: base64_encode(&b,s[0],0,0,1); break;
57 case 2: base64_encode(&b,s[0],s[1],0,2); break;
63 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
65 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
69 case 3: s[2]=(char) tuple;
70 case 2: s[1]=(char) (tuple >> 8);
71 case 1: s[0]=(char) (tuple >> 16);
73 luaL_addlstring(b,s,n);
76 static int Lbase64_decode(lua_State *L) /** decode(s) */
79 const char *s=luaL_checklstring(L,1,&l);
91 p=strchr(code,c); if (p==NULL) return 0;
92 t[n++]= (char) (p-code);
95 base64_decode(&b,t[0],t[1],t[2],t[3],4);
102 case 1: base64_decode(&b,t[0],0,0,0,1); break;
103 case 2: base64_decode(&b,t[0],t[1],0,0,2); break;
104 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
111 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
117 static const luaL_Reg Reg_base64[] =
119 { "encode", Lbase64_encode },
120 { "decode", Lbase64_decode },
124 /******************* UTF-8 ********************/
127 * Adapted from Lua 5.3
128 * Needed because libidn does not validate that input is valid UTF-8
131 #define MAXUNICODE 0x10FFFF
134 * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
136 static const char *utf8_decode (const char *o, int *val) {
137 static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
138 const unsigned char *s = (const unsigned char *)o;
139 unsigned int c = s[0];
140 unsigned int res = 0; /* final result */
141 if (c < 0x80) /* ascii? */
144 int count = 0; /* to count number of continuation bytes */
145 while (c & 0x40) { /* still have continuation bytes? */
146 int cc = s[++count]; /* read next byte */
147 if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
148 return NULL; /* invalid byte sequence */
149 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
150 c <<= 1; /* to test next bit */
152 res |= ((c & 0x7F) << (count * 5)); /* add first byte */
153 if (count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff) )
154 return NULL; /* invalid byte sequence */
155 s += count; /* skip continuation bytes read */
158 return (const char *)s + 1; /* +1 to include first byte */
162 * Check that a string is valid UTF-8
163 * Returns NULL if not
165 const char* check_utf8 (lua_State *L, int idx, size_t *l) {
167 const char *s = luaL_checklstring(L, 1, &len);
170 const char *s1 = utf8_decode(s + pos, NULL);
171 if (s1 == NULL) { /* conversion error? */
182 static int Lutf8_valid(lua_State *L) {
183 lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
187 static int Lutf8_length(lua_State *L) {
189 if(!check_utf8(L, 1, &len)) {
191 lua_pushliteral(L, "invalid utf8");
194 lua_pushinteger(L, len);
198 static const luaL_Reg Reg_utf8[] =
200 { "valid", Lutf8_valid },
201 { "length", Lutf8_length },
206 /***************** STRINGPREP *****************/
207 #ifdef USE_STRINGPREP_ICU
209 #include <unicode/usprep.h>
210 #include <unicode/ustring.h>
211 #include <unicode/utrace.h>
213 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
216 int32_t unprepped_len, prepped_len, output_len;
220 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
223 UErrorCode err = U_ZERO_ERROR;
225 if(!lua_isstring(L, 1)) {
229 input = lua_tolstring(L, 1, &input_len);
230 if (input_len >= 1024) {
234 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
235 if (U_FAILURE(err)) {
239 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
240 if (U_FAILURE(err)) {
244 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
245 if (U_SUCCESS(err) && output_len < 1024)
246 lua_pushlstring(L, output, output_len);
253 UStringPrepProfile *icu_nameprep;
254 UStringPrepProfile *icu_nodeprep;
255 UStringPrepProfile *icu_resourceprep;
256 UStringPrepProfile *icu_saslprep;
258 /* initialize global ICU stringprep profiles */
261 UErrorCode err = U_ZERO_ERROR;
262 utrace_setLevel(UTRACE_VERBOSE);
263 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
264 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
265 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
266 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
267 if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
270 #define MAKE_PREP_FUNC(myFunc, prep) \
271 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
273 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
274 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
275 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
276 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
278 static const luaL_Reg Reg_stringprep[] =
280 { "nameprep", Lstringprep_nameprep },
281 { "nodeprep", Lstringprep_nodeprep },
282 { "resourceprep", Lstringprep_resourceprep },
283 { "saslprep", Lstringprep_saslprep },
286 #else /* USE_STRINGPREP_ICU */
288 /****************** libidn ********************/
290 #include <stringprep.h>
292 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
298 if(!lua_isstring(L, 1)) {
302 s = check_utf8(L, 1, &len);
303 if (s == NULL || len >= 1024 || len != strlen(s)) {
305 return 1; /* TODO return error message */
308 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
309 if (ret == STRINGPREP_OK) {
310 lua_pushstring(L, string);
314 return 1; /* TODO return error message */
318 #define MAKE_PREP_FUNC(myFunc, prep) \
319 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
321 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
322 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
323 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
324 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
326 static const luaL_Reg Reg_stringprep[] =
328 { "nameprep", Lstringprep_nameprep },
329 { "nodeprep", Lstringprep_nodeprep },
330 { "resourceprep", Lstringprep_resourceprep },
331 { "saslprep", Lstringprep_saslprep },
336 /***************** IDNA *****************/
337 #ifdef USE_STRINGPREP_ICU
338 #include <unicode/ustdio.h>
339 #include <unicode/uidna.h>
340 /* IDNA2003 or IDNA2008 ? ? ? */
341 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
344 int32_t ulen, dest_len, output_len;
345 const char *s = luaL_checklstring(L, 1, &len);
347 UErrorCode err = U_ZERO_ERROR;
351 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
352 if (U_FAILURE(err)) {
357 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
358 if (U_FAILURE(err)) {
362 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
363 if (U_SUCCESS(err) && output_len < 1024)
364 lua_pushlstring(L, output, output_len);
371 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
374 int32_t ulen, dest_len, output_len;
375 const char *s = luaL_checklstring(L, 1, &len);
377 UErrorCode err = U_ZERO_ERROR;
381 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
382 if (U_FAILURE(err)) {
387 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
388 if (U_FAILURE(err)) {
392 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
393 if (U_SUCCESS(err) && output_len < 1024)
394 lua_pushlstring(L, output, output_len);
401 #else /* USE_STRINGPREP_ICU */
402 /****************** libidn ********************/
405 #include <idn-free.h>
407 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
410 const char *s = check_utf8(L, 1, &len);
411 if (s == NULL || len != strlen(s)) {
413 return 1; /* TODO return error message */
416 int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
417 if (ret == IDNA_SUCCESS) {
418 lua_pushstring(L, output);
424 return 1; /* TODO return error message */
428 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
431 const char *s = luaL_checklstring(L, 1, &len);
433 int ret = idna_to_unicode_8z8z(s, &output, 0);
434 if (ret == IDNA_SUCCESS) {
435 lua_pushstring(L, output);
441 return 1; /* TODO return error message */
446 static const luaL_Reg Reg_idna[] =
448 { "to_ascii", Lidna_to_ascii },
449 { "to_unicode", Lidna_to_unicode },
453 /***************** end *****************/
455 LUALIB_API int luaopen_util_encodings(lua_State *L)
457 #ifdef USE_STRINGPREP_ICU
463 luaL_register(L, NULL, Reg_base64);
464 lua_setfield(L, -2, "base64");
467 luaL_register(L, NULL, Reg_stringprep);
468 lua_setfield(L, -2, "stringprep");
471 luaL_register(L, NULL, Reg_idna);
472 lua_setfield(L, -2, "idna");
475 luaL_register(L, NULL, Reg_utf8);
476 lua_setfield(L, -2, "utf8");
478 lua_pushliteral(L, "-3.14");
479 lua_setfield(L, -2, "version");