Merge with 0.5
[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         if(!lua_isstring(L, 1)) {
128                 lua_pushnil(L);
129                 return 1;
130         }
131         size_t len;
132         const char *s = lua_tolstring(L, 1, &len);
133         char string[1024];
134         int ret;
135         if (len >= 1024) {
136                 lua_pushnil(L);
137                 return 1; // TODO return error message
138         }
139         strcpy(string, s);
140         ret = stringprep(string, 1024, 0, profile);
141         if (ret == STRINGPREP_OK) {
142                 lua_pushstring(L, string);
143                 return 1;
144         } else {
145                 lua_pushnil(L);
146                 return 1; // TODO return error message
147         }
148 }
149
150 #define MAKE_PREP_FUNC(myFunc, prep) \
151 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
152
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) */
157
158 static const luaL_Reg Reg_stringprep[] =
159 {
160         { "nameprep",   Lstringprep_nameprep    },
161         { "nodeprep",   Lstringprep_nodeprep    },
162         { "resourceprep",       Lstringprep_resourceprep        },
163         { "saslprep",   Lstringprep_saslprep    },
164         { NULL,         NULL    }
165 };
166
167 /***************** IDNA *****************/
168
169 #include <idna.h>
170
171 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */
172 {
173         size_t len;
174         const char *s = luaL_checklstring(L, 1, &len);
175         char* output = NULL;
176         int ret = idna_to_ascii_8z(s, &output, 0);
177         if (ret == IDNA_SUCCESS) {
178                 lua_pushstring(L, output);
179                 idn_free(output);
180                 return 1;
181         } else {
182                 lua_pushnil(L);
183                 idn_free(output);
184                 return 1; // TODO return error message
185         }
186 }
187
188 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */
189 {
190         size_t len;
191         const char *s = luaL_checklstring(L, 1, &len);
192         char* output = NULL;
193         int ret = idna_to_unicode_8z8z(s, &output, 0);
194         if (ret == IDNA_SUCCESS) {
195                 lua_pushstring(L, output);
196                 idn_free(output);
197                 return 1;
198         } else {
199                 lua_pushnil(L);
200                 idn_free(output);
201                 return 1; // TODO return error message
202         }
203 }
204
205 static const luaL_Reg Reg_idna[] =
206 {
207         { "to_ascii",   Lidna_to_ascii  },
208         { "to_unicode", Lidna_to_unicode        },
209         { NULL,         NULL    }
210 };
211
212 /***************** end *****************/
213
214 static const luaL_Reg Reg[] =
215 {
216         { NULL,         NULL    }
217 };
218
219 LUALIB_API int luaopen_util_encodings(lua_State *L)
220 {
221         luaL_register(L, "encodings", Reg);
222
223         lua_pushliteral(L, "base64");
224         lua_newtable(L);
225         luaL_register(L, NULL, Reg_base64);
226         lua_settable(L,-3);
227
228         lua_pushliteral(L, "stringprep");
229         lua_newtable(L);
230         luaL_register(L, NULL, Reg_stringprep);
231         lua_settable(L,-3);
232
233         lua_pushliteral(L, "idna");
234         lua_newtable(L);
235         luaL_register(L, NULL, Reg_idna);
236         lua_settable(L,-3);
237
238         lua_pushliteral(L, "version");                  /** version */
239         lua_pushliteral(L, "-3.14");
240         lua_settable(L,-3);
241         return 1;
242 }