3 * Lua library for base64, stringprep and idna encodings
\r
6 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
\r
7 #define _CRT_SECURE_NO_DEPRECATE
\r
13 #include "lauxlib.h"
\r
15 /***************** BASE64 *****************/
\r
17 static const char code[]=
\r
18 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
\r
20 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
\r
22 unsigned long tuple=c3+256UL*(c2+256UL*c1);
\r
25 for (i=0; i<4; i++) {
\r
26 s[3-i] = code[tuple % 64];
\r
29 for (i=n+1; i<4; i++) s[i]='=';
\r
30 luaL_addlstring(b,s,4);
\r
33 static int Lbase64_encode(lua_State *L) /** encode(s) */
\r
36 const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
\r
39 luaL_buffinit(L,&b);
\r
40 for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
\r
43 case 1: base64_encode(&b,s[0],0,0,1); break;
\r
44 case 2: base64_encode(&b,s[0],s[1],0,2); break;
\r
46 luaL_pushresult(&b);
\r
50 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
\r
52 unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
\r
56 case 3: s[2]=(char) tuple;
\r
57 case 2: s[1]=(char) (tuple >> 8);
\r
58 case 1: s[0]=(char) (tuple >> 16);
\r
60 luaL_addlstring(b,s,n);
\r
63 static int Lbase64_decode(lua_State *L) /** decode(s) */
\r
66 const char *s=luaL_checklstring(L,1,&l);
\r
70 luaL_buffinit(L,&b);
\r
78 p=strchr(code,c); if (p==NULL) return 0;
\r
79 t[n++]= (char) (p-code);
\r
82 base64_decode(&b,t[0],t[1],t[2],t[3],4);
\r
89 case 1: base64_decode(&b,t[0],0,0,0,1); break;
\r
90 case 2: base64_decode(&b,t[0],t[1],0,0,2); break;
\r
91 case 3: base64_decode(&b,t[0],t[1],t[2],0,3); break;
\r
94 luaL_pushresult(&b);
\r
96 case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
\r
103 static const luaL_Reg Reg_base64[] =
\r
105 { "encode", Lbase64_encode },
\r
106 { "decode", Lbase64_decode },
\r
110 /***************** STRINGPREP *****************/
\r
112 #include <stringprep.h>
\r
114 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
\r
117 const char *s = luaL_checklstring(L, 1, &len);
\r
122 return 1; // TODO return error message
\r
125 ret = stringprep(string, 1024, 0, profile);
\r
126 if (ret == STRINGPREP_OK) {
\r
127 lua_pushstring(L, string);
\r
131 return 1; // TODO return error message
\r
135 #define MAKE_PREP_FUNC(myFunc, prep) \
\r
136 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
\r
138 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
\r
139 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
\r
140 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
\r
141 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
\r
143 static const luaL_Reg Reg_stringprep[] =
\r
145 { "nameprep", Lstringprep_nameprep },
\r
146 { "nodeprep", Lstringprep_nodeprep },
\r
147 { "resourceprep", Lstringprep_resourceprep },
\r
148 { "saslprep", Lstringprep_saslprep },
\r
152 /***************** IDNA *****************/
\r
156 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */
\r
159 const char *s = luaL_checklstring(L, 1, &len);
\r
160 char* output = NULL;
\r
161 int ret = idna_to_ascii_8z(s, &output, 0);
\r
162 if (ret == IDNA_SUCCESS) {
\r
163 lua_pushstring(L, output);
\r
164 if (output) free(output);
\r
168 if (output) free(output);
\r
169 return 1; // TODO return error message
\r
173 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */
\r
176 const char *s = luaL_checklstring(L, 1, &len);
\r
177 char* output = NULL;
\r
178 int ret = idna_to_unicode_8z8z(s, &output, 0);
\r
179 if (ret == IDNA_SUCCESS) {
\r
180 lua_pushstring(L, output);
\r
181 if (output) free(output);
\r
185 if (output) free(output);
\r
186 return 1; // TODO return error message
\r
190 static const luaL_Reg Reg_idna[] =
\r
192 { "to_ascii", Lidna_to_ascii },
\r
193 { "to_unicode", Lidna_to_unicode },
\r
197 /***************** end *****************/
\r
199 static const luaL_Reg Reg[] =
\r
204 LUALIB_API int luaopen_util_encodings(lua_State *L)
\r
206 luaL_register(L, "encodings", Reg);
\r
208 lua_pushliteral(L, "base64");
\r
210 luaL_register(L, NULL, Reg_base64);
\r
211 lua_settable(L,-3);
\r
213 lua_pushliteral(L, "stringprep");
\r
215 luaL_register(L, NULL, Reg_stringprep);
\r
216 lua_settable(L,-3);
\r
218 lua_pushliteral(L, "idna");
\r
220 luaL_register(L, NULL, Reg_idna);
\r
221 lua_settable(L,-3);
\r
223 lua_pushliteral(L, "version"); /** version */
\r
224 lua_pushliteral(L, "-3.14");
\r
225 lua_settable(L,-3);
\r