2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
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':
114 static const luaL_Reg Reg_base64[] =
116 { "encode", Lbase64_encode },
117 { "decode", Lbase64_decode },
121 /***************** STRINGPREP *****************/
123 #include <stringprep.h>
125 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
131 if(!lua_isstring(L, 1)) {
135 s = lua_tolstring(L, 1, &len);
138 return 1; // TODO return error message
141 ret = stringprep(string, 1024, 0, profile);
142 if (ret == STRINGPREP_OK) {
143 lua_pushstring(L, string);
147 return 1; // TODO return error message
151 #define MAKE_PREP_FUNC(myFunc, prep) \
152 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
154 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
155 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
156 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
157 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
159 static const luaL_Reg Reg_stringprep[] =
161 { "nameprep", Lstringprep_nameprep },
162 { "nodeprep", Lstringprep_nodeprep },
163 { "resourceprep", Lstringprep_resourceprep },
164 { "saslprep", Lstringprep_saslprep },
168 /***************** IDNA *****************/
171 #include <idn-free.h>
173 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
176 const char *s = luaL_checklstring(L, 1, &len);
178 int ret = idna_to_ascii_8z(s, &output, 0);
179 if (ret == IDNA_SUCCESS) {
180 lua_pushstring(L, output);
186 return 1; // TODO return error message
190 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
193 const char *s = luaL_checklstring(L, 1, &len);
195 int ret = idna_to_unicode_8z8z(s, &output, 0);
196 if (ret == IDNA_SUCCESS) {
197 lua_pushstring(L, output);
203 return 1; // TODO return error message
207 static const luaL_Reg Reg_idna[] =
209 { "to_ascii", Lidna_to_ascii },
210 { "to_unicode", Lidna_to_unicode },
214 /***************** end *****************/
216 static const luaL_Reg Reg[] =
221 LUALIB_API int luaopen_util_encodings(lua_State *L)
223 luaL_register(L, "encodings", Reg);
225 lua_pushliteral(L, "base64");
227 luaL_register(L, NULL, Reg_base64);
230 lua_pushliteral(L, "stringprep");
232 luaL_register(L, NULL, Reg_stringprep);
235 lua_pushliteral(L, "idna");
237 luaL_register(L, NULL, Reg_idna);
240 lua_pushliteral(L, "version"); /** version */
241 lua_pushliteral(L, "-3.14");