util-src/encodings.c

changeset 3762
f02bac902a1e
parent 3723
72a917d910a4
child 3763
1ae89ac7bf62
equal deleted inserted replaced
3723:72a917d910a4 3762:f02bac902a1e
1 /* Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 }
112
113 static const luaL_Reg Reg_base64[] =
114 {
115 { "encode", Lbase64_encode },
116 { "decode", Lbase64_decode },
117 { NULL, NULL }
118 };
119
120 /***************** STRINGPREP *****************/
121
122 #include <stringprep.h>
123
124 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile)
125 {
126 size_t len;
127 const char *s;
128 char string[1024];
129 int ret;
130 if(!lua_isstring(L, 1)) {
131 lua_pushnil(L);
132 return 1;
133 }
134 s = lua_tolstring(L, 1, &len);
135 if (len >= 1024) {
136 lua_pushnil(L);
137 return 1; // TODO return error message
138 }
139 strcpy(string, s);
140 ret = stringprep(string, 1024, 0, profile);
141 if (ret == STRINGPREP_OK) {
142 lua_pushstring(L, string);
143 return 1;
144 } else {
145 lua_pushnil(L);
146 return 1; // TODO return error message
147 }
148 }
149
150 #define MAKE_PREP_FUNC(myFunc, prep) \
151 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
152
153 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */
154 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */
155 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */
156 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */
157
158 static const luaL_Reg Reg_stringprep[] =
159 {
160 { "nameprep", Lstringprep_nameprep },
161 { "nodeprep", Lstringprep_nodeprep },
162 { "resourceprep", Lstringprep_resourceprep },
163 { "saslprep", Lstringprep_saslprep },
164 { NULL, NULL }
165 };
166
167 /***************** IDNA *****************/
168
169 #include <idna.h>
170 #include <idn-free.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, IDNA_USE_STD3_ASCII_RULES);
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 }

mercurial