Comparison

util-src/encodings.c @ 10261:010c67532ed0

util.encodings: Switch ICU binding to IDNA2008 (fixes #533, #1301)
author Kim Alvefur <zash@zash.se>
date Wed, 11 Sep 2019 00:14:59 +0200
parent 10006:5a5fd234dec7
child 10262:b1209bc15cd1
comparison
equal deleted inserted replaced
10260:d6b9cacfef76 10261:010c67532ed0
267 267
268 #include <unicode/usprep.h> 268 #include <unicode/usprep.h>
269 #include <unicode/ustring.h> 269 #include <unicode/ustring.h>
270 #include <unicode/utrace.h> 270 #include <unicode/utrace.h>
271 #include <unicode/uspoof.h> 271 #include <unicode/uspoof.h>
272 #include <unicode/uidna.h>
272 273
273 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) { 274 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) {
274 size_t input_len; 275 size_t input_len;
275 int32_t unprepped_len, prepped_len, output_len; 276 int32_t unprepped_len, prepped_len, output_len;
276 const char *input; 277 const char *input;
321 UStringPrepProfile *icu_nameprep; 322 UStringPrepProfile *icu_nameprep;
322 UStringPrepProfile *icu_nodeprep; 323 UStringPrepProfile *icu_nodeprep;
323 UStringPrepProfile *icu_resourceprep; 324 UStringPrepProfile *icu_resourceprep;
324 UStringPrepProfile *icu_saslprep; 325 UStringPrepProfile *icu_saslprep;
325 USpoofChecker *icu_spoofcheck; 326 USpoofChecker *icu_spoofcheck;
327 UIDNA *icu_idna2008;
326 328
327 #if (U_ICU_VERSION_MAJOR_NUM < 58) 329 #if (U_ICU_VERSION_MAJOR_NUM < 58)
328 /* COMPAT */ 330 /* COMPAT */
329 #define USPOOF_CONFUSABLE (USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE) 331 #define USPOOF_CONFUSABLE (USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_MIXED_SCRIPT_CONFUSABLE | USPOOF_WHOLE_SCRIPT_CONFUSABLE)
330 #endif 332 #endif
337 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err); 339 icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
338 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err); 340 icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
339 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err); 341 icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
340 icu_spoofcheck = uspoof_open(&err); 342 icu_spoofcheck = uspoof_open(&err);
341 uspoof_setChecks(icu_spoofcheck, USPOOF_CONFUSABLE, &err); 343 uspoof_setChecks(icu_spoofcheck, USPOOF_CONFUSABLE, &err);
344 icu_idna2008 = uidna_openUTS46(UIDNA_USE_STD3_RULES, &err);
342 345
343 if(U_FAILURE(err)) { 346 if(U_FAILURE(err)) {
344 fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err)); 347 fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
345 } 348 }
346 } 349 }
432 if(U_FAILURE(err)) { 435 if(U_FAILURE(err)) {
433 lua_pushnil(L); 436 lua_pushnil(L);
434 return 1; 437 return 1;
435 } 438 }
436 439
437 dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); 440 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
438 441 dest_len = uidna_nameToASCII(icu_idna2008, ustr, ulen, dest, 256, &info, &err);
439 if(U_FAILURE(err)) { 442
443 if(U_FAILURE(err) || info.errors) {
440 lua_pushnil(L); 444 lua_pushnil(L);
441 return 1; 445 return 1;
442 } else { 446 } else {
443 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); 447 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
444 448
466 if(U_FAILURE(err)) { 470 if(U_FAILURE(err)) {
467 lua_pushnil(L); 471 lua_pushnil(L);
468 return 1; 472 return 1;
469 } 473 }
470 474
471 dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err); 475 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
472 476 dest_len = uidna_nameToUnicode(icu_idna2008, ustr, ulen, dest, 1024, &info, &err);
473 if(U_FAILURE(err)) { 477
478 if(U_FAILURE(err) || info.errors) {
474 lua_pushnil(L); 479 lua_pushnil(L);
475 return 1; 480 return 1;
476 } else { 481 } else {
477 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err); 482 u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
478 483