]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/uidna.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / common / uidna.cpp
index eb4e30f1bc196f09e0fc53ab3bc43d575bde4756..c9ab8026258c5b1e81a397600f97f58aed81744d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *******************************************************************************
  *
- *   Copyright (C) 2003, International Business Machines
+ *   Copyright (C) 2003-2014, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
 
 #include "unicode/uidna.h"
 #include "unicode/ustring.h"
-#include "strprep.h"
+#include "unicode/usprep.h"
 #include "punycode.h"
 #include "ustr_imp.h"
 #include "cmemory.h"
+#include "uassert.h"
 #include "sprpimpl.h"
 
 /* it is official IDNA ACE Prefix is "xn--" */
@@ -31,15 +32,18 @@ static const UChar 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
+/* The Max length of the labels should not be more than MAX_LABEL_LENGTH */
+#define MAX_LABEL_BUFFER_SIZE 100
+
+#define MAX_DOMAIN_NAME_LENGTH 255
+/* The Max length of the domain names should not be more than MAX_DOMAIN_NAME_LENGTH */
+#define MAX_IDN_BUFFER_SIZE   MAX_DOMAIN_NAME_LENGTH+1
 
-#define CAPITAL_A        0x0041
-#define CAPITAL_Z        0x005A
 #define LOWER_CASE_DELTA 0x0020
+#define HYPHEN           0x002D
 #define FULL_STOP        0x002E
+#define CAPITAL_A        0x0041
+#define CAPITAL_Z        0x005A
 
 inline static UChar 
 toASCIILower(UChar ch){
@@ -65,12 +69,6 @@ startsWithPrefix(const UChar* src , int32_t srcLength){
     return startsWithPrefix;
 }
 
-inline static void 
-toASCIILower(UChar* src, int32_t srcLen){
-    for(int32_t i=0; i<srcLen; i++){
-        src[i] = toASCIILower(src[i]);
-    }
-}
 
 inline static int32_t
 compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len, 
@@ -120,29 +118,97 @@ compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
 }
 
 
-U_CAPI int32_t U_EXPORT2
-uidna_toASCII(const UChar* src, int32_t srcLength, 
-              UChar* dest, int32_t destCapacity,
-              int32_t options,
-              UParseError* parseError,
-              UErrorCode* status){
-    
-    if(status == NULL || U_FAILURE(*status)){
-        return 0;
+/**
+ * Ascertain if the given code point is a label separator as 
+ * defined by the IDNA RFC
+ * 
+ * @param ch The code point to be ascertained
+ * @return true if the char is a label separator
+ * @stable ICU 2.8
+ */
+static inline UBool isLabelSeparator(UChar ch){
+    switch(ch){
+        case 0x002e:
+        case 0x3002:
+        case 0xFF0E:
+        case 0xFF61:
+            return TRUE;
+        default:
+            return FALSE;           
     }
-    if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
-        *status = U_ILLEGAL_ARGUMENT_ERROR;
-        return 0;
+}
+
+// returns the length of the label excluding the separator
+// if *limit == separator then the length returned does not include 
+// the separtor.
+static inline int32_t
+getNextSeparator(UChar *src, int32_t srcLength,
+                 UChar **limit, UBool *done){
+    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(isLabelSeparator(src[i])){
+                *limit = src + (i+1); // go past the delimiter
+                return i;
+                
+            }
+        }
+    }else{
+        int32_t i;
+        for(i=0;i<srcLength;i++){
+            if(isLabelSeparator(src[i])){
+                *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;
+    }
+}
+static inline UBool isLDHChar(UChar ch){
+    // high runner case
+    if(ch>0x007A){
+        return FALSE;
     }
+    //[\\u002D \\u0030-\\u0039 \\u0041-\\u005A \\u0061-\\u007A]
+    if( (ch==0x002D) || 
+        (0x0030 <= ch && ch <= 0x0039) ||
+        (0x0041 <= ch && ch <= 0x005A) ||
+        (0x0061 <= ch && ch <= 0x007A)
+      ){
+        return TRUE;
+    }
+    return FALSE;
+}
+
+static int32_t 
+_internal_toASCII(const UChar* src, int32_t srcLength, 
+                  UChar* dest, int32_t destCapacity,
+                  int32_t options,
+                  UStringPrepProfile* nameprep,
+                  UParseError* parseError,
+                  UErrorCode* status)
+{
+
+    // 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];
     //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;
 
-
+    int32_t namePrepOptions = ((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0;
     UBool* caseFlags = NULL;
     
     // the source contains all ascii codepoints
@@ -153,54 +219,82 @@ uidna_toASCII(const UChar* src, int32_t srcLength,
     int32_t j=0;
 
     //get the options
-    UBool allowUnassigned   = (UBool)((options & UIDNA_ALLOW_UNASSIGNED) != 0);
     UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
-    
-    int32_t failPos = -1;
-    // step 2
-    StringPrep* prep = StringPrep::createNameprepInstance(*status);
 
-    if(U_FAILURE(*status)){
-        goto CLEANUP;
-    }
+    int32_t failPos = -1;
     
-    b1Len = prep->process(src,srcLength,b1, b1Capacity,allowUnassigned, parseError, *status);
+    if(srcLength == -1){
+        srcLength = u_strlen(src);
+    }
     
-    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(srcLength > b1Capacity){
+        b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
         if(b1==NULL){
             *status = U_MEMORY_ALLOCATION_ERROR;
             goto CLEANUP;
         }
+        b1Capacity = srcLength;
+    }
 
-        *status = U_ZERO_ERROR; // reset error
+    // step 1 
+    for( j=0;j<srcLength;j++){
+        if(src[j] > 0x7F){
+            srcIsASCII = FALSE;
+        }
+        b1[b1Len++] = src[j];
+    }
+    
+    // step 2 is performed only if the source contains non ASCII
+    if(srcIsASCII == FALSE){
         
-        b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, 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
+            if(b1 != b1Stack){
+                uprv_free(b1);
+            }
+            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;
     }
+    if(b1Len == 0){
+        *status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
+        goto CLEANUP;
+    }
 
-    // step 3 & 4
+    // for step 3 & 4
+    srcIsASCII = TRUE;
     for( j=0;j<b1Len;j++){
+        // check if output of usprep_prepare is all ASCII 
         if(b1[j] > 0x7F){
             srcIsASCII = FALSE;
-        }
-        // here we do not assemble surrogates
-        // since we know that LDH code points
-        // are in the ASCII range only
-        if(prep->isLDHChar(b1[j])==FALSE){
+        }else if(isLDHChar(b1[j])==FALSE){  // if the char is in ASCII range verify that it is an LDH character
             srcIsLDH = FALSE;
             failPos = j;
         }
     }
-    
     if(useSTD3ASCIIRules == TRUE){
         // verify 3a and 3b
-        if( srcIsLDH == FALSE /* source contains some non-LDH characters */
+        // 3(a) Verify the absence of non-LDH ASCII code points; that is, the
+        //  absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
+        // 3(b) Verify the absence of leading and trailing hyphen-minus; that
+        //  is, the absence of U+002D at the beginning and end of the
+        //  sequence.
+        if( srcIsLDH == FALSE /* source at this point should not contain anyLDH characters */
             || b1[0] ==  HYPHEN || b1[b1Len-1] == HYPHEN){
             *status = U_IDNA_STD3_ASCII_RULES_ERROR;
 
@@ -219,6 +313,7 @@ uidna_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);
@@ -278,7 +373,7 @@ uidna_toASCII(const UChar* src, int32_t srcLength,
             goto CLEANUP;
         }
     }
-
+    // step 8: verify the length of label
     if(reqLength > MAX_LABEL_LENGTH){
         *status = U_IDNA_LABEL_TOO_LONG_ERROR;
     }
@@ -292,52 +387,38 @@ CLEANUP:
     }
     uprv_free(caseFlags);
     
-    delete prep;
-
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
 
-
-U_CAPI int32_t U_EXPORT2
-uidna_toUnicode(const UChar* src, int32_t srcLength,
-                UChar* dest, int32_t destCapacity,
-                int32_t options,
-                UParseError* parseError,
-                UErrorCode* status){
-
-    if(status == NULL || U_FAILURE(*status)){
-        return 0;
-    }
-    if( (src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
-        *status = U_ILLEGAL_ARGUMENT_ERROR;
-        return 0;
-    }
+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 allowUnassigned   = (UBool)((options & UIDNA_ALLOW_UNASSIGNED) != 0);
-    UBool useSTD3ASCIIRules = (UBool)((options & UIDNA_USE_STD3_RULES) != 0);
-    
+    //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,
+    int32_t b1Len = 0, b2Len, b1PrimeLen, b3Len,
             b1Capacity = MAX_LABEL_BUFFER_SIZE, 
             b2Capacity = MAX_LABEL_BUFFER_SIZE,
             b3Capacity = MAX_LABEL_BUFFER_SIZE,
             reqLength=0;
-    
-    StringPrep* prep = StringPrep::createNameprepInstance(*status);
-    b1Len = 0;
+
     UBool* caseFlags = NULL;
 
     UBool srcIsASCII = TRUE;
-    UBool srcIsLDH = TRUE;
-    int32_t failPos =0;
-
-    if(U_FAILURE(*status)){
-        goto CLEANUP;
-    }
+    /*UBool srcIsLDH = TRUE;
+    int32_t failPos =0;*/
 
     // step 1: find out if all the codepoints in src are ASCII  
     if(srcLength==-1){
@@ -345,34 +426,34 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
         for(;src[srcLength]!=0;){
             if(src[srcLength]> 0x7f){
                 srcIsASCII = FALSE;
-            }
-            // here we do not assemble surrogates
-            // since we know that LDH code points
-            // are in the ASCII range only
-            if(prep->isLDHChar(src[srcLength])==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{
+    }else if(srcLength > 0){
         for(int32_t j=0; j<srcLength; j++){
             if(src[j]> 0x7f){
                 srcIsASCII = FALSE;
-            }
-            // here we do not assemble surrogates
-            // since we know that LDH code points
-            // are in the ASCII range only
-            if(prep->isLDHChar(src[j])==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 = prep->process(src,srcLength,b1,b1Capacity,allowUnassigned, parseError, *status);
+        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*/
@@ -384,7 +465,7 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
 
             *status = U_ZERO_ERROR; // reset error
             
-            b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned,  parseError, *status);
+            b1Len = usprep_prepare(nameprep, src, srcLength, b1, b1Len, namePrepOptions, parseError, status);
         }
         //bail out on error
         if(U_FAILURE(*status)){
@@ -397,8 +478,14 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
         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(src,srcLength)){
+    if(startsWithPrefix(b1,b1Len)){
 
         //step 4: Remove the ACE Prefix
         b1Prime = b1 + ACE_PREFIX_LENGTH;
@@ -406,7 +493,7 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
 
         //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*/
@@ -417,14 +504,13 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
             }
 
             *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);
+        b3Len = uidna_toASCII(b2, b2Len, b3, b3Capacity, options, parseError, status);
 
         if(*status == U_BUFFER_OVERFLOW_ERROR){
             // redo processing of string
@@ -436,9 +522,9 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
             }
 
             *status = U_ZERO_ERROR; // reset error
-            
+
             b3Len =  uidna_toASCII(b2,b2Len,b3,b3Len,options,parseError, status);
-        
+
         }
         //bail out on error
         if(U_FAILURE(*status)){
@@ -447,7 +533,8 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
 
         //step 7: verify
         if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){
-            *status = U_IDNA_VERIFICATION_ERROR; 
+            // Cause the original to be returned.
+            *status = U_IDNA_VERIFICATION_ERROR;
             goto CLEANUP;
         }
 
@@ -456,14 +543,16 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
         if(b2Len <= destCapacity) {
             uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR);
         }
-    }else{
+    }
+    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 */
+        /*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 */
+                // populate the parseError struct
                 if(srcIsLDH==FALSE){
                     // failPos is always set the index of failure
                     uprv_syntaxError(src,failPos, srcLength,parseError);
@@ -477,7 +566,8 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
 
                 goto CLEANUP;
             }
-        }
+        }*/
+        // just return the source
         //copy the source to destination
         if(srcLength <= destCapacity){
             uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
@@ -485,6 +575,7 @@ uidna_toUnicode(const UChar* src, int32_t srcLength,
         reqLength = srcLength;
     }
 
+
 CLEANUP:
 
     if(b1 != b1Stack && b1!=src){
@@ -494,9 +585,7 @@ CLEANUP:
         uprv_free(b2);
     }
     uprv_free(caseFlags);
-    
-    delete prep;
-    
+
     // The RFC states that 
     // <quote>
     // ToUnicode never fails. If any step fails, then the original input
@@ -506,57 +595,75 @@ CLEANUP:
     if(U_FAILURE(*status)){
         //copy the source to destination
         if(dest && srcLength <= destCapacity){
-          if(srcLength == -1) {
-            uprv_memmove(dest,src,u_strlen(src)* U_SIZEOF_UCHAR);
-          } else {
+            // 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);
 }
 
-// returns the length of the label excluding the separator
-// if *limit == separator then the length returned does not include 
-// the separtor.
-static int32_t
-getNextSeparator(UChar *src,int32_t srcLength,StringPrep* prep,
-                 UChar **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;
+U_CAPI int32_t U_EXPORT2
+uidna_toASCII(const UChar* src, int32_t srcLength, 
+              UChar* dest, int32_t destCapacity,
+              int32_t options,
+              UParseError* parseError,
+              UErrorCode* status){
+    
+    if(status == NULL || U_FAILURE(*status)){
+        return 0;
+    }
+    if((src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
+        *status = U_ILLEGAL_ARGUMENT_ERROR;
+        return 0;
+    }
 
-        return i;
+    UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
+    
+    if(U_FAILURE(*status)){
+        return -1;
     }
+    
+    int32_t retLen = _internal_toASCII(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
+    
+    /* close the profile*/
+    usprep_close(nameprep);
+    
+    return retLen;
 }
 
+U_CAPI int32_t U_EXPORT2
+uidna_toUnicode(const UChar* src, int32_t srcLength,
+                UChar* dest, int32_t destCapacity,
+                int32_t options,
+                UParseError* parseError,
+                UErrorCode* status){
+
+    if(status == NULL || U_FAILURE(*status)){
+        return 0;
+    }
+    if( (src==NULL) || (srcLength < -1) || (destCapacity<0) || (!dest && destCapacity > 0)){
+        *status = U_ILLEGAL_ARGUMENT_ERROR;
+        return 0;
+    }  
+
+    UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
+    
+    if(U_FAILURE(*status)){
+        return -1;
+    }
+    
+    int32_t retLen = _internal_toUnicode(src, srcLength, dest, destCapacity, options, nameprep, parseError, status);
+
+    usprep_close(nameprep);
+    
+    return retLen;
+}
+
+
 U_CAPI int32_t U_EXPORT2
 uidna_IDNToASCII(  const UChar *src, int32_t srcLength,
                    UChar* dest, int32_t destCapacity,
@@ -574,7 +681,7 @@ uidna_IDNToASCII(  const UChar *src, int32_t srcLength,
 
     int32_t reqLength = 0;
 
-    StringPrep* prep = StringPrep::createNameprepInstance(*status);
+    UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
     
     if(U_FAILURE(*status)){
         return 0;
@@ -592,16 +699,20 @@ uidna_IDNToASCII(  const UChar *src, int32_t srcLength,
 
     for(;;){
 
-        labelLen = getNextSeparator(labelStart,remainingLen, prep, &delimiter,&done, status);
+        labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
+        labelReqLength = 0;
+        if(!(labelLen==0 && done)){// make sure this is not a root label separator.
         
-        labelReqLength = uidna_toASCII( labelStart, labelLen, 
-                                        currentDest, remainingDestCapacity, 
-                                        options, parseError, status);
-
-        if(*status == U_BUFFER_OVERFLOW_ERROR){
-            
-            *status = U_ZERO_ERROR; // reset error
-            remainingDestCapacity = 0;
+            labelReqLength = _internal_toASCII( labelStart, labelLen, 
+                                                currentDest, remainingDestCapacity, 
+                                                options, nameprep, 
+                                                parseError, status);
+    
+            if(*status == U_BUFFER_OVERFLOW_ERROR){
+                
+                *status = U_ZERO_ERROR; // reset error
+                remainingDestCapacity = 0;
+            }
         }
 
     
@@ -618,6 +729,7 @@ uidna_IDNToASCII(  const UChar *src, int32_t srcLength,
             // should never occur
             remainingDestCapacity = 0;
         }
+
         if(done == TRUE){
             break;
         }
@@ -627,16 +739,20 @@ uidna_IDNToASCII(  const UChar *src, int32_t srcLength,
             *currentDest++ = FULL_STOP;
             remainingDestCapacity--;
         }
-        reqLength++;           
+        reqLength++;
 
         labelStart = delimiter;
         if(remainingLen >0 ){
-            remainingLen = srcLength - (delimiter - src);
+            remainingLen = (int32_t)(srcLength - (delimiter - src));
         }
 
     }
-   
-    delete prep;
+
+    if(reqLength > MAX_DOMAIN_NAME_LENGTH){
+        *status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
+    }
+
+    usprep_close(nameprep);
     
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
@@ -658,7 +774,7 @@ uidna_IDNToUnicode(  const UChar* src, int32_t srcLength,
 
     int32_t reqLength = 0;
 
-    StringPrep* prep = StringPrep::createNameprepInstance(*status);
+    UStringPrepProfile* nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, status);
     
     if(U_FAILURE(*status)){
         return 0;
@@ -673,22 +789,31 @@ uidna_IDNToUnicode(  const UChar* src, int32_t srcLength,
     int32_t labelLen = 0, labelReqLength = 0;
     UBool done = FALSE;
 
-
     for(;;){
 
-        labelLen = getNextSeparator(labelStart,remainingLen, prep, &delimiter,&done, status);
+        labelLen = getNextSeparator(labelStart,remainingLen, &delimiter,&done);
         
-        labelReqLength = uidna_toUnicode(labelStart, labelLen, 
-                                         currentDest, remainingDestCapacity, 
-                                         options, parseError, status);
+        // The RFC states that 
+        // <quote>
+        // ToUnicode never fails. If any step fails, then the original input
+        // is returned immediately in that step.
+        // </quote>
+        // _internal_toUnicode will copy the label.
+        /*if(labelLen==0 && done==FALSE){ 
+            *status = U_IDNA_ZERO_LENGTH_LABEL_ERROR;
+            break;
+        }*/
+        
+        labelReqLength = _internal_toUnicode(labelStart, labelLen, 
+                                             currentDest, remainingDestCapacity, 
+                                             options, nameprep, 
+                                             parseError, status);
 
         if(*status == U_BUFFER_OVERFLOW_ERROR){
-            
             *status = U_ZERO_ERROR; // reset error
             remainingDestCapacity = 0;
         }
 
-    
         if(U_FAILURE(*status)){
             break;
         }
@@ -708,20 +833,25 @@ uidna_IDNToUnicode(  const UChar* src, int32_t srcLength,
         }
 
         // add the label separator
+        // Unlike the ToASCII operation we don't normalize the label separators
         if(remainingDestCapacity > 0){
-            *currentDest++ = FULL_STOP;
+            *currentDest++ = *(labelStart + labelLen);
             remainingDestCapacity--;
         }
-        reqLength++;           
+        reqLength++;
 
         labelStart = delimiter;
         if(remainingLen >0 ){
-            remainingLen = srcLength - (delimiter - src);
+            remainingLen = (int32_t)(srcLength - (delimiter - src));
         }
 
     }
-   
-    delete prep;
+
+    if(reqLength > MAX_DOMAIN_NAME_LENGTH){
+        *status = U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR;
+    }
+
+    usprep_close(nameprep);
     
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }