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':
113 static const luaL_Reg Reg_base64[] =
115 { "encode", Lbase64_encode },
116 { "decode", Lbase64_decode },
120 /***************** STRINGPREP *****************/
122 #include <stringprep.h>
124 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
130 if(!lua_isstring(L, 1)) {
134 s = lua_tolstring(L, 1, &len);
137 return 1; // TODO return error message
140 ret = stringprep(string, 1024, 0, profile);
141 if (ret == STRINGPREP_OK) {
142 lua_pushstring(L, string);
146 return 1; // TODO return error message
150 #define MAKE_PREP_FUNC(myFunc, prep) \
151 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
153 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
154 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
155 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
156 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
158 static const luaL_Reg Reg_stringprep[] =
160 { "nameprep", Lstringprep_nameprep },
161 { "nodeprep", Lstringprep_nodeprep },
162 { "resourceprep", Lstringprep_resourceprep },
163 { "saslprep", Lstringprep_saslprep },
167 /***************** IDNA *****************/
170 #include <idn-free.h>
172 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
175 const char *s = luaL_checklstring(L, 1, &len);
177 int ret = idna_to_ascii_8z(s, &output, 0);
178 if (ret == IDNA_SUCCESS) {
179 lua_pushstring(L, output);
185 return 1; // TODO return error message
189 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
192 const char *s = luaL_checklstring(L, 1, &len);
194 int ret = idna_to_unicode_8z8z(s, &output, 0);
195 if (ret == IDNA_SUCCESS) {
196 lua_pushstring(L, output);
202 return 1; // TODO return error message
206 static const luaL_Reg Reg_idna[] =
208 { "to_ascii", Lidna_to_ascii },
209 { "to_unicode", Lidna_to_unicode },
213 /***************** end *****************/
215 static const luaL_Reg Reg[] =
220 LUALIB_API int luaopen_util_encodings(lua_State *L)
222 luaL_register(L, "encodings", Reg);
224 lua_pushliteral(L, "base64");
226 luaL_register(L, NULL, Reg_base64);
229 lua_pushliteral(L, "stringprep");
231 luaL_register(L, NULL, Reg_stringprep);
234 lua_pushliteral(L, "idna");
236 luaL_register(L, NULL, Reg_idna);
239 lua_pushliteral(L, "version"); /** version */
240 lua_pushliteral(L, "-3.14");