Fix for handling latin1 encoded hostnames in SASL
[prosody.git] / util-src / encodings.c
1 /* Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19
20
21 /*\r
22 * encodings.c\r
23 * Lua library for base64, stringprep and idna encodings\r
24 */\r
25 \r
26 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way\r
27 #define _CRT_SECURE_NO_DEPRECATE\r
28 \r
29 #include <string.h>\r
30 #include <stdlib.h>\r
31 \r
32 #include "lua.h"\r
33 #include "lauxlib.h"\r
34 \r
35 /***************** BASE64 *****************/\r
36 \r
37 static const char code[]=\r
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
39 \r
40 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)\r
41 {\r
42         unsigned long tuple=c3+256UL*(c2+256UL*c1);\r
43         int i;\r
44         char s[4];\r
45         for (i=0; i<4; i++) {\r
46                 s[3-i] = code[tuple % 64];\r
47                 tuple /= 64;\r
48         }\r
49         for (i=n+1; i<4; i++) s[i]='=';\r
50         luaL_addlstring(b,s,4);\r
51 }\r
52 \r
53 static int Lbase64_encode(lua_State *L)         /** encode(s) */\r
54 {\r
55         size_t l;\r
56         const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);\r
57         luaL_Buffer b;\r
58         int n;\r
59         luaL_buffinit(L,&b);\r
60         for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);\r
61         switch (l%3)\r
62         {\r
63                 case 1: base64_encode(&b,s[0],0,0,1);           break;\r
64                 case 2: base64_encode(&b,s[0],s[1],0,2);                break;\r
65         }\r
66         luaL_pushresult(&b);\r
67         return 1;\r
68 }\r
69 \r
70 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)\r
71 {\r
72         unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));\r
73         char s[3];\r
74         switch (--n)\r
75         {\r
76                 case 3: s[2]=(char) tuple;\r
77                 case 2: s[1]=(char) (tuple >> 8);\r
78                 case 1: s[0]=(char) (tuple >> 16);\r
79         }\r
80         luaL_addlstring(b,s,n);\r
81 }\r
82 \r
83 static int Lbase64_decode(lua_State *L)         /** decode(s) */\r
84 {\r
85         size_t l;\r
86         const char *s=luaL_checklstring(L,1,&l);\r
87         luaL_Buffer b;\r
88         int n=0;\r
89         char t[4];\r
90         luaL_buffinit(L,&b);\r
91         for (;;)\r
92         {\r
93                 int c=*s++;\r
94                 switch (c)\r
95                 {\r
96                         const char *p;\r
97                         default:\r
98                                 p=strchr(code,c); if (p==NULL) return 0;\r
99                                 t[n++]= (char) (p-code);\r
100                                 if (n==4)\r
101                                 {\r
102                                         base64_decode(&b,t[0],t[1],t[2],t[3],4);\r
103                                         n=0;\r
104                                 }\r
105                                 break;\r
106                         case '=':\r
107                                 switch (n)\r
108                                 {\r
109                                         case 1: base64_decode(&b,t[0],0,0,0,1);         break;\r
110                                         case 2: base64_decode(&b,t[0],t[1],0,0,2);      break;\r
111                                         case 3: base64_decode(&b,t[0],t[1],t[2],0,3);   break;\r
112                                 }\r
113                         case 0:\r
114                                 luaL_pushresult(&b);\r
115                                 return 1;\r
116                         case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':\r
117                                 break;\r
118                 }\r
119         }\r
120         return 0;\r
121 }\r
122 \r
123 static const luaL_Reg Reg_base64[] =\r
124 {\r
125         { "encode",     Lbase64_encode  },\r
126         { "decode",     Lbase64_decode  },\r
127         { NULL,         NULL    }\r
128 };\r
129 \r
130 /***************** STRINGPREP *****************/\r
131 \r
132 #include <stringprep.h>\r
133 \r
134 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)\r
135 {\r
136         size_t len;\r
137         const char *s = luaL_checklstring(L, 1, &len);\r
138         char string[1024];\r
139         int ret;\r
140         if (len >= 1024) {\r
141                 lua_pushnil(L);\r
142                 return 1; // TODO return error message\r
143         }\r
144         strcpy(string, s);\r
145         ret = stringprep(string, 1024, 0, profile);\r
146         if (ret == STRINGPREP_OK) {\r
147                 lua_pushstring(L, string);\r
148                 return 1;\r
149         } else {\r
150                 lua_pushnil(L);\r
151                 return 1; // TODO return error message\r
152         }\r
153 }\r
154 \r
155 #define MAKE_PREP_FUNC(myFunc, prep) \\r
156 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }\r
157 \r
158 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep)               /** stringprep.nameprep(s) */\r
159 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep)          /** stringprep.nodeprep(s) */\r
160 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep)          /** stringprep.resourceprep(s) */\r
161 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep)               /** stringprep.saslprep(s) */\r
162 \r
163 static const luaL_Reg Reg_stringprep[] =\r
164 {\r
165         { "nameprep",   Lstringprep_nameprep    },\r
166         { "nodeprep",   Lstringprep_nodeprep    },\r
167         { "resourceprep",       Lstringprep_resourceprep        },\r
168         { "saslprep",   Lstringprep_saslprep    },\r
169         { NULL,         NULL    }\r
170 };\r
171 \r
172 /***************** IDNA *****************/\r
173 \r
174 #include <idna.h>\r
175 \r
176 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */\r
177 {\r
178         size_t len;\r
179         const char *s = luaL_checklstring(L, 1, &len);\r
180         char* output = NULL;\r
181         int ret = idna_to_ascii_8z(s, &output, 0);\r
182         if (ret == IDNA_SUCCESS) {\r
183                 lua_pushstring(L, output);\r
184                 if (output) free(output);\r
185                 return 1;\r
186         } else {\r
187                 lua_pushnil(L);\r
188                 if (output) free(output);\r
189                 return 1; // TODO return error message\r
190         }\r
191 }\r
192 \r
193 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */\r
194 {\r
195         size_t len;\r
196         const char *s = luaL_checklstring(L, 1, &len);\r
197         char* output = NULL;\r
198         int ret = idna_to_unicode_8z8z(s, &output, 0);\r
199         if (ret == IDNA_SUCCESS) {\r
200                 lua_pushstring(L, output);\r
201                 if (output) free(output);\r
202                 return 1;\r
203         } else {\r
204                 lua_pushnil(L);\r
205                 if (output) free(output);\r
206                 return 1; // TODO return error message\r
207         }\r
208 }\r
209 \r
210 static const luaL_Reg Reg_idna[] =\r
211 {\r
212         { "to_ascii",   Lidna_to_ascii  },\r
213         { "to_unicode", Lidna_to_unicode        },\r
214         { NULL,         NULL    }\r
215 };\r
216 \r
217 /***************** end *****************/\r
218 \r
219 static const luaL_Reg Reg[] =\r
220 {\r
221         { NULL,         NULL    }\r
222 };\r
223 \r
224 LUALIB_API int luaopen_util_encodings(lua_State *L)\r
225 {\r
226         luaL_register(L, "encodings", Reg);\r
227 \r
228         lua_pushliteral(L, "base64");\r
229         lua_newtable(L);\r
230         luaL_register(L, NULL, Reg_base64);\r
231         lua_settable(L,-3);\r
232 \r
233         lua_pushliteral(L, "stringprep");\r
234         lua_newtable(L);\r
235         luaL_register(L, NULL, Reg_stringprep);\r
236         lua_settable(L,-3);\r
237 \r
238         lua_pushliteral(L, "idna");\r
239         lua_newtable(L);\r
240         luaL_register(L, NULL, Reg_idna);\r
241         lua_settable(L,-3);\r
242 \r
243         lua_pushliteral(L, "version");                  /** version */\r
244         lua_pushliteral(L, "-3.14");\r
245         lua_settable(L,-3);\r
246         return 1;\r
247 }\r