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 /***************** BASE64 *****************/
26 static const char code[]=
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
31 unsigned long tuple=c3+256UL*(c2+256UL*c1);
35 s[3-i] = code[tuple % 64];
38 for (i=n+1; i<4; i++) s[i]='=';
39 luaL_addlstring(b,s,4);
42 static int Lbase64_encode(lua_State *L) /** encode(s) */
45 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
49 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
52 case 1: base64_encode(&b,s[0],0,0,1); break;
53 case 2: base64_encode(&b,s[0],s[1],0,2); break;
59 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
61 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
65 case 3: s[2]=(char) tuple;
66 case 2: s[1]=(char) (tuple >> 8);
67 case 1: s[0]=(char) (tuple >> 16);
69 luaL_addlstring(b,s,n);
72 static int Lbase64_decode(lua_State *L) /** decode(s) */
75 const char *s=luaL_checklstring(L,1,&l);
87 p=strchr(code,c); if (p==NULL) return 0;
88 t[n++]= (char) (p-code);
91 base64_decode(&b,t[0],t[1],t[2],t[3],4);
98 case 1: base64_decode(&b,t[0],0,0,0,1); break;
99 case 2: base64_decode(&b,t[0],t[1],0,0,2); break;
100 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
107 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
113 static const luaL_Reg Reg_base64[] =
115 { "encode", Lbase64_encode },
116 { "decode", Lbase64_decode },
120 /******************* UTF-8 ********************/
123 * Adapted from Lua 5.3
124 * Needed because libidn does not validate that input is valid UTF-8
127 #define MAXUNICODE 0x10FFFF
130 * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
132 static const char *utf8_decode (const char *o, int *val) {
133 static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
134 const unsigned char *s = (const unsigned char *)o;
135 unsigned int c = s[0];
136 unsigned int res = 0; /* final result */
137 if (c < 0x80) /* ascii? */
140 int count = 0; /* to count number of continuation bytes */
141 while (c & 0x40) { /* still have continuation bytes? */
142 int cc = s[++count]; /* read next byte */
143 if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
144 return NULL; /* invalid byte sequence */
145 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
146 c <<= 1; /* to test next bit */
148 res |= ((c & 0x7F) << (count * 5)); /* add first byte */
149 if (count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff) )
150 return NULL; /* invalid byte sequence */
151 s += count; /* skip continuation bytes read */
154 return (const char *)s + 1; /* +1 to include first byte */
158 * Check that a string is valid UTF-8
159 * Returns NULL if not
161 const char* check_utf8 (lua_State *L, int idx, size_t *l) {
163 const char *s = luaL_checklstring(L, 1, &len);
166 const char *s1 = utf8_decode(s + pos, NULL);
167 if (s1 == NULL) { /* conversion error? */
178 static int Lutf8_valid(lua_State *L) {
179 lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
183 static int Lutf8_length(lua_State *L) {
185 if(!check_utf8(L, 1, &len)) {
187 lua_pushliteral(L, "invalid utf8");
190 lua_pushinteger(L, len);
194 static const luaL_Reg Reg_utf8[] =
196 { "valid", Lutf8_valid },
197 { "length", Lutf8_length },
202 /***************** STRINGPREP *****************/
203 #ifdef USE_STRINGPREP_ICU
205 #include <unicode/usprep.h>
206 #include <unicode/ustring.h>
207 #include <unicode/utrace.h>
209 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
212 int32_t unprepped_len, prepped_len, output_len;
216 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
219 UErrorCode err = U_ZERO_ERROR;
221 if(!lua_isstring(L, 1)) {
225 input = lua_tolstring(L, 1, &input_len);
226 if (input_len >= 1024) {
230 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
231 if (U_FAILURE(err)) {
235 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
236 if (U_FAILURE(err)) {
240 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
241 if (U_SUCCESS(err) && output_len < 1024)
242 lua_pushlstring(L, output, output_len);
249 UStringPrepProfile *icu_nameprep;
250 UStringPrepProfile *icu_nodeprep;
251 UStringPrepProfile *icu_resourceprep;
252 UStringPrepProfile *icu_saslprep;
254 /* initialize global ICU stringprep profiles */
257 UErrorCode err = U_ZERO_ERROR;
258 utrace_setLevel(UTRACE_VERBOSE);
259 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
260 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
261 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
262 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
263 if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
266 #define MAKE_PREP_FUNC(myFunc, prep) \
267 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
269 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
270 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
271 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
272 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
274 static const luaL_Reg Reg_stringprep[] =
276 { "nameprep", Lstringprep_nameprep },
277 { "nodeprep", Lstringprep_nodeprep },
278 { "resourceprep", Lstringprep_resourceprep },
279 { "saslprep", Lstringprep_saslprep },
282 #else /* USE_STRINGPREP_ICU */
284 /****************** libidn ********************/
286 #include <stringprep.h>
288 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
294 if(!lua_isstring(L, 1)) {
298 s = check_utf8(L, 1, &len);
299 if (s == NULL || len >= 1024 || len != strlen(s)) {
301 return 1; /* TODO return error message */
304 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
305 if (ret == STRINGPREP_OK) {
306 lua_pushstring(L, string);
310 return 1; /* TODO return error message */
314 #define MAKE_PREP_FUNC(myFunc, prep) \
315 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
317 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
318 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
319 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
320 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
322 static const luaL_Reg Reg_stringprep[] =
324 { "nameprep", Lstringprep_nameprep },
325 { "nodeprep", Lstringprep_nodeprep },
326 { "resourceprep", Lstringprep_resourceprep },
327 { "saslprep", Lstringprep_saslprep },
332 /***************** IDNA *****************/
333 #ifdef USE_STRINGPREP_ICU
334 #include <unicode/ustdio.h>
335 #include <unicode/uidna.h>
336 /* IDNA2003 or IDNA2008 ? ? ? */
337 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
340 int32_t ulen, dest_len, output_len;
341 const char *s = luaL_checklstring(L, 1, &len);
343 UErrorCode err = U_ZERO_ERROR;
347 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
348 if (U_FAILURE(err)) {
353 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
354 if (U_FAILURE(err)) {
358 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
359 if (U_SUCCESS(err) && output_len < 1024)
360 lua_pushlstring(L, output, output_len);
367 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
370 int32_t ulen, dest_len, output_len;
371 const char *s = luaL_checklstring(L, 1, &len);
373 UErrorCode err = U_ZERO_ERROR;
377 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
378 if (U_FAILURE(err)) {
383 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
384 if (U_FAILURE(err)) {
388 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
389 if (U_SUCCESS(err) && output_len < 1024)
390 lua_pushlstring(L, output, output_len);
397 #else /* USE_STRINGPREP_ICU */
398 /****************** libidn ********************/
401 #include <idn-free.h>
403 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
406 const char *s = check_utf8(L, 1, &len);
407 if (s == NULL || len != strlen(s)) {
409 return 1; /* TODO return error message */
412 int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
413 if (ret == IDNA_SUCCESS) {
414 lua_pushstring(L, output);
420 return 1; /* TODO return error message */
424 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
427 const char *s = luaL_checklstring(L, 1, &len);
429 int ret = idna_to_unicode_8z8z(s, &output, 0);
430 if (ret == IDNA_SUCCESS) {
431 lua_pushstring(L, output);
437 return 1; /* TODO return error message */
442 static const luaL_Reg Reg_idna[] =
444 { "to_ascii", Lidna_to_ascii },
445 { "to_unicode", Lidna_to_unicode },
449 /***************** end *****************/
451 static const luaL_Reg Reg[] =
456 LUALIB_API int luaopen_util_encodings(lua_State *L)
458 #ifdef USE_STRINGPREP_ICU
461 luaL_register(L, "encodings", Reg);
463 lua_pushliteral(L, "base64");
465 luaL_register(L, NULL, Reg_base64);
468 lua_pushliteral(L, "stringprep");
470 luaL_register(L, NULL, Reg_stringprep);
473 lua_pushliteral(L, "idna");
475 luaL_register(L, NULL, Reg_idna);
478 lua_pushliteral(L, "utf8");
480 luaL_register(L, NULL, Reg_utf8);
483 lua_pushliteral(L, "version"); /** version */
484 lua_pushliteral(L, "-3.14");