Software /
code /
prosody
Comparison
util-src/encodings.c @ 4272:0a4ce2086a88
util.encodings: Swap code order ("ifndef" bugs me)
author | Paul Aurich <paul@darkrain42.org> |
---|---|
date | Sun, 22 May 2011 15:40:16 -0700 |
parent | 4271:18d888c8c12d |
child | 4273:7f789266b741 |
comparison
equal
deleted
inserted
replaced
4271:18d888c8c12d | 4272:0a4ce2086a88 |
---|---|
115 { "decode", Lbase64_decode }, | 115 { "decode", Lbase64_decode }, |
116 { NULL, NULL } | 116 { NULL, NULL } |
117 }; | 117 }; |
118 | 118 |
119 /***************** STRINGPREP *****************/ | 119 /***************** STRINGPREP *****************/ |
120 #ifndef USE_STRINGPREP_ICU | 120 #ifdef USE_STRINGPREP_ICU |
121 /****************** libidn ********************/ | 121 |
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; | |
129 char string[1024]; | |
130 int ret; | |
131 if(!lua_isstring(L, 1)) { | |
132 lua_pushnil(L); | |
133 return 1; | |
134 } | |
135 s = lua_tolstring(L, 1, &len); | |
136 if (len >= 1024) { | |
137 lua_pushnil(L); | |
138 return 1; /* TODO return error message */ | |
139 } | |
140 strcpy(string, s); | |
141 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile); | |
142 if (ret == STRINGPREP_OK) { | |
143 lua_pushstring(L, string); | |
144 return 1; | |
145 } else { | |
146 lua_pushnil(L); | |
147 return 1; /* TODO return error message */ | |
148 } | |
149 } | |
150 | |
151 #define MAKE_PREP_FUNC(myFunc, prep) \ | |
152 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); } | |
153 | |
154 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */ | |
155 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */ | |
156 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */ | |
157 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */ | |
158 | |
159 static const luaL_Reg Reg_stringprep[] = | |
160 { | |
161 { "nameprep", Lstringprep_nameprep }, | |
162 { "nodeprep", Lstringprep_nodeprep }, | |
163 { "resourceprep", Lstringprep_resourceprep }, | |
164 { "saslprep", Lstringprep_saslprep }, | |
165 { NULL, NULL } | |
166 }; | |
167 | |
168 #else | |
169 #include <unicode/usprep.h> | 122 #include <unicode/usprep.h> |
170 #include <unicode/ustring.h> | 123 #include <unicode/ustring.h> |
171 #include <unicode/utrace.h> | 124 #include <unicode/utrace.h> |
172 | 125 |
173 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) | 126 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) |
237 { "nodeprep", Lstringprep_nodeprep }, | 190 { "nodeprep", Lstringprep_nodeprep }, |
238 { "resourceprep", Lstringprep_resourceprep }, | 191 { "resourceprep", Lstringprep_resourceprep }, |
239 { "saslprep", Lstringprep_saslprep }, | 192 { "saslprep", Lstringprep_saslprep }, |
240 { NULL, NULL } | 193 { NULL, NULL } |
241 }; | 194 }; |
195 #else /* USE_STRINGPREP_ICU */ | |
196 | |
197 /****************** libidn ********************/ | |
198 | |
199 #include <stringprep.h> | |
200 | |
201 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) | |
202 { | |
203 size_t len; | |
204 const char *s; | |
205 char string[1024]; | |
206 int ret; | |
207 if(!lua_isstring(L, 1)) { | |
208 lua_pushnil(L); | |
209 return 1; | |
210 } | |
211 s = lua_tolstring(L, 1, &len); | |
212 if (len >= 1024) { | |
213 lua_pushnil(L); | |
214 return 1; /* TODO return error message */ | |
215 } | |
216 strcpy(string, s); | |
217 ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile); | |
218 if (ret == STRINGPREP_OK) { | |
219 lua_pushstring(L, string); | |
220 return 1; | |
221 } else { | |
222 lua_pushnil(L); | |
223 return 1; /* TODO return error message */ | |
224 } | |
225 } | |
226 | |
227 #define MAKE_PREP_FUNC(myFunc, prep) \ | |
228 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); } | |
229 | |
230 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep) /** stringprep.nameprep(s) */ | |
231 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep) /** stringprep.nodeprep(s) */ | |
232 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep) /** stringprep.resourceprep(s) */ | |
233 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep) /** stringprep.saslprep(s) */ | |
234 | |
235 static const luaL_Reg Reg_stringprep[] = | |
236 { | |
237 { "nameprep", Lstringprep_nameprep }, | |
238 { "nodeprep", Lstringprep_nodeprep }, | |
239 { "resourceprep", Lstringprep_resourceprep }, | |
240 { "saslprep", Lstringprep_saslprep }, | |
241 { NULL, NULL } | |
242 }; | |
242 #endif | 243 #endif |
243 | 244 |
244 /***************** IDNA *****************/ | 245 /***************** IDNA *****************/ |
245 #ifndef USE_STRINGPREP_ICU | 246 #ifdef USE_STRINGPREP_ICU |
247 #include <unicode/ustdio.h> | |
248 #include <unicode/uidna.h> | |
249 /* IDNA2003 or IDNA2008 ? ? ? */ | |
250 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ | |
251 { | |
252 size_t len; | |
253 int32_t ulen, dest_len, output_len; | |
254 const char *s = luaL_checklstring(L, 1, &len); | |
255 UChar ustr[1024]; | |
256 UErrorCode err = U_ZERO_ERROR; | |
257 UChar dest[1024]; | |
258 char output[1024]; | |
259 | |
260 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); | |
261 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); | |
262 if (U_FAILURE(err)) { | |
263 lua_pushnil(L); | |
264 return 1; | |
265 } else { | |
266 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); | |
267 if(output_len < 1024) | |
268 lua_pushlstring(L, output, output_len); | |
269 else | |
270 lua_pushnil(L); | |
271 return 1; | |
272 } | |
273 } | |
274 | |
275 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ | |
276 { | |
277 size_t len; | |
278 int32_t ulen, dest_len, output_len; | |
279 const char *s = luaL_checklstring(L, 1, &len); | |
280 UChar ustr[1024]; | |
281 UErrorCode err = U_ZERO_ERROR; | |
282 UChar dest[1024]; | |
283 char output[1024]; | |
284 | |
285 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); | |
286 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); | |
287 if (U_FAILURE(err)) { | |
288 lua_pushnil(L); | |
289 return 1; | |
290 } else { | |
291 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); | |
292 if(output_len < 1024) | |
293 lua_pushlstring(L, output, output_len); | |
294 else | |
295 lua_pushnil(L); | |
296 return 1; | |
297 } | |
298 } | |
299 | |
300 #else /* USE_STRINGPREP_ICU */ | |
246 /****************** libidn ********************/ | 301 /****************** libidn ********************/ |
247 | 302 |
248 #include <idna.h> | 303 #include <idna.h> |
249 #include <idn-free.h> | 304 #include <idn-free.h> |
250 | 305 |
279 lua_pushnil(L); | 334 lua_pushnil(L); |
280 idn_free(output); | 335 idn_free(output); |
281 return 1; /* TODO return error message */ | 336 return 1; /* TODO return error message */ |
282 } | 337 } |
283 } | 338 } |
284 #else | |
285 #include <unicode/ustdio.h> | |
286 #include <unicode/uidna.h> | |
287 /* IDNA2003 or IDNA2008 ? ? ? */ | |
288 static int Lidna_to_ascii(lua_State *L) /** idna.to_ascii(s) */ | |
289 { | |
290 size_t len; | |
291 int32_t ulen, dest_len, output_len; | |
292 const char *s = luaL_checklstring(L, 1, &len); | |
293 UChar ustr[1024]; | |
294 UErrorCode err = U_ZERO_ERROR; | |
295 UChar dest[1024]; | |
296 char output[1024]; | |
297 | |
298 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); | |
299 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); | |
300 if (U_FAILURE(err)) { | |
301 lua_pushnil(L); | |
302 return 1; | |
303 } else { | |
304 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); | |
305 if(output_len < 1024) | |
306 lua_pushlstring(L, output, output_len); | |
307 else | |
308 lua_pushnil(L); | |
309 return 1; | |
310 } | |
311 } | |
312 | |
313 static int Lidna_to_unicode(lua_State *L) /** idna.to_unicode(s) */ | |
314 { | |
315 size_t len; | |
316 int32_t ulen, dest_len, output_len; | |
317 const char *s = luaL_checklstring(L, 1, &len); | |
318 UChar ustr[1024]; | |
319 UErrorCode err = U_ZERO_ERROR; | |
320 UChar dest[1024]; | |
321 char output[1024]; | |
322 | |
323 u_strFromUTF8(ustr, 1024, &ulen, s, len, &err); | |
324 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); | |
325 if (U_FAILURE(err)) { | |
326 lua_pushnil(L); | |
327 return 1; | |
328 } else { | |
329 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); | |
330 if(output_len < 1024) | |
331 lua_pushlstring(L, output, output_len); | |
332 else | |
333 lua_pushnil(L); | |
334 return 1; | |
335 } | |
336 } | |
337 #endif | 339 #endif |
338 | 340 |
339 static const luaL_Reg Reg_idna[] = | 341 static const luaL_Reg Reg_idna[] = |
340 { | 342 { |
341 { "to_ascii", Lidna_to_ascii }, | 343 { "to_ascii", Lidna_to_ascii }, |