Fixed util.encodings.base64.decode to not truncate results when encountering an ...
[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++;
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;
112                                 }
113                                 n=0;
114                                 break;\r
115                         case 0:\r
116                                 luaL_pushresult(&b);\r
117                                 return 1;\r
118                         case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':\r
119                                 break;\r
120                 }\r
121         }\r
122         return 0;\r
123 }\r
124 \r
125 static const luaL_Reg Reg_base64[] =\r
126 {\r
127         { "encode",     Lbase64_encode  },\r
128         { "decode",     Lbase64_decode  },\r
129         { NULL,         NULL    }\r
130 };\r
131 \r
132 /***************** STRINGPREP *****************/\r
133 \r
134 #include <stringprep.h>\r
135 \r
136 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)\r
137 {\r
138         size_t len;\r
139         const char *s = luaL_checklstring(L, 1, &len);\r
140         char string[1024];\r
141         int ret;\r
142         if (len >= 1024) {\r
143                 lua_pushnil(L);\r
144                 return 1; // TODO return error message\r
145         }\r
146         strcpy(string, s);\r
147         ret = stringprep(string, 1024, 0, profile);\r
148         if (ret == STRINGPREP_OK) {\r
149                 lua_pushstring(L, string);\r
150                 return 1;\r
151         } else {\r
152                 lua_pushnil(L);\r
153                 return 1; // TODO return error message\r
154         }\r
155 }\r
156 \r
157 #define MAKE_PREP_FUNC(myFunc, prep) \\r
158 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }\r
159 \r
160 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep)               /** stringprep.nameprep(s) */\r
161 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep)          /** stringprep.nodeprep(s) */\r
162 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep)          /** stringprep.resourceprep(s) */\r
163 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep)               /** stringprep.saslprep(s) */\r
164 \r
165 static const luaL_Reg Reg_stringprep[] =\r
166 {\r
167         { "nameprep",   Lstringprep_nameprep    },\r
168         { "nodeprep",   Lstringprep_nodeprep    },\r
169         { "resourceprep",       Lstringprep_resourceprep        },\r
170         { "saslprep",   Lstringprep_saslprep    },\r
171         { NULL,         NULL    }\r
172 };\r
173 \r
174 /***************** IDNA *****************/\r
175 \r
176 #include <idna.h>\r
177 \r
178 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */\r
179 {\r
180         size_t len;\r
181         const char *s = luaL_checklstring(L, 1, &len);\r
182         char* output = NULL;\r
183         int ret = idna_to_ascii_8z(s, &output, 0);\r
184         if (ret == IDNA_SUCCESS) {\r
185                 lua_pushstring(L, output);\r
186                 if (output) free(output);\r
187                 return 1;\r
188         } else {\r
189                 lua_pushnil(L);\r
190                 if (output) free(output);\r
191                 return 1; // TODO return error message\r
192         }\r
193 }\r
194 \r
195 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */\r
196 {\r
197         size_t len;\r
198         const char *s = luaL_checklstring(L, 1, &len);\r
199         char* output = NULL;\r
200         int ret = idna_to_unicode_8z8z(s, &output, 0);\r
201         if (ret == IDNA_SUCCESS) {\r
202                 lua_pushstring(L, output);\r
203                 if (output) free(output);\r
204                 return 1;\r
205         } else {\r
206                 lua_pushnil(L);\r
207                 if (output) free(output);\r
208                 return 1; // TODO return error message\r
209         }\r
210 }\r
211 \r
212 static const luaL_Reg Reg_idna[] =\r
213 {\r
214         { "to_ascii",   Lidna_to_ascii  },\r
215         { "to_unicode", Lidna_to_unicode        },\r
216         { NULL,         NULL    }\r
217 };\r
218 \r
219 /***************** end *****************/\r
220 \r
221 static const luaL_Reg Reg[] =\r
222 {\r
223         { NULL,         NULL    }\r
224 };\r
225 \r
226 LUALIB_API int luaopen_util_encodings(lua_State *L)\r
227 {\r
228         luaL_register(L, "encodings", Reg);\r
229 \r
230         lua_pushliteral(L, "base64");\r
231         lua_newtable(L);\r
232         luaL_register(L, NULL, Reg_base64);\r
233         lua_settable(L,-3);\r
234 \r
235         lua_pushliteral(L, "stringprep");\r
236         lua_newtable(L);\r
237         luaL_register(L, NULL, Reg_stringprep);\r
238         lua_settable(L,-3);\r
239 \r
240         lua_pushliteral(L, "idna");\r
241         lua_newtable(L);\r
242         luaL_register(L, NULL, Reg_idna);\r
243         lua_settable(L,-3);\r
244 \r
245         lua_pushliteral(L, "version");                  /** version */\r
246         lua_pushliteral(L, "-3.14");\r
247         lua_settable(L,-3);\r
248         return 1;\r
249 }\r