diff --git a/icu4c/source/common/uidna.cpp b/icu4c/source/common/uidna.cpp index 549c93c775a..d66b576dd3c 100644 --- a/icu4c/source/common/uidna.cpp +++ b/icu4c/source/common/uidna.cpp @@ -205,7 +205,7 @@ _internal_toASCII(const UChar* src, int32_t srcLength, UChar b1Stack[MAX_LABEL_BUFFER_SIZE], b2Stack[MAX_LABEL_BUFFER_SIZE]; //initialize pointers to stack buffers UChar *b1 = b1Stack, *b2 = b2Stack; - int32_t b1Len, b2Len, + int32_t b1Len=0, b2Len, b1Capacity = MAX_LABEL_BUFFER_SIZE, b2Capacity = MAX_LABEL_BUFFER_SIZE , reqLength=0; @@ -224,30 +224,48 @@ _internal_toASCII(const UChar* src, int32_t srcLength, UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0); int32_t failPos = -1; - - // step 2 - 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; - } + if(srcLength == -1){ + srcLength = u_strlen(src); + } - *status = U_ZERO_ERROR; // reset error + // step 1 + for( j=0;j 0x7F){ + srcIsASCII = FALSE; + } + b1[b1Len++] = src[j]; + } + + // step 2 is performed only if the source contains non ASCII + if(srcIsASCII == FALSE){ - b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status); + // step 2 + 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); + } } // error bail out if(U_FAILURE(*status)){ goto CLEANUP; } - // step 3 & 4 + // for step 3 & 4 + srcIsASCII = TRUE; for( j=0;j 0x7F){ srcIsASCII = FALSE; }else if(isLDHChar(b1[j])==FALSE){ // if the char is in ASCII range verify that it is an LDH character @@ -255,7 +273,6 @@ _internal_toASCII(const UChar* src, int32_t srcLength, failPos = j; } } - if(useSTD3ASCIIRules == TRUE){ // verify 3a and 3b // 3(a) Verify the absence of non-LDH ASCII code points; that is, the @@ -282,6 +299,7 @@ _internal_toASCII(const UChar* src, int32_t srcLength, goto CLEANUP; } } + // Step 4: if the source is ASCII then proceed to step 8 if(srcIsASCII){ if(b1Len <= destCapacity){ uprv_memmove(dest, b1, b1Len * U_SIZEOF_UCHAR); @@ -341,7 +359,7 @@ _internal_toASCII(const UChar* src, int32_t srcLength, goto CLEANUP; } } - + // step 8: verify the length of lable if(reqLength > MAX_LABEL_LENGTH){ *status = U_IDNA_LABEL_TOO_LONG_ERROR; } diff --git a/icu4c/source/common/utypes.c b/icu4c/source/common/utypes.c index 02f5f006120..e441a984487 100644 --- a/icu4c/source/common/utypes.c +++ b/icu4c/source/common/utypes.c @@ -159,9 +159,9 @@ _uRegexErrorName[U_REGEX_ERROR_LIMIT - U_REGEX_ERROR_START] = { static const char * const _uIDNAErrorName[U_IDNA_ERROR_LIMIT - U_IDNA_ERROR_START] = { "U_IDNA_ERROR_START", - "U_IDNA_PROHIBITED_ERROR", - "U_IDNA_UNASSIGNED_ERROR", - "U_IDNA_CHECK_BIDI_ERROR", + "U_STRINGPREP_PROHIBITED_ERROR", + "U_STRINGPREP_UNASSIGNED_ERROR", + "U_STRINGPREP_CHECK_BIDI_ERROR", "U_IDNA_STD3_ASCII_RULES_ERROR", "U_IDNA_ACE_PREFIX_ERROR", "U_IDNA_VERIFICATION_ERROR",