2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * Lua library for base64, stringprep and idna encodings
\r
26 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
\r
27 #define _CRT_SECURE_NO_DEPRECATE
\r
33 #include "lauxlib.h"
\r
35 /***************** BASE64 *****************/
\r
37 static const char code[]=
\r
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
\r
40 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
\r
42 unsigned long tuple=c3+256UL*(c2+256UL*c1);
\r
45 for (i=0; i<4; i++) {
\r
46 s[3-i] = code[tuple % 64];
\r
49 for (i=n+1; i<4; i++) s[i]='=';
\r
50 luaL_addlstring(b,s,4);
\r
53 static int Lbase64_encode(lua_State *L) /** encode(s) */
\r
56 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
\r
59 luaL_buffinit(L,&b);
\r
60 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
\r
63 case 1: base64_encode(&b,s[0],0,0,1); break;
\r
64 case 2: base64_encode(&b,s[0],s[1],0,2); break;
\r
66 luaL_pushresult(&b);
\r
70 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
\r
72 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
\r
76 case 3: s[2]=(char) tuple;
\r
77 case 2: s[1]=(char) (tuple >> 8);
\r
78 case 1: s[0]=(char) (tuple >> 16);
\r
80 luaL_addlstring(b,s,n);
\r
83 static int Lbase64_decode(lua_State *L) /** decode(s) */
\r
86 const char *s=luaL_checklstring(L,1,&l);
\r
90 luaL_buffinit(L,&b);
\r
98 p=strchr(code,c); if (p==NULL) return 0;
\r
99 t[n++]= (char) (p-code);
\r
102 base64_decode(&b,t[0],t[1],t[2],t[3],4);
\r
109 case 1: base64_decode(&b,t[0],0,0,0,1); break;
\r
110 case 2: base64_decode(&b,t[0],t[1],0,0,2); break;
\r
111 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
116 luaL_pushresult(&b);
\r
118 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
\r
125 static const luaL_Reg Reg_base64[] =
\r
127 { "encode", Lbase64_encode },
\r
128 { "decode", Lbase64_decode },
\r
132 /***************** STRINGPREP *****************/
\r
134 #include <stringprep.h>
\r
136 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
\r
139 const char *s = luaL_checklstring(L, 1, &len);
\r
144 return 1; // TODO return error message
\r
147 ret = stringprep(string, 1024, 0, profile);
\r
148 if (ret == STRINGPREP_OK) {
\r
149 lua_pushstring(L, string);
\r
153 return 1; // TODO return error message
\r
157 #define MAKE_PREP_FUNC(myFunc, prep) \
\r
158 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
\r
160 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
\r
161 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
\r
162 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
\r
163 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
\r
165 static const luaL_Reg Reg_stringprep[] =
\r
167 { "nameprep", Lstringprep_nameprep },
\r
168 { "nodeprep", Lstringprep_nodeprep },
\r
169 { "resourceprep", Lstringprep_resourceprep },
\r
170 { "saslprep", Lstringprep_saslprep },
\r
174 /***************** IDNA *****************/
\r
178 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
\r
181 const char *s = luaL_checklstring(L, 1, &len);
\r
182 char* output = NULL;
\r
183 int ret = idna_to_ascii_8z(s, &output, 0);
\r
184 if (ret == IDNA_SUCCESS) {
\r
185 lua_pushstring(L, output);
\r
186 if (output) free(output);
\r
190 if (output) free(output);
\r
191 return 1; // TODO return error message
\r
195 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
\r
198 const char *s = luaL_checklstring(L, 1, &len);
\r
199 char* output = NULL;
\r
200 int ret = idna_to_unicode_8z8z(s, &output, 0);
\r
201 if (ret == IDNA_SUCCESS) {
\r
202 lua_pushstring(L, output);
\r
203 if (output) free(output);
\r
207 if (output) free(output);
\r
208 return 1; // TODO return error message
\r
212 static const luaL_Reg Reg_idna[] =
\r
214 { "to_ascii", Lidna_to_ascii },
\r
215 { "to_unicode", Lidna_to_unicode },
\r
219 /***************** end *****************/
\r
221 static const luaL_Reg Reg[] =
\r
226 LUALIB_API int luaopen_util_encodings(lua_State *L)
\r
228 luaL_register(L, "encodings", Reg);
\r
230 lua_pushliteral(L, "base64");
\r
232 luaL_register(L, NULL, Reg_base64);
\r
233 lua_settable(L,-3);
\r
235 lua_pushliteral(L, "stringprep");
\r
237 luaL_register(L, NULL, Reg_stringprep);
\r
238 lua_settable(L,-3);
\r
240 lua_pushliteral(L, "idna");
\r
242 luaL_register(L, NULL, Reg_idna);
\r
243 lua_settable(L,-3);
\r
245 lua_pushliteral(L, "version"); /** version */
\r
246 lua_pushliteral(L, "-3.14");
\r
247 lua_settable(L,-3);
\r