2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 * Lua library for base64, stringprep and idna encodings
15 /* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
16 #define _CRT_SECURE_NO_DEPRECATE
23 /***************** BASE64 *****************/
25 static const char code[]=
26 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
30 unsigned long tuple=c3+256UL*(c2+256UL*c1);
34 s[3-i] = code[tuple % 64];
37 for (i=n+1; i<4; i++) s[i]='=';
38 luaL_addlstring(b,s,4);
41 static int Lbase64_encode(lua_State *L) /** encode(s) */
44 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
48 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
51 case 1: base64_encode(&b,s[0],0,0,1); break;
52 case 2: base64_encode(&b,s[0],s[1],0,2); break;
58 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
60 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
64 case 3: s[2]=(char) tuple;
65 case 2: s[1]=(char) (tuple >> 8);
66 case 1: s[0]=(char) (tuple >> 16);
68 luaL_addlstring(b,s,n);
71 static int Lbase64_decode(lua_State *L) /** decode(s) */
74 const char *s=luaL_checklstring(L,1,&l);
86 p=strchr(code,c); if (p==NULL) return 0;
87 t[n++]= (char) (p-code);
90 base64_decode(&b,t[0],t[1],t[2],t[3],4);
97 case 1: base64_decode(&b,t[0],0,0,0,1); break;
98 case 2: base64_decode(&b,t[0],t[1],0,0,2); break;
99 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
106 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
112 static const luaL_Reg Reg_base64[] =
114 { "encode", Lbase64_encode },
115 { "decode", Lbase64_decode },
119 /***************** STRINGPREP *****************/
120 #ifdef USE_STRINGPREP_ICU
122 #include <unicode/usprep.h>
123 #include <unicode/ustring.h>
124 #include <unicode/utrace.h>
126 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile)
129 int32_t unprepped_len, prepped_len, output_len;
133 UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
136 UErrorCode err = U_ZERO_ERROR;
138 if(!lua_isstring(L, 1)) {
142 input = lua_tolstring(L, 1, &input_len);
143 if (input_len >= 1024) {
147 u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
148 prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, 0, NULL, &err);
149 if (U_FAILURE(err)) {
153 u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
154 if(output_len < 1024)
155 lua_pushlstring(L, output, output_len);
162 UStringPrepProfile *icu_nameprep;
163 UStringPrepProfile *icu_nodeprep;
164 UStringPrepProfile *icu_resourceprep;
165 UStringPrepProfile *icu_saslprep;
167 /* initialize global ICU stringprep profiles */
170 UErrorCode err = U_ZERO_ERROR;
171 utrace_setLevel(UTRACE_VERBOSE);
172 icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
173 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
174 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
175 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
176 if (U_FAILURE(err)) fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
179 #define MAKE_PREP_FUNC(myFunc, prep) \
180 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
182 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep) /** stringprep.nameprep(s) */
183 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep) /** stringprep.nodeprep(s) */
184 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep) /** stringprep.resourceprep(s) */
185 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep) /** stringprep.saslprep(s) */
187 static const luaL_Reg Reg_stringprep[] =
189 { "nameprep", Lstringprep_nameprep },
190 { "nodeprep", Lstringprep_nodeprep },
191 { "resourceprep", Lstringprep_resourceprep },
192 { "saslprep", Lstringprep_saslprep },
195 #else /* USE_STRINGPREP_ICU */
197 /****************** libidn ********************/
199 #include <stringprep.h>
201 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
207 if(!lua_isstring(L, 1)) {
211 s = lua_tolstring(L, 1, &len);
214 return 1; /* TODO return error message */
217 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
218 if (ret == STRINGPREP_OK) {
219 lua_pushstring(L, string);
223 return 1; /* TODO return error message */
227 #define MAKE_PREP_FUNC(myFunc, prep) \
228 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
230 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
231 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
232 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
233 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
235 static const luaL_Reg Reg_stringprep[] =
237 { "nameprep", Lstringprep_nameprep },
238 { "nodeprep", Lstringprep_nodeprep },
239 { "resourceprep", Lstringprep_resourceprep },
240 { "saslprep", Lstringprep_saslprep },
245 /***************** IDNA *****************/
246 #ifdef USE_STRINGPREP_ICU
247 #include <unicode/ustdio.h>
248 #include <unicode/uidna.h>
249 /* IDNA2003 or IDNA2008 ? ? ? */
250 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
253 int32_t ulen, dest_len, output_len;
254 const char *s = luaL_checklstring(L, 1, &len);
256 UErrorCode err = U_ZERO_ERROR;
260 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
261 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
262 if (U_FAILURE(err)) {
266 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
267 if(output_len < 1024)
268 lua_pushlstring(L, output, output_len);
275 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
278 int32_t ulen, dest_len, output_len;
279 const char *s = luaL_checklstring(L, 1, &len);
281 UErrorCode err = U_ZERO_ERROR;
285 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
286 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
287 if (U_FAILURE(err)) {
291 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
292 if(output_len < 1024)
293 lua_pushlstring(L, output, output_len);
300 #else /* USE_STRINGPREP_ICU */
301 /****************** libidn ********************/
304 #include <idn-free.h>
306 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
309 const char *s = luaL_checklstring(L, 1, &len);
311 int ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
312 if (ret == IDNA_SUCCESS) {
313 lua_pushstring(L, output);
319 return 1; /* TODO return error message */
323 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
326 const char *s = luaL_checklstring(L, 1, &len);
328 int ret = idna_to_unicode_8z8z(s, &output, 0);
329 if (ret == IDNA_SUCCESS) {
330 lua_pushstring(L, output);
336 return 1; /* TODO return error message */
341 static const luaL_Reg Reg_idna[] =
343 { "to_ascii", Lidna_to_ascii },
344 { "to_unicode", Lidna_to_unicode },
348 /***************** end *****************/
350 static const luaL_Reg Reg[] =
355 LUALIB_API int luaopen_util_encodings(lua_State *L)
357 #ifdef USE_STRINGPREP_ICU
360 luaL_register(L, "encodings", Reg);
362 lua_pushliteral(L, "base64");
364 luaL_register(L, NULL, Reg_base64);
367 lua_pushliteral(L, "stringprep");
369 luaL_register(L, NULL, Reg_stringprep);
372 lua_pushliteral(L, "idna");
374 luaL_register(L, NULL, Reg_idna);
377 lua_pushliteral(L, "version"); /** version */
378 lua_pushliteral(L, "-3.14");