1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2003-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: idnaref.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2003feb1
* created by: Ram Viswanadha
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
#include "idnaref.h"
#include "punyref.h"
#include "ustr_imp.h"
#include "cmemory.h"
#include "sprpimpl.h"
#include "nptrans.h"
#include "testidna.h"
#include "punycode.h"
#include "unicode/ustring.h"
/* it is official IDNA ACE Prefix is "xn--" */
static const char16_t ACE_PREFIX[] ={ 0x0078,0x006E,0x002d,0x002d } ;
#define ACE_PREFIX_LENGTH 4
#define MAX_LABEL_LENGTH 63
#define HYPHEN 0x002D
/* The Max length of the labels should not be more than 64 */
#define MAX_LABEL_BUFFER_SIZE 100
#define MAX_IDN_BUFFER_SIZE 300
#define CAPITAL_A 0x0041
#define CAPITAL_Z 0x005A
#define LOWER_CASE_DELTA 0x0020
#define FULL_STOP 0x002E
inline static UBool
startsWithPrefix(const char16_t* src , int32_t srcLength){
UBool startsWithPrefix = true;
if(srcLength < ACE_PREFIX_LENGTH){
return false;
}
for(int8_t i=0; i< ACE_PREFIX_LENGTH; i++){
if(u_tolower(src[i]) != ACE_PREFIX[i]){
startsWithPrefix = false;
}
}
return startsWithPrefix;
}
inline static char16_t
toASCIILower(char16_t ch){
if(CAPITAL_A <= ch && ch <= CAPITAL_Z){
return ch + LOWER_CASE_DELTA;
}
return ch;
}
inline static int32_t
compareCaseInsensitiveASCII(const char16_t* s1, int32_t s1Len,
const char16_t* s2, int32_t s2Len){
if(s1Len != s2Len){
return (s1Len > s2Len) ? s1Len : s2Len;
}
char16_t c1,c2;
int32_t rc;
for(int32_t i =0;/* no condition */;i++) {
/* If we reach the ends of both strings then they match */
if(i == s1Len) {
return 0;
}
c1 = s1[i];
c2 = s2[i];
/* Case-insensitive comparison */
if(c1!=c2) {
rc = static_cast<int32_t>(toASCIILower(c1)) - static_cast<int32_t>(toASCIILower(c2));
if(rc!=0) {
return rc;
}
}
}
}
static UErrorCode getError(enum punycode_status status){
switch(status){
case punycode_success:
return U_ZERO_ERROR;
case punycode_bad_input: /* Input is invalid. */
return U_INVALID_CHAR_FOUND;
case punycode_big_output: /* Output would exceed the space provided. */
return U_BUFFER_OVERFLOW_ERROR;
case punycode_overflow : /* Input requires wider integers to process. */
return U_INDEX_OUTOFBOUNDS_ERROR;
default:
return U_INTERNAL_PROGRAM_ERROR;
}
}
static inline int32_t convertASCIIToUChars(const char* src,char16_t* dest, int32_t length){
int i;
for(i=0;i<length;i++){
dest[i] = src[i];
}
return i;
}
static inline int32_t convertUCharsToASCII(const char16_t* src,char* dest, int32_t length){
int i;
for(i=0;i<length;i++){
dest[i] = static_cast<char>(src[i]);
}
return i;
}
// wrapper around the reference Punycode implementation
static int32_t convertToPuny(const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
UErrorCode& status){
uint32_t b1Stack[MAX_LABEL_BUFFER_SIZE];
int32_t b1Len = 0, b1Capacity = MAX_LABEL_BUFFER_SIZE;
uint32_t* b1 = b1Stack;
char b2Stack[MAX_LABEL_BUFFER_SIZE];
char* b2 = b2Stack;
int32_t b2Len =MAX_LABEL_BUFFER_SIZE ;
punycode_status error;
unsigned char* caseFlags = nullptr;
u_strToUTF32(reinterpret_cast<UChar32*>(b1), b1Capacity, &b1Len, src, srcLength, &status);
if(status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = static_cast<uint32_t*>(uprv_malloc(b1Len * sizeof(uint32_t)));
if(b1==nullptr){
status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
status = U_ZERO_ERROR; // reset error
u_strToUTF32(reinterpret_cast<UChar32*>(b1), b1Len, &b1Len, src, srcLength, &status);
}
if(U_FAILURE(status)){
goto CLEANUP;
}
//caseFlags = (unsigned char*) uprv_malloc(b1Len *sizeof(unsigned char));
error = punycode_encode(b1Len, b1, caseFlags, reinterpret_cast<uint32_t*>(&b2Len), b2);
status = getError(error);
if(status == U_BUFFER_OVERFLOW_ERROR){
/* we do not have enough room so grow the buffer*/
b2 = static_cast<char*>(uprv_malloc(b2Len * sizeof(char)));
if(b2==nullptr){
status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
status = U_ZERO_ERROR; // reset error
punycode_status error = punycode_encode(b1Len, b1, caseFlags, reinterpret_cast<uint32_t*>(&b2Len), b2);
status = getError(error);
}
if(U_FAILURE(status)){
goto CLEANUP;
}
if(b2Len < destCapacity){
convertASCIIToUChars(b2,dest,b2Len);
}else{
status =U_BUFFER_OVERFLOW_ERROR;
}
CLEANUP:
if(b1Stack != b1){
uprv_free(b1);
}
if(b2Stack != b2){
uprv_free(b2);
}
uprv_free(caseFlags);
return b2Len;
}
static NamePrepTransform* getInstance(UErrorCode& status){
TestIDNA *thisTest = dynamic_cast<TestIDNA *>(IntlTest::gTest);
if (thisTest == nullptr && U_SUCCESS(status)) {
status = U_INTERNAL_PROGRAM_ERROR;
}
if (U_FAILURE(status)) return nullptr;
return thisTest->getInstance(status);
}
static int32_t convertFromPuny( const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
UErrorCode& status){
char b1Stack[MAX_LABEL_BUFFER_SIZE];
char* b1 = b1Stack;
int32_t destLen =0;
convertUCharsToASCII(src, b1,srcLength);
uint32_t b2Stack[MAX_LABEL_BUFFER_SIZE];
uint32_t* b2 = b2Stack;
int32_t b2Len =MAX_LABEL_BUFFER_SIZE;
unsigned char* caseFlags = nullptr; //(unsigned char*) uprv_malloc(srcLength * sizeof(unsigned char*));
punycode_status error = punycode_decode(srcLength, b1, reinterpret_cast<uint32_t*>(&b2Len), b2, caseFlags);
status = getError(error);
if(status == U_BUFFER_OVERFLOW_ERROR){
b2 = static_cast<uint32_t*>(uprv_malloc(b2Len * sizeof(uint32_t)));
if(b2 == nullptr){
status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
error = punycode_decode(srcLength, b1, reinterpret_cast<uint32_t*>(&b2Len), b2, caseFlags);
status = getError(error);
}
if(U_FAILURE(status)){
goto CLEANUP;
}
u_strFromUTF32(dest, destCapacity, &destLen, reinterpret_cast<UChar32*>(b2), b2Len, &status);
CLEANUP:
if(b1Stack != b1){
uprv_free(b1);
}
if(b2Stack != b2){
uprv_free(b2);
}
uprv_free(caseFlags);
return destLen;
}
U_CFUNC int32_t U_EXPORT2
idnaref_toASCII(const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
int32_t options,
UParseError* parseError,
UErrorCode* status){
if(status == nullptr || U_FAILURE(*status)){
return 0;
}
if((src == nullptr) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
char16_t b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE];
//initialize pointers to stack buffers
char16_t *b1 = b1Stack, *b2 = b2Stack;
int32_t b1Len=0, b2Len=0,
b1Capacity = MAX_LABEL_BUFFER_SIZE,
b2Capacity = MAX_LABEL_BUFFER_SIZE ,
reqLength=0;
//get the options
UBool allowUnassigned = (options & IDNAREF_ALLOW_UNASSIGNED) != 0;
UBool useSTD3ASCIIRules = (options & IDNAREF_USE_STD3_RULES) != 0;
UBool* caseFlags = nullptr;
// assume the source contains all ascii codepoints
UBool srcIsASCII = true;
// assume the source contains all LDH codepoints
UBool srcIsLDH = true;
int32_t j=0;
if(srcLength == -1){
srcLength = u_strlen(src);
}
// step 1
for( j=0;j<srcLength;j++){
if(src[j] > 0x7F){
srcIsASCII = false;
}
b1[b1Len++] = src[j];
}
NamePrepTransform* prep = getInstance(*status);
if(U_FAILURE(*status)){
goto CLEANUP;
}
// step 2 is performed only if the source contains non ASCII
if (!srcIsASCII) {
b1Len = prep->process(src,srcLength,b1, b1Capacity,allowUnassigned,parseError,*status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status);
}
// error bail out
if(U_FAILURE(*status)){
goto CLEANUP;
}
}
if(b1Len == 0){
*status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
goto CLEANUP;
}
srcIsASCII = true;
// step 3 & 4
for( j=0;j<b1Len;j++){
if(b1[j] > 0x7F){// check if output of usprep_prepare is all ASCII
srcIsASCII = false;
}else if(prep->isLDHChar(b1[j])==false){ // if the char is in ASCII range verify that it is an LDH character{
srcIsLDH = false;
}
}
if(useSTD3ASCIIRules == true){
// verify 3a and 3b
if( srcIsLDH == false /* source contains some non-LDH characters */
|| b1[0] == HYPHEN || b1[b1Len-1] == HYPHEN){
*status = U_IDNA_STD3_ASCII_RULES_ERROR;
goto CLEANUP;
}
}
if(srcIsASCII){
if(b1Len <= destCapacity){
u_memmove(dest, b1, b1Len);
reqLength = b1Len;
}else{
reqLength = b1Len;
goto CLEANUP;
}
}else{
// step 5 : verify the sequence does not begin with ACE prefix
if(!startsWithPrefix(b1,b1Len)){
//step 6: encode the sequence with punycode
//caseFlags = (UBool*) uprv_malloc(b1Len * sizeof(UBool));
b2Len = convertToPuny(b1,b1Len, b2,b2Capacity,*status);
//b2Len = u_strToPunycode(b2,b2Capacity,b1,b1Len, caseFlags, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b2 = (char16_t*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
if(b2 == nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b2Len = convertToPuny(b1, b1Len, b2, b2Len, *status);
//b2Len = u_strToPunycode(b2,b2Len,b1,b1Len, caseFlags, status);
}
//error bail out
if(U_FAILURE(*status)){
goto CLEANUP;
}
reqLength = b2Len+ACE_PREFIX_LENGTH;
if(reqLength > destCapacity){
*status = U_BUFFER_OVERFLOW_ERROR;
goto CLEANUP;
}
//Step 7: prepend the ACE prefix
u_memcpy(dest, ACE_PREFIX, ACE_PREFIX_LENGTH);
//Step 6: copy the contents in b2 into dest
u_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len);
}else{
*status = U_IDNA_ACE_PREFIX_ERROR;
goto CLEANUP;
}
}
if(reqLength > MAX_LABEL_LENGTH){
*status = U_IDNA_LABEL_TOO_LONG_ERROR;
}
CLEANUP:
if(b1 != b1Stack){
uprv_free(b1);
}
if(b2 != b2Stack){
uprv_free(b2);
}
uprv_free(caseFlags);
// delete prep;
return u_terminateUChars(dest, destCapacity, reqLength, status);
}
U_CFUNC int32_t U_EXPORT2
idnaref_toUnicode(const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
int32_t options,
UParseError* parseError,
UErrorCode* status){
if(status == nullptr || U_FAILURE(*status)){
return 0;
}
if((src == nullptr) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
char16_t b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE], b3Stack[MAX_LABEL_BUFFER_SIZE];
//initialize pointers to stack buffers
char16_t *b1 = b1Stack, *b2 = b2Stack, *b1Prime=nullptr, *b3=b3Stack;
int32_t b1Len, b2Len, b1PrimeLen, b3Len,
b1Capacity = MAX_LABEL_BUFFER_SIZE,
b2Capacity = MAX_LABEL_BUFFER_SIZE,
b3Capacity = MAX_LABEL_BUFFER_SIZE,
reqLength=0;
// UParseError parseError;
NamePrepTransform* prep = getInstance(*status);
b1Len = 0;
UBool* caseFlags = nullptr;
//get the options
UBool allowUnassigned = (options & IDNAREF_ALLOW_UNASSIGNED) != 0;
UBool useSTD3ASCIIRules = (options & IDNAREF_USE_STD3_RULES) != 0;
UBool srcIsASCII = true;
UBool srcIsLDH = true;
int32_t failPos =0;
if(U_FAILURE(*status)){
goto CLEANUP;
}
// step 1: find out if all the codepoints in src are ASCII
if(srcLength==-1){
srcLength = 0;
for(;src[srcLength]!=0;){
if(src[srcLength]> 0x7f){
srcIsASCII = false;
}if(prep->isLDHChar(src[srcLength])==false){
// here we do not assemble surrogates
// since we know that LDH code points
// are in the ASCII range only
srcIsLDH = false;
failPos = srcLength;
}
srcLength++;
}
}else{
for(int32_t j=0; j<srcLength; j++){
if(src[j]> 0x7f){
srcIsASCII = false;
}else if(prep->isLDHChar(src[j])==false){
// here we do not assemble surrogates
// since we know that LDH code points
// are in the ASCII range only
srcIsLDH = false;
failPos = j;
}
}
}
if(srcIsASCII == false){
// step 2: process the string
b1Len = prep->process(src,srcLength,b1,b1Capacity,allowUnassigned, parseError, *status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status);
}
//bail out on error
if(U_FAILURE(*status)){
goto CLEANUP;
}
}else{
// copy everything to b1
if(srcLength < b1Capacity){
u_memmove(b1, src, srcLength);
}else{
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
u_memmove(b1, src, srcLength);
}
b1Len = srcLength;
}
//step 3: verify ACE Prefix
if(startsWithPrefix(src,srcLength)){
//step 4: Remove the ACE Prefix
b1Prime = b1 + ACE_PREFIX_LENGTH;
b1PrimeLen = b1Len - ACE_PREFIX_LENGTH;
//step 5: Decode using punycode
b2Len = convertFromPuny(b1Prime,b1PrimeLen, b2, b2Capacity, *status);
//b2Len = u_strFromPunycode(b2, b2Capacity,b1Prime,b1PrimeLen, caseFlags, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b2 = (char16_t*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
if(b2==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b2Len = convertFromPuny(b1Prime,b1PrimeLen, b2, b2Len, *status);
//b2Len = u_strFromPunycode(b2, b2Len,b1Prime,b1PrimeLen,caseFlags, status);
}
//step 6:Apply toASCII
b3Len = idnaref_toASCII(b2,b2Len,b3,b3Capacity,options,parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b3 = (char16_t*) uprv_malloc(b3Len * U_SIZEOF_UCHAR);
if(b3==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b3Len = idnaref_toASCII(b2,b2Len,b3,b3Len, options, parseError, status);
}
//bail out on error
if(U_FAILURE(*status)){
goto CLEANUP;
}
//step 7: verify
if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){
*status = U_IDNA_VERIFICATION_ERROR;
goto CLEANUP;
}
//step 8: return output of step 5
reqLength = b2Len;
if(b2Len <= destCapacity) {
u_memmove(dest, b2, b2Len);
}
}else{
// verify that STD3 ASCII rules are satisfied
if(useSTD3ASCIIRules == true){
if( srcIsLDH == false /* source contains some non-LDH characters */
|| src[0] == HYPHEN || src[srcLength-1] == HYPHEN){
*status = U_IDNA_STD3_ASCII_RULES_ERROR;
/* populate the parseError struct */
if(srcIsLDH==false){
// failPos is always set the index of failure
uprv_syntaxError(src,failPos, srcLength,parseError);
}else if(src[0] == HYPHEN){
// fail position is 0
uprv_syntaxError(src,0,srcLength,parseError);
}else{
// the last index in the source is always length-1
uprv_syntaxError(src, (srcLength>0) ? srcLength-1 : srcLength, srcLength,parseError);
}
goto CLEANUP;
}
}
//copy the source to destination
if(srcLength <= destCapacity){
u_memmove(dest, src, srcLength);
}
reqLength = srcLength;
}
CLEANUP:
if(b1 != b1Stack){
uprv_free(b1);
}
if(b2 != b2Stack){
uprv_free(b2);
}
uprv_free(caseFlags);
// The RFC states that
// <quote>
// ToUnicode never fails. If any step fails, then the original input
// is returned immediately in that step.
// </quote>
// So if any step fails lets copy source to destination
if(U_FAILURE(*status)){
//copy the source to destination
if(dest && srcLength <= destCapacity){
if(srcLength == -1) {
u_memmove(dest, src, u_strlen(src));
} else {
u_memmove(dest, src, srcLength);
}
}
reqLength = srcLength;
*status = U_ZERO_ERROR;
}
return u_terminateUChars(dest, destCapacity, reqLength, status);
}
static int32_t
getNextSeparator(char16_t *src,int32_t srcLength,NamePrepTransform* prep,
char16_t **limit,
UBool *done,
UErrorCode *status){
if(srcLength == -1){
int32_t i;
for(i=0 ; ;i++){
if(src[i] == 0){
*limit = src + i; // point to null
*done = true;
return i;
}
if(prep->isLabelSeparator(src[i],*status)){
*limit = src + (i+1); // go past the delimiter
return i;
}
}
}else{
int32_t i;
for(i=0;i<srcLength;i++){
if(prep->isLabelSeparator(src[i],*status)){
*limit = src + (i+1); // go past the delimiter
return i;
}
}
// we have not found the delimiter
if(i==srcLength){
*limit = src+srcLength;
*done = true;
}
return i;
}
}
U_CFUNC int32_t U_EXPORT2
idnaref_IDNToASCII( const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
int32_t options,
UParseError* parseError,
UErrorCode* status){
if(status == nullptr || U_FAILURE(*status)){
return 0;
}
if((src == nullptr) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
int32_t reqLength = 0;
// UParseError parseError;
NamePrepTransform* prep = getInstance(*status);
//initialize pointers to stack buffers
char16_t b1Stack[MAX_LABEL_BUFFER_SIZE];
char16_t *b1 = b1Stack;
int32_t b1Len, labelLen;
char16_t* delimiter = (char16_t*)src;
char16_t* labelStart = (char16_t*)src;
int32_t remainingLen = srcLength;
int32_t b1Capacity = MAX_LABEL_BUFFER_SIZE;
//get the options
// UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
// UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
UBool done = false;
if(U_FAILURE(*status)){
goto CLEANUP;
}
if(srcLength == -1){
for(;;){
if(*delimiter == 0){
break;
}
labelLen = getNextSeparator(labelStart, -1, prep, &delimiter, &done, status);
b1Len = 0;
if(!(labelLen==0 && done)){// make sure this is not a root label separator.
b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Capacity,
options, parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Len,
options, parseError, status);
}
}
if(U_FAILURE(*status)){
goto CLEANUP;
}
int32_t tempLen = (reqLength + b1Len );
// copy to dest
if( tempLen< destCapacity){
u_memmove(dest+reqLength, b1, b1Len);
}
reqLength = tempLen;
// add the label separator
if(done == false){
if(reqLength < destCapacity){
dest[reqLength] = FULL_STOP;
}
reqLength++;
}
labelStart = delimiter;
}
}else{
for(;;){
if(delimiter == src+srcLength){
break;
}
labelLen = getNextSeparator(labelStart, remainingLen, prep, &delimiter, &done, status);
b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Capacity,
options,parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Len,
options, parseError, status);
}
if(U_FAILURE(*status)){
goto CLEANUP;
}
int32_t tempLen = (reqLength + b1Len );
// copy to dest
if( tempLen< destCapacity){
u_memmove(dest+reqLength, b1, b1Len);
}
reqLength = tempLen;
// add the label separator
if(done == false){
if(reqLength < destCapacity){
dest[reqLength] = FULL_STOP;
}
reqLength++;
}
labelStart = delimiter;
remainingLen = static_cast<int32_t>(srcLength - (delimiter - src));
}
}
CLEANUP:
if(b1 != b1Stack){
uprv_free(b1);
}
// delete prep;
return u_terminateUChars(dest, destCapacity, reqLength, status);
}
U_CFUNC int32_t U_EXPORT2
idnaref_IDNToUnicode( const char16_t* src, int32_t srcLength,
char16_t* dest, int32_t destCapacity,
int32_t options,
UParseError* parseError,
UErrorCode* status){
if(status == nullptr || U_FAILURE(*status)){
return 0;
}
if((src == nullptr) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
int32_t reqLength = 0;
UBool done = false;
NamePrepTransform* prep = getInstance(*status);
//initialize pointers to stack buffers
char16_t b1Stack[MAX_LABEL_BUFFER_SIZE];
char16_t *b1 = b1Stack;
int32_t b1Len, labelLen;
char16_t* delimiter = (char16_t*)src;
char16_t* labelStart = (char16_t*)src;
int32_t remainingLen = srcLength;
int32_t b1Capacity = MAX_LABEL_BUFFER_SIZE;
//get the options
// UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
// UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
if(U_FAILURE(*status)){
goto CLEANUP;
}
if(srcLength == -1){
for(;;){
if(*delimiter == 0){
break;
}
labelLen = getNextSeparator(labelStart, -1, prep, &delimiter, &done, status);
if(labelLen==0 && done==false){
*status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
}
b1Len = idnaref_toUnicode(labelStart, labelLen, b1, b1Capacity,
options, parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len,
options, parseError, status);
}
if(U_FAILURE(*status)){
goto CLEANUP;
}
int32_t tempLen = (reqLength + b1Len );
// copy to dest
if( tempLen< destCapacity){
u_memmove(dest+reqLength, b1, b1Len);
}
reqLength = tempLen;
// add the label separator
if(done == false){
if(reqLength < destCapacity){
dest[reqLength] = FULL_STOP;
}
reqLength++;
}
labelStart = delimiter;
}
}else{
for(;;){
if(delimiter == src+srcLength){
break;
}
labelLen = getNextSeparator(labelStart, remainingLen, prep, &delimiter, &done, status);
if(labelLen==0 && done==false){
*status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
}
b1Len = idnaref_toUnicode( labelStart,labelLen, b1, b1Capacity,
options, parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len,
options, parseError, status);
}
if(U_FAILURE(*status)){
goto CLEANUP;
}
int32_t tempLen = (reqLength + b1Len );
// copy to dest
if( tempLen< destCapacity){
u_memmove(dest+reqLength, b1, b1Len);
}
reqLength = tempLen;
// add the label separator
if(done == false){
if(reqLength < destCapacity){
dest[reqLength] = FULL_STOP;
}
reqLength++;
}
labelStart = delimiter;
remainingLen = static_cast<int32_t>(srcLength - (delimiter - src));
}
}
CLEANUP:
if(b1 != b1Stack){
uprv_free(b1);
}
// delete prep;
return u_terminateUChars(dest, destCapacity, reqLength, status);
}
U_CFUNC int32_t U_EXPORT2
idnaref_compare( const char16_t *s1, int32_t length1,
const char16_t *s2, int32_t length2,
int32_t options,
UErrorCode* status){
if(status == nullptr || U_FAILURE(*status)){
return -1;
}
char16_t b1Stack[MAX_IDN_BUFFER_SIZE], b2Stack[MAX_IDN_BUFFER_SIZE];
char16_t *b1 = b1Stack, *b2 = b2Stack;
int32_t b1Len, b2Len, b1Capacity = MAX_IDN_BUFFER_SIZE, b2Capacity = MAX_IDN_BUFFER_SIZE;
int32_t result = -1;
UParseError parseError;
b1Len = idnaref_IDNToASCII(s1, length1, b1, b1Capacity, options, &parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b1 = (char16_t*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
if(b1==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b1Len = idnaref_IDNToASCII(s1,length1,b1,b1Len, options, &parseError, status);
}
b2Len = idnaref_IDNToASCII(s2,length2,b2,b2Capacity,options, &parseError, status);
if(*status == U_BUFFER_OVERFLOW_ERROR){
// redo processing of string
/* we do not have enough room so grow the buffer*/
b2 = (char16_t*) uprv_malloc(b2Len * U_SIZEOF_UCHAR);
if(b2==nullptr){
*status = U_MEMORY_ALLOCATION_ERROR;
goto CLEANUP;
}
*status = U_ZERO_ERROR; // reset error
b2Len = idnaref_IDNToASCII(s2,length2,b2,b2Len,options, &parseError, status);
}
// when toASCII is applied all label separators are replaced with FULL_STOP
result = compareCaseInsensitiveASCII(b1,b1Len,b2,b2Len);
CLEANUP:
if(b1 != b1Stack){
uprv_free(b1);
}
if(b2 != b2Stack){
uprv_free(b2);
}
return result;
}
#endif /* #if !UCONFIG_NO_IDNA */
|