util.encodings: Fixed: Last change was not ANSI C compatible.
[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
172 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */
173 {
174         size_t len;
175         const char *s = luaL_checklstring(L, 1, &len);
176         char* output = NULL;
177         int ret = idna_to_ascii_8z(s, &output, 0);
178         if (ret == IDNA_SUCCESS) {
179                 lua_pushstring(L, output);
180                 idn_free(output);
181                 return 1;
182         } else {
183                 lua_pushnil(L);
184                 idn_free(output);
185                 return 1; // TODO return error message
186         }
187 }
188
189 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */
190 {
191         size_t len;
192         const char *s = luaL_checklstring(L, 1, &len);
193         char* output = NULL;
194         int ret = idna_to_unicode_8z8z(s, &output, 0);
195         if (ret == IDNA_SUCCESS) {
196                 lua_pushstring(L, output);
197                 idn_free(output);
198                 return 1;
199         } else {
200                 lua_pushnil(L);
201                 idn_free(output);
202                 return 1; // TODO return error message
203         }
204 }
205
206 static const luaL_Reg Reg_idna[] =
207 {
208         { "to_ascii",   Lidna_to_ascii  },
209         { "to_unicode", Lidna_to_unicode        },
210         { NULL,         NULL    }
211 };
212
213 /***************** end *****************/
214
215 static const luaL_Reg Reg[] =
216 {
217         { NULL,         NULL    }
218 };
219
220 LUALIB_API int luaopen_util_encodings(lua_State *L)
221 {
222         luaL_register(L, "encodings", Reg);
223
224         lua_pushliteral(L, "base64");
225         lua_newtable(L);
226         luaL_register(L, NULL, Reg_base64);
227         lua_settable(L,-3);
228
229         lua_pushliteral(L, "stringprep");
230         lua_newtable(L);
231         luaL_register(L, NULL, Reg_stringprep);
232         lua_settable(L,-3);
233
234         lua_pushliteral(L, "idna");
235         lua_newtable(L);
236         luaL_register(L, NULL, Reg_idna);
237         lua_settable(L,-3);
238
239         lua_pushliteral(L, "version");                  /** version */
240         lua_pushliteral(L, "-3.14");
241         lua_settable(L,-3);
242         return 1;
243 }