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 == 501)
25 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
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) {
34 unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1);
38 for(i = 0; i < 4; i++) {
39 s[3 - i] = code[tuple % 64];
43 for(i = n + 1; i < 4; i++) {
47 luaL_addlstring(b, s, 4);
50 static int Lbase64_encode(lua_State* L) { /** encode(s) */
52 const unsigned char* s = (const unsigned char*)luaL_checklstring(L, 1, &l);
57 for(n = l / 3; n--; s += 3) {
58 base64_encode(&b, s[0], s[1], s[2], 3);
63 base64_encode(&b, s[0], 0, 0, 1);
66 base64_encode(&b, s[0], s[1], 0, 2);
74 static void base64_decode(luaL_Buffer* b, int c1, int c2, int c3, int c4, int n) {
75 unsigned long tuple = c4 + 64L * (c3 + 64L * (c2 + 64L * c1));
82 s[1] = (char)(tuple >> 8);
84 s[0] = (char)(tuple >> 16);
87 luaL_addlstring(b, s, n);
90 static int Lbase64_decode(lua_State* L) { /** decode(s) */
92 const char* s = luaL_checklstring(L, 1, &l);
110 t[n++] = (char)(p - code);
113 base64_decode(&b, t[0], t[1], t[2], t[3], 4);
122 base64_decode(&b, t[0], 0, 0, 0, 1);
125 base64_decode(&b, t[0], t[1], 0, 0, 2);
128 base64_decode(&b, t[0], t[1], t[2], 0, 3);
148 static const luaL_Reg Reg_base64[] = {
149 { "encode", Lbase64_encode },
150 { "decode", Lbase64_decode },
154 /******************* UTF-8 ********************/
157 * Adapted from Lua 5.3
158 * Needed because libidn does not validate that input is valid UTF-8
161 #define MAXUNICODE 0x10FFFF
164 * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
166 static const char* utf8_decode(const char* o, int* val) {
167 static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
168 const unsigned char* s = (const unsigned char*)o;
169 unsigned int c = s[0];
170 unsigned int res = 0; /* final result */
172 if(c < 0x80) { /* ascii? */
175 int count = 0; /* to count number of continuation bytes */
177 while(c & 0x40) { /* still have continuation bytes? */
178 int cc = s[++count]; /* read next byte */
180 if((cc & 0xC0) != 0x80) { /* not a continuation byte? */
181 return NULL; /* invalid byte sequence */
184 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
185 c <<= 1; /* to test next bit */
188 res |= ((c & 0x7F) << (count * 5)); /* add first byte */
190 if(count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff)) {
191 return NULL; /* invalid byte sequence */
194 s += count; /* skip continuation bytes read */
201 return (const char*)s + 1; /* +1 to include first byte */
205 * Check that a string is valid UTF-8
206 * Returns NULL if not
208 const char* check_utf8(lua_State* L, int idx, size_t* l) {
210 const char* s = luaL_checklstring(L, 1, &len);
214 const char* s1 = utf8_decode(s + pos, NULL);
216 if(s1 == NULL) { /* conversion error? */
230 static int Lutf8_valid(lua_State* L) {
231 lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
235 static int Lutf8_length(lua_State* L) {
238 if(!check_utf8(L, 1, &len)) {
240 lua_pushliteral(L, "invalid utf8");
244 lua_pushinteger(L, len);
248 static const luaL_Reg Reg_utf8[] = {
249 { "valid", Lutf8_valid },
250 { "length", Lutf8_length },
255 /***************** STRINGPREP *****************/
256 #ifdef USE_STRINGPREP_ICU
258 #include <unicode/usprep.h>
259 #include <unicode/ustring.h>
260 #include <unicode/utrace.h>
262 static int icu_stringprep_prep(lua_State* L, const UStringPrepProfile* profile) {
264 int32_t unprepped_len, prepped_len, output_len;
268 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
271 UErrorCode err = U_ZERO_ERROR;
273 if(!lua_isstring(L, 1)) {
278 input = lua_tolstring(L, 1, &input_len);
280 if(input_len >= 1024) {
285 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
292 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
298 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
300 if(U_SUCCESS(err) && output_len < 1024) {
301 lua_pushlstring(L, output, output_len);
310 UStringPrepProfile* icu_nameprep;
311 UStringPrepProfile* icu_nodeprep;
312 UStringPrepProfile* icu_resourceprep;
313 UStringPrepProfile* icu_saslprep;
315 /* initialize global ICU stringprep profiles */
317 UErrorCode err = U_ZERO_ERROR;
318 utrace_setLevel(UTRACE_VERBOSE);
319 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
320 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
321 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
322 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
325 fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
329 #define MAKE_PREP_FUNC(myFunc, prep) \
330 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
332 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
333 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
334 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
335 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
337 static const luaL_Reg Reg_stringprep[] = {
338 { "nameprep", Lstringprep_nameprep },
339 { "nodeprep", Lstringprep_nodeprep },
340 { "resourceprep", Lstringprep_resourceprep },
341 { "saslprep", Lstringprep_saslprep },
344 #else /* USE_STRINGPREP_ICU */
346 /****************** libidn ********************/
348 #include <stringprep.h>
350 static int stringprep_prep(lua_State* L, const Stringprep_profile* profile) {
356 if(!lua_isstring(L, 1)) {
361 s = check_utf8(L, 1, &len);
363 if(s == NULL || len >= 1024 || len != strlen(s)) {
365 return 1; /* TODO return error message */
369 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
371 if(ret == STRINGPREP_OK) {
372 lua_pushstring(L, string);
376 return 1; /* TODO return error message */
380 #define MAKE_PREP_FUNC(myFunc, prep) \
381 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
383 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
384 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
385 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
386 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
388 static const luaL_Reg Reg_stringprep[] = {
389 { "nameprep", Lstringprep_nameprep },
390 { "nodeprep", Lstringprep_nodeprep },
391 { "resourceprep", Lstringprep_resourceprep },
392 { "saslprep", Lstringprep_saslprep },
397 /***************** IDNA *****************/
398 #ifdef USE_STRINGPREP_ICU
399 #include <unicode/ustdio.h>
400 #include <unicode/uidna.h>
401 /* IDNA2003 or IDNA2008 ? ? ? */
402 static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */
404 int32_t ulen, dest_len, output_len;
405 const char* s = luaL_checklstring(L, 1, &len);
407 UErrorCode err = U_ZERO_ERROR;
411 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
418 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
424 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
426 if(U_SUCCESS(err) && output_len < 1024) {
427 lua_pushlstring(L, output, output_len);
436 static int Lidna_to_unicode(lua_State* L) { /** idna.to_unicode(s) */
438 int32_t ulen, dest_len, output_len;
439 const char* s = luaL_checklstring(L, 1, &len);
441 UErrorCode err = U_ZERO_ERROR;
445 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
452 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
458 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
460 if(U_SUCCESS(err) && output_len < 1024) {
461 lua_pushlstring(L, output, output_len);
470 #else /* USE_STRINGPREP_ICU */
471 /****************** libidn ********************/
474 #include <idn-free.h>
476 static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */
478 const char* s = check_utf8(L, 1, &len);
482 if(s == NULL || len != strlen(s)) {
484 return 1; /* TODO return error message */
487 ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
489 if(ret == IDNA_SUCCESS) {
490 lua_pushstring(L, output);
496 return 1; /* TODO return error message */
500 static int Lidna_to_unicode(lua_State* L) { /** idna.to_unicode(s) */
502 const char* s = luaL_checklstring(L, 1, &len);
504 int ret = idna_to_unicode_8z8z(s, &output, 0);
506 if(ret == IDNA_SUCCESS) {
507 lua_pushstring(L, output);
513 return 1; /* TODO return error message */
518 static const luaL_Reg Reg_idna[] = {
519 { "to_ascii", Lidna_to_ascii },
520 { "to_unicode", Lidna_to_unicode },
524 /***************** end *****************/
526 LUALIB_API int luaopen_util_encodings(lua_State* L) {
527 #ifdef USE_STRINGPREP_ICU
533 luaL_setfuncs(L, Reg_base64, 0);
534 lua_setfield(L, -2, "base64");
537 luaL_setfuncs(L, Reg_stringprep, 0);
538 lua_setfield(L, -2, "stringprep");
541 luaL_setfuncs(L, Reg_idna, 0);
542 lua_setfield(L, -2, "idna");
545 luaL_setfuncs(L, Reg_utf8, 0);
546 lua_setfield(L, -2, "utf8");
548 lua_pushliteral(L, "-3.14");
549 lua_setfield(L, -2, "version");