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