Automated merge.
[prosody.git] / util-src / encodings.c
1 /*\r
2 * encodings.c\r
3 * Lua library for base64, stringprep and idna encodings\r
4 */\r
5 \r
6 // Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way\r
7 #define _CRT_SECURE_NO_DEPRECATE\r
8 \r
9 #include <string.h>\r
10 #include <stdlib.h>\r
11 \r
12 #include "lua.h"\r
13 #include "lauxlib.h"\r
14 \r
15 /***************** BASE64 *****************/\r
16 \r
17 static const char code[]=\r
18 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
19 \r
20 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n)\r
21 {\r
22         unsigned long tuple=c3+256UL*(c2+256UL*c1);\r
23         int i;\r
24         char s[4];\r
25         for (i=0; i<4; i++) {\r
26                 s[3-i] = code[tuple % 64];\r
27                 tuple /= 64;\r
28         }\r
29         for (i=n+1; i<4; i++) s[i]='=';\r
30         luaL_addlstring(b,s,4);\r
31 }\r
32 \r
33 static int Lbase64_encode(lua_State *L)         /** encode(s) */\r
34 {\r
35         size_t l;\r
36         const unsigned char *s=(const unsigned char*)luaL_checklstring(L,1,&l);\r
37         luaL_Buffer b;\r
38         int n;\r
39         luaL_buffinit(L,&b);\r
40         for (n=l/3; n--; s+=3) base64_encode(&b,s[0],s[1],s[2],3);\r
41         switch (l%3)\r
42         {\r
43                 case 1: base64_encode(&b,s[0],0,0,1);           break;\r
44                 case 2: base64_encode(&b,s[0],s[1],0,2);                break;\r
45         }\r
46         luaL_pushresult(&b);\r
47         return 1;\r
48 }\r
49 \r
50 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n)\r
51 {\r
52         unsigned long tuple=c4+64L*(c3+64L*(c2+64L*c1));\r
53         char s[3];\r
54         switch (--n)\r
55         {\r
56                 case 3: s[2]=(char) tuple;\r
57                 case 2: s[1]=(char) (tuple >> 8);\r
58                 case 1: s[0]=(char) (tuple >> 16);\r
59         }\r
60         luaL_addlstring(b,s,n);\r
61 }\r
62 \r
63 static int Lbase64_decode(lua_State *L)         /** decode(s) */\r
64 {\r
65         size_t l;\r
66         const char *s=luaL_checklstring(L,1,&l);\r
67         luaL_Buffer b;\r
68         int n=0;\r
69         char t[4];\r
70         luaL_buffinit(L,&b);\r
71         for (;;)\r
72         {\r
73                 int c=*s++;\r
74                 switch (c)\r
75                 {\r
76                         const char *p;\r
77                         default:\r
78                                 p=strchr(code,c); if (p==NULL) return 0;\r
79                                 t[n++]= (char) (p-code);\r
80                                 if (n==4)\r
81                                 {\r
82                                         base64_decode(&b,t[0],t[1],t[2],t[3],4);\r
83                                         n=0;\r
84                                 }\r
85                                 break;\r
86                         case '=':\r
87                                 switch (n)\r
88                                 {\r
89                                         case 1: base64_decode(&b,t[0],0,0,0,1);         break;\r
90                                         case 2: base64_decode(&b,t[0],t[1],0,0,2);      break;\r
91                                         case 3: base64_decode(&b,t[0],t[1],t[2],0,3);   break;\r
92                                 }\r
93                         case 0:\r
94                                 luaL_pushresult(&b);\r
95                                 return 1;\r
96                         case '\n': case '\r': case '\t': case ' ': case '\f': case '\b':\r
97                                 break;\r
98                 }\r
99         }\r
100         return 0;\r
101 }\r
102 \r
103 static const luaL_Reg Reg_base64[] =\r
104 {\r
105         { "encode",     Lbase64_encode  },\r
106         { "decode",     Lbase64_decode  },\r
107         { NULL,         NULL    }\r
108 };\r
109 \r
110 /***************** STRINGPREP *****************/\r
111 \r
112 #include <stringprep.h>\r
113 \r
114 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)\r
115 {\r
116         size_t len;\r
117         const char *s = luaL_checklstring(L, 1, &len);\r
118         char string[1024];\r
119         int ret;\r
120         if (len >= 1024) {\r
121                 lua_pushnil(L);\r
122                 return 1; // TODO return error message\r
123         }\r
124         strcpy(string, s);\r
125         ret = stringprep(string, 1024, 0, profile);\r
126         if (ret == STRINGPREP_OK) {\r
127                 lua_pushstring(L, string);\r
128                 return 1;\r
129         } else {\r
130                 lua_pushnil(L);\r
131                 return 1; // TODO return error message\r
132         }\r
133 }\r
134 \r
135 #define MAKE_PREP_FUNC(myFunc, prep) \\r
136 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }\r
137 \r
138 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep)               /** stringprep.nameprep(s) */\r
139 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep)          /** stringprep.nodeprep(s) */\r
140 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep)          /** stringprep.resourceprep(s) */\r
141 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep)               /** stringprep.saslprep(s) */\r
142 \r
143 static const luaL_Reg Reg_stringprep[] =\r
144 {\r
145         { "nameprep",   Lstringprep_nameprep    },\r
146         { "nodeprep",   Lstringprep_nodeprep    },\r
147         { "resourceprep",       Lstringprep_resourceprep        },\r
148         { "saslprep",   Lstringprep_saslprep    },\r
149         { NULL,         NULL    }\r
150 };\r
151 \r
152 /***************** IDNA *****************/\r
153 \r
154 #include <idna.h>\r
155 \r
156 static int Lidna_to_ascii(lua_State *L)         /** idna.to_ascii(s) */\r
157 {\r
158         size_t len;\r
159         const char *s = luaL_checklstring(L, 1, &len);\r
160         char* output = NULL;\r
161         int ret = idna_to_ascii_8z(s, &output, 0);\r
162         if (ret == IDNA_SUCCESS) {\r
163                 lua_pushstring(L, output);\r
164                 if (output) free(output);\r
165                 return 1;\r
166         } else {\r
167                 lua_pushnil(L);\r
168                 if (output) free(output);\r
169                 return 1; // TODO return error message\r
170         }\r
171 }\r
172 \r
173 static int Lidna_to_unicode(lua_State *L)               /** idna.to_unicode(s) */\r
174 {\r
175         size_t len;\r
176         const char *s = luaL_checklstring(L, 1, &len);\r
177         char* output = NULL;\r
178         int ret = idna_to_unicode_8z8z(s, &output, 0);\r
179         if (ret == IDNA_SUCCESS) {\r
180                 lua_pushstring(L, output);\r
181                 if (output) free(output);\r
182                 return 1;\r
183         } else {\r
184                 lua_pushnil(L);\r
185                 if (output) free(output);\r
186                 return 1; // TODO return error message\r
187         }\r
188 }\r
189 \r
190 static const luaL_Reg Reg_idna[] =\r
191 {\r
192         { "to_ascii",   Lidna_to_ascii  },\r
193         { "to_unicode", Lidna_to_unicode        },\r
194         { NULL,         NULL    }\r
195 };\r
196 \r
197 /***************** end *****************/\r
198 \r
199 static const luaL_Reg Reg[] =\r
200 {\r
201         { NULL,         NULL    }\r
202 };\r
203 \r
204 LUALIB_API int luaopen_util_encodings(lua_State *L)\r
205 {\r
206         luaL_register(L, "encodings", Reg);\r
207 \r
208         lua_pushliteral(L, "base64");\r
209         lua_newtable(L);\r
210         luaL_register(L, NULL, Reg_base64);\r
211         lua_settable(L,-3);\r
212 \r
213         lua_pushliteral(L, "stringprep");\r
214         lua_newtable(L);\r
215         luaL_register(L, NULL, Reg_stringprep);\r
216         lua_settable(L,-3);\r
217 \r
218         lua_pushliteral(L, "idna");\r
219         lua_newtable(L);\r
220         luaL_register(L, NULL, Reg_idna);\r
221         lua_settable(L,-3);\r
222 \r
223         lua_pushliteral(L, "version");                  /** version */\r
224         lua_pushliteral(L, "-3.14");\r
225         lua_settable(L,-3);\r
226         return 1;\r
227 }\r