util.encodings: Included idn-free.h, which explicitly declares the idn_free function.
[prosody.git] / util-src / encodings.c
1 /* Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8 */
9
10 /*
11 * encodings.c
12 * Lua library for base64, stringprep and idna encodings
13 */
14
15 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way
16 #define _CRT_SECURE_NO_DEPRECATE
17
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "lua.h"
22 #include "lauxlib.h"
23
24 /***************** BASE64 *****************/
25
26 static const char code[]=
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28
29 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)
30 {
31         unsigned long tuple=c3+256UL*(c2+256UL*c1);
32         int i;
33         char s[4];
34         for (i=0; i<4; i++) {
35                 s[3-i] = code[tuple % 64];
36                 tuple /= 64;
37         }
38         for (i=n+1; i<4; i++) s[i]='=';
39         luaL_addlstring(b,s,4);
40 }
41
42 static int Lbase64_encode(lua_State *L)         /** encode(s) */
43 {
44         size_t l;
45         const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);
46         luaL_Buffer b;
47         int n;
48         luaL_buffinit(L,&b);
49         for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);
50         switch (l%3)
51         {
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;
54         }
55         luaL_pushresult(&b);
56         return 1;
57 }
58
59 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)
60 {
61         unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));
62         char s[3];
63         switch (--n)
64         {
65                 case 3: s[2]=(char) tuple;
66                 case 2: s[1]=(char) (tuple >> 8);
67                 case 1: s[0]=(char) (tuple >> 16);
68         }
69         luaL_addlstring(b,s,n);
70 }
71
72 static int Lbase64_decode(lua_State *L)         /** decode(s) */
73 {
74         size_t l;
75         const char *s=luaL_checklstring(L,1,&l);
76         luaL_Buffer b;
77         int n=0;
78         char t[4];
79         luaL_buffinit(L,&b);
80         for (;;)
81         {
82                 int c=*s++;
83                 switch (c)
84                 {
85                         const char *p;
86                         default:
87                                 p=strchr(code,c); if (p==NULL) return 0;
88                                 t[n++]= (char) (p-code);
89                                 if (n==4)
90                                 {
91                                         base64_decode(&b,t[0],t[1],t[2],t[3],4);
92                                         n=0;
93                                 }
94                                 break;
95                         case '=':
96                                 switch (n)
97                                 {
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;
101                                 }
102                                 n=0;
103                                 break;
104                         case 0:
105                                 luaL_pushresult(&b);
106                                 return 1;
107                         case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':
108                                 break;
109                 }
110         }
111         return 0;
112 }
113
114 static const luaL_Reg Reg_base64[] =
115 {
116         { "encode",     Lbase64_encode  },
117         { "decode",     Lbase64_decode  },
118         { NULL,         NULL    }
119 };
120
121 /***************** STRINGPREP *****************/
122
123 #include <stringprep.h>
124
125 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
126 {
127         size_t len;
128         const char *s;
129         char string[1024];
130         int ret;
131         if(!lua_isstring(L, 1)) {
132                 lua_pushnil(L);
133                 return 1;
134         }
135         s = lua_tolstring(L, 1, &len);
136         if (len >= 1024) {
137                 lua_pushnil(L);
138                 return 1; // TODO return error message
139         }
140         strcpy(string, s);
141         ret = stringprep(string, 1024, 0, profile);
142         if (ret == STRINGPREP_OK) {
143                 lua_pushstring(L, string);
144                 return 1;
145         } else {
146                 lua_pushnil(L);
147                 return 1; // TODO return error message
148         }
149 }
150
151 #define MAKE_PREP_FUNC(myFunc, prep) \
152 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
153
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) */
158
159 static const luaL_Reg Reg_stringprep[] =
160 {
161         { "nameprep",   Lstringprep_nameprep    },
162         { "nodeprep",   Lstringprep_nodeprep    },
163         { "resourceprep",       Lstringprep_resourceprep        },
164         { "saslprep",   Lstringprep_saslprep    },
165         { NULL,         NULL    }
166 };
167
168 /***************** IDNA *****************/
169
170 #include <idna.h>
171 #include <idn-free.h>
172
173 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */
174 {
175         size_t len;
176         const char *s = luaL_checklstring(L, 1, &len);
177         char* output = NULL;
178         int ret = idna_to_ascii_8z(s, &output, 0);
179         if (ret == IDNA_SUCCESS) {
180                 lua_pushstring(L, output);
181                 idn_free(output);
182                 return 1;
183         } else {
184                 lua_pushnil(L);
185                 idn_free(output);
186                 return 1; // TODO return error message
187         }
188 }
189
190 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */
191 {
192         size_t len;
193         const char *s = luaL_checklstring(L, 1, &len);
194         char* output = NULL;
195         int ret = idna_to_unicode_8z8z(s, &output, 0);
196         if (ret == IDNA_SUCCESS) {
197                 lua_pushstring(L, output);
198                 idn_free(output);
199                 return 1;
200         } else {
201                 lua_pushnil(L);
202                 idn_free(output);
203                 return 1; // TODO return error message
204         }
205 }
206
207 static const luaL_Reg Reg_idna[] =
208 {
209         { "to_ascii",   Lidna_to_ascii  },
210         { "to_unicode", Lidna_to_unicode        },
211         { NULL,         NULL    }
212 };
213
214 /***************** end *****************/
215
216 static const luaL_Reg Reg[] =
217 {
218         { NULL,         NULL    }
219 };
220
221 LUALIB_API int luaopen_util_encodings(lua_State *L)
222 {
223         luaL_register(L, "encodings", Reg);
224
225         lua_pushliteral(L, "base64");
226         lua_newtable(L);
227         luaL_register(L, NULL, Reg_base64);
228         lua_settable(L,-3);
229
230         lua_pushliteral(L, "stringprep");
231         lua_newtable(L);
232         luaL_register(L, NULL, Reg_stringprep);
233         lua_settable(L,-3);
234
235         lua_pushliteral(L, "idna");
236         lua_newtable(L);
237         luaL_register(L, NULL, Reg_idna);
238         lua_settable(L,-3);
239
240         lua_pushliteral(L, "version");                  /** version */
241         lua_pushliteral(L, "-3.14");
242         lua_settable(L,-3);
243         return 1;
244 }