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)
128 const char *s = luaL_checklstring(L, 1, &len);
133 return 1; // TODO return error message
136 ret = stringprep(string, 1024, 0, profile);
137 if (ret == STRINGPREP_OK) {
138 lua_pushstring(L, string);
142 return 1; // TODO return error message
146 #define MAKE_PREP_FUNC(myFunc, prep) \
147 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
149 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
150 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
151 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
152 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
154 static const luaL_Reg Reg_stringprep[] =
156 { "nameprep", Lstringprep_nameprep },
157 { "nodeprep", Lstringprep_nodeprep },
158 { "resourceprep", Lstringprep_resourceprep },
159 { "saslprep", Lstringprep_saslprep },
163 /***************** IDNA *****************/
167 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
170 const char *s = luaL_checklstring(L, 1, &len);
172 int ret = idna_to_ascii_8z(s, &output, 0);
173 if (ret == IDNA_SUCCESS) {
174 lua_pushstring(L, output);
180 return 1; // TODO return error message
184 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
187 const char *s = luaL_checklstring(L, 1, &len);
189 int ret = idna_to_unicode_8z8z(s, &output, 0);
190 if (ret == IDNA_SUCCESS) {
191 lua_pushstring(L, output);
197 return 1; // TODO return error message
201 static const luaL_Reg Reg_idna[] =
203 { "to_ascii", Lidna_to_ascii },
204 { "to_unicode", Lidna_to_unicode },
208 /***************** end *****************/
210 static const luaL_Reg Reg[] =
215 LUALIB_API int luaopen_util_encodings(lua_State *L)
217 luaL_register(L, "encodings", Reg);
219 lua_pushliteral(L, "base64");
221 luaL_register(L, NULL, Reg_base64);
224 lua_pushliteral(L, "stringprep");
226 luaL_register(L, NULL, Reg_stringprep);
229 lua_pushliteral(L, "idna");
231 luaL_register(L, NULL, Reg_idna);
234 lua_pushliteral(L, "version"); /** version */
235 lua_pushliteral(L, "-3.14");