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? */
179 /***************** STRINGPREP *****************/
180 #ifdef USE_STRINGPREP_ICU
182 #include <unicode/usprep.h>
183 #include <unicode/ustring.h>
184 #include <unicode/utrace.h>
186 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
189 int32_t unprepped_len, prepped_len, output_len;
193 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
196 UErrorCode err = U_ZERO_ERROR;
198 if(!lua_isstring(L, 1)) {
202 input = lua_tolstring(L, 1, &input_len);
203 if (input_len >= 1024) {
207 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
208 if (U_FAILURE(err)) {
212 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
213 if (U_FAILURE(err)) {
217 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
218 if (U_SUCCESS(err) && output_len < 1024)
219 lua_pushlstring(L, output, output_len);
226 UStringPrepProfile *icu_nameprep;
227 UStringPrepProfile *icu_nodeprep;
228 UStringPrepProfile *icu_resourceprep;
229 UStringPrepProfile *icu_saslprep;
231 /* initialize global ICU stringprep profiles */
234 UErrorCode err = U_ZERO_ERROR;
235 utrace_setLevel(UTRACE_VERBOSE);
236 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
237 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
238 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
239 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
240 if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
243 #define MAKE_PREP_FUNC(myFunc, prep) \
244 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
246 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
247 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
248 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
249 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
251 static const luaL_Reg Reg_stringprep[] =
253 { "nameprep", Lstringprep_nameprep },
254 { "nodeprep", Lstringprep_nodeprep },
255 { "resourceprep", Lstringprep_resourceprep },
256 { "saslprep", Lstringprep_saslprep },
259 #else /* USE_STRINGPREP_ICU */
261 /****************** libidn ********************/
263 #include <stringprep.h>
265 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
271 if(!lua_isstring(L, 1)) {
275 s = check_utf8(L, 1, &len);
276 if (s == NULL || len >= 1024 || len != strlen(s)) {
278 return 1; /* TODO return error message */
281 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
282 if (ret == STRINGPREP_OK) {
283 lua_pushstring(L, string);
287 return 1; /* TODO return error message */
291 #define MAKE_PREP_FUNC(myFunc, prep) \
292 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
294 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
295 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
296 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
297 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
299 static const luaL_Reg Reg_stringprep[] =
301 { "nameprep", Lstringprep_nameprep },
302 { "nodeprep", Lstringprep_nodeprep },
303 { "resourceprep", Lstringprep_resourceprep },
304 { "saslprep", Lstringprep_saslprep },
309 /***************** IDNA *****************/
310 #ifdef USE_STRINGPREP_ICU
311 #include <unicode/ustdio.h>
312 #include <unicode/uidna.h>
313 /* IDNA2003 or IDNA2008 ? ? ? */
314 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
317 int32_t ulen, dest_len, output_len;
318 const char *s = luaL_checklstring(L, 1, &len);
320 UErrorCode err = U_ZERO_ERROR;
324 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
325 if (U_FAILURE(err)) {
330 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
331 if (U_FAILURE(err)) {
335 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
336 if (U_SUCCESS(err) && output_len < 1024)
337 lua_pushlstring(L, output, output_len);
344 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
347 int32_t ulen, dest_len, output_len;
348 const char *s = luaL_checklstring(L, 1, &len);
350 UErrorCode err = U_ZERO_ERROR;
354 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
355 if (U_FAILURE(err)) {
360 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
361 if (U_FAILURE(err)) {
365 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
366 if (U_SUCCESS(err) && output_len < 1024)
367 lua_pushlstring(L, output, output_len);
374 #else /* USE_STRINGPREP_ICU */
375 /****************** libidn ********************/
378 #include <idn-free.h>
380 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
383 const char *s = check_utf8(L, 1, &len);
384 if (s == NULL || len != strlen(s)) {
386 return 1; /* TODO return error message */
389 int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
390 if (ret == IDNA_SUCCESS) {
391 lua_pushstring(L, output);
397 return 1; /* TODO return error message */
401 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
404 const char *s = luaL_checklstring(L, 1, &len);
406 int ret = idna_to_unicode_8z8z(s, &output, 0);
407 if (ret == IDNA_SUCCESS) {
408 lua_pushstring(L, output);
414 return 1; /* TODO return error message */
419 static const luaL_Reg Reg_idna[] =
421 { "to_ascii", Lidna_to_ascii },
422 { "to_unicode", Lidna_to_unicode },
426 /***************** end *****************/
428 static const luaL_Reg Reg[] =
433 LUALIB_API int luaopen_util_encodings(lua_State *L)
435 #ifdef USE_STRINGPREP_ICU
438 luaL_register(L, "encodings", Reg);
440 lua_pushliteral(L, "base64");
442 luaL_register(L, NULL, Reg_base64);
445 lua_pushliteral(L, "stringprep");
447 luaL_register(L, NULL, Reg_stringprep);
450 lua_pushliteral(L, "idna");
452 luaL_register(L, NULL, Reg_idna);
455 lua_pushliteral(L, "version"); /** version */
456 lua_pushliteral(L, "-3.14");