static void TestJB4475(){ static const UChar input[][10] = { {0x0054,0x0045,0x0053,0x0054,0x0000},/* TEST */ {0x0074,0x0065,0x0073,0x0074,0x0000} /* test */ }; int i; UChar output[40] = {0}; for(i=0; i< sizeof(input)/sizeof(input[0]); i++){ const UChar* src = input[i]; int32_t srcLen = u_strlen(src); UChar* dest = output; int32_t destLen = 40; UErrorCode status = U_ZERO_ERROR; UParseError ps; destLen = uidna_toASCII(src, srcLen, dest, destLen,UIDNA_DEFAULT, &ps, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status)); continue; } if(u_strncmp(input[i], dest, srcLen)!=0){ log_err("uidna_toASCII did not return the expected output.\n"); } } }
static void TestJB4490(){ static const UChar data[][50]= { {0x00F5,0x00dE,0x00dF,0x00dD, 0x0000}, {0xFB00,0xFB01} }; UChar output1[40] = {0}; UChar output2[40] = {0}; int32_t i; for(i=0; i< sizeof(data)/sizeof(data[0]); i++){ const UChar* src1 = data[i]; int32_t src1Len = u_strlen(src1); UChar* dest1 = output1; int32_t dest1Len = 40; UErrorCode status = U_ZERO_ERROR; UParseError ps; UChar* src2 = NULL; int32_t src2Len = 0; UChar* dest2 = output2; int32_t dest2Len = 40; dest1Len = uidna_toASCII(src1, src1Len, dest1, dest1Len,UIDNA_DEFAULT, &ps, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status)); } src2 = dest1; src2Len = dest1Len; dest2Len = uidna_toUnicode(src2, src2Len, dest2, dest2Len, UIDNA_DEFAULT, &ps, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status)); } } }
static void TestUnicode32Norm() { /* * test Unicode 3.2 normalization, before Public Review Issue #29 * see cnormtst.c TestComposition() */ static const UChar strings[][8]={ { 0x1100, 0x0300, 0x1161, 0x0327 }, { 0x0b47, 0x0300, 0x0b3e, 0x0327 } }; UChar ascii[20], unicode[20]; int32_t i, length; UErrorCode errorCode; for(i=0; i<LENGTHOF(strings); ++i) { errorCode=U_ZERO_ERROR; length=uidna_toASCII(strings[i], -1, ascii, LENGTHOF(ascii), 0, NULL, &errorCode); length=uidna_toUnicode(ascii, length, unicode, LENGTHOF(unicode), 0, NULL, &errorCode); if(errorCode!=U_IDNA_VERIFICATION_ERROR) { log_err("string %d yields %s instead of U_IDNA_VERIFICATION_ERROR\n", i, u_errorName(errorCode)); } } }
static void TestLength(){ { static const char* cl = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label"; UChar ul[128] = {'\0'}; UChar dest[256] = {'\0'}; /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an IDNA prepared string (including xn--)that is exactly 63 bytes long */ UChar ul1[] = { 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774, 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0xD574, 0xD55C, 0xB2E4, 0xBA74, 0xC138, 0x0041, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0x0000 }; int32_t len1 = LENGTHOF(ul1)-1/*remove the null termination*/; int32_t destLen = LENGTHOF(dest); UErrorCode status = U_ZERO_ERROR; UParseError ps; int32_t len = (int32_t)strlen(cl); u_charsToUChars(cl, ul, len+1); destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_ZERO_ERROR){ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = -1; destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_ZERO_ERROR){ log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = (int32_t)strlen(cl); destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_LABEL_TOO_LONG_ERROR){ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = -1; destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_LABEL_TOO_LONG_ERROR){ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_ZERO_ERROR){ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len1 = -1; destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_ZERO_ERROR){ log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status)); } } { static const char* cl = "my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.ibm.com"; UChar ul[400] = {'\0'}; UChar dest[400] = {'\0'}; int32_t destLen = LENGTHOF(dest); UErrorCode status = U_ZERO_ERROR; UParseError ps; int32_t len = (int32_t)strlen(cl); u_charsToUChars(cl, ul, len+1); destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = -1; destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = (int32_t)strlen(cl); destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; destLen = LENGTHOF(dest); len = -1; destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status)); } status = U_ZERO_ERROR; uidna_compare(ul, len, ul, len, UIDNA_DEFAULT, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status)); } uidna_compare(ul, -1, ul, -1, UIDNA_DEFAULT, &status); if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){ log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status)); } } }
static int32_t _internal_toUnicode(const UChar* src, int32_t srcLength, UChar* dest, int32_t destCapacity, int32_t options, UStringPrepProfile* nameprep, UParseError* parseError, UErrorCode* status) { //get the options //UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0); int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0; // TODO Revisit buffer handling. The label should not be over 63 ASCII characters. ICU4J may need to be updated too. UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE], b3Stack[MAX_LABEL_BUFFER_SIZE]; //initialize pointers to stack buffers UChar *b1 = b1Stack, *b2 = b2Stack, *b1Prime=NULL, *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; b1Len = 0; UBool* caseFlags = NULL; UBool srcIsASCII = TRUE; /*UBool srcIsLDH = TRUE; int32_t failPos =0;*/ // 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; }/*else if(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 if(srcLength > 0){ for(int32_t j=0; j<srcLength; j++){ if(src[j]> 0x7f){ srcIsASCII = FALSE; }/*else if(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; }*/ } }else{ return 0; } if(srcIsASCII == FALSE){ // step 2: process the string b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Capacity, namePrepOptions, parseError, status); if(*status == U_BUFFER_OVERFLOW_ERROR){ // redo processing of string /* we do not have enough room so grow the buffer*/ b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR); if(b1==NULL){ *status = U_MEMORY_ALLOCATION_ERROR; goto CLEANUP; } *status = U_ZERO_ERROR; // reset error b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status); } //bail out on error if(U_FAILURE(*status)){ goto CLEANUP; } }else{ //just point src to b1 b1 = (UChar*) src; b1Len = srcLength; } // The RFC states that // <quote> // ToUnicode never fails. If any step fails, then the original input // is returned immediately in that step. // </quote> //step 3: verify ACE Prefix if(startsWithPrefix(b1,b1Len)){ //step 4: Remove the ACE Prefix b1Prime = b1 + ACE_PREFIX_LENGTH; b1PrimeLen = b1Len - ACE_PREFIX_LENGTH; //step 5: Decode using punycode b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Capacity, caseFlags,status); if(*status == U_BUFFER_OVERFLOW_ERROR){ // redo processing of string /* we do not have enough room so grow the buffer*/ b2 = (UChar*) uprv_malloc(b2Len * U_SIZEOF_UCHAR); if(b2==NULL){ *status = U_MEMORY_ALLOCATION_ERROR; goto CLEANUP; } *status = U_ZERO_ERROR; // reset error b2Len = u_strFromPunycode(b1Prime, b1PrimeLen, b2, b2Len, caseFlags, status); } //step 6:Apply toASCII b3Len = uidna_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 = (UChar*) uprv_malloc(b3Len * U_SIZEOF_UCHAR); if(b3==NULL){ *status = U_MEMORY_ALLOCATION_ERROR; goto CLEANUP; } *status = U_ZERO_ERROR; // reset error b3Len = uidna_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){ // Cause the original to be returned. *status = U_IDNA_VERIFICATION_ERROR; goto CLEANUP; } //step 8: return output of step 5 reqLength = b2Len; if(b2Len <= destCapacity) { uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR); } } else{ // See the start of this if statement for why this is commented out. // 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; } }*/ // just return the source //copy the source to destination if(srcLength <= destCapacity){ uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR); } reqLength = srcLength; } CLEANUP: if(b1 != b1Stack && b1!=src){ 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){ // srcLength should have already been set earlier. U_ASSERT(srcLength >= 0); uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR); } reqLength = srcLength; *status = U_ZERO_ERROR; } return u_terminateUChars(dest, destCapacity, reqLength, status); }