]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/idnaref.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / test / intltest / idnaref.cpp
index 90803970f0523ca62e4a2cda37cc85dc7ac76222..eabe1f56d3a1206cd6d314c916a311903528b9d6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *******************************************************************************
  *
- *   Copyright (C) 2003, International Business Machines
+ *   Copyright (C) 2003-2007, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
@@ -18,7 +18,6 @@
 
 #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
 #include "idnaref.h"
-#include "strprep.h"
 #include "punyref.h"
 #include "ustr_imp.h"
 #include "cmemory.h"
 #include "unicode/ustring.h"
 
 /* it is official IDNA ACE Prefix is "xn--" */
-static const UChar ACE_PREFIX[] ={ 0x0058,0x004E,0x002d,0x002d } ;
+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_LABEL_BUFFER_SIZE 100
 #define MAX_IDN_BUFFER_SIZE   300
 
 #define CAPITAL_A        0x0041
@@ -44,22 +43,7 @@ static const UChar ACE_PREFIX[] ={ 0x0058,0x004E,0x002d,0x002d } ;
 #define FULL_STOP        0x002E
 
 
-NamePrepTransform* TestIDNA::prep = NULL;
-
-NamePrepTransform* TestIDNA::getInstance(UErrorCode& status){
-    if(TestIDNA::prep == NULL){
-        UParseError parseError;
-        TestIDNA::prep = NamePrepTransform::createInstance(parseError, status);
-        if(TestIDNA::prep ==NULL){
-           //status = U_MEMORY_ALLOCATION_ERROR;
-           return NULL;
-        }
-    }
-    return TestIDNA::prep;
-
-}
-
-inline static UBool 
+inline static UBool
 startsWithPrefix(const UChar* src , int32_t srcLength){
     UBool startsWithPrefix = TRUE;
 
@@ -68,14 +52,14 @@ startsWithPrefix(const UChar* src , int32_t srcLength){
     }
 
     for(int8_t i=0; i< ACE_PREFIX_LENGTH; i++){
-        if(u_toupper(src[i]) != ACE_PREFIX[i]){
+        if(u_tolower(src[i]) != ACE_PREFIX[i]){
             startsWithPrefix = FALSE;
         }
     }
     return startsWithPrefix;
 }
 
-inline static UChar 
+inline static UChar
 toASCIILower(UChar ch){
     if(CAPITAL_A <= ch && ch <= CAPITAL_Z){
         return ch + LOWER_CASE_DELTA;
@@ -84,7 +68,7 @@ toASCIILower(UChar ch){
 }
 
 inline static int32_t
-compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len, 
+compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
                             const UChar* s2, int32_t s2Len){
     if(s1Len != s2Len){
         return (s1Len > s2Len) ? s1Len : s2Len;
@@ -97,10 +81,10 @@ compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
         if(i == s1Len) {
             return 0;
         }
-        
+
         c1 = s1[i];
         c2 = s2[i];
-        
+
         /* Case-insensitive comparison */
         if(c1!=c2) {
             rc=(int32_t)toASCIILower(c1)-(int32_t)toASCIILower(c2);
@@ -109,7 +93,7 @@ compareCaseInsensitiveASCII(const UChar* s1, int32_t s1Len,
             }
         }
     }
-    
+
 }
 
 static UErrorCode getError(enum punycode_status status){
@@ -141,9 +125,9 @@ static inline int32_t convertUCharsToASCII(const UChar* src,char* dest, int32_t
     }
     return i;
 }
-// wrapper around the reference Punycode implementation 
-static int32_t convertToPuny(const UChar* src, int32_t srcLength, 
-                                                        UChar* dest, int32_t destCapacity,
+// wrapper around the reference Punycode implementation
+static int32_t convertToPuny(const UChar* src, int32_t srcLength,
+                             UChar* dest, int32_t destCapacity,
                              UErrorCode& status){
     uint32_t b1Stack[MAX_LABEL_BUFFER_SIZE];
     int32_t b1Len = 0, b1Capacity = MAX_LABEL_BUFFER_SIZE;
@@ -165,7 +149,7 @@ static int32_t convertToPuny(const UChar* src, int32_t srcLength,
         }
 
         status = U_ZERO_ERROR; // reset error
-        
+
         u_strToUTF32((UChar32*)b1,b1Len,&b1Len,src,srcLength,&status);
     }
     if(U_FAILURE(status)){
@@ -176,7 +160,7 @@ static int32_t convertToPuny(const UChar* src, int32_t srcLength,
 
     error = punycode_encode(b1Len,b1,caseFlags, (uint32_t*)&b2Len, b2);
     status = getError(error);
-    
+
     if(status == U_BUFFER_OVERFLOW_ERROR){
         /* we do not have enough room so grow the buffer*/
         b2 = (char*) uprv_malloc( b2Len * sizeof(char));
@@ -193,7 +177,7 @@ static int32_t convertToPuny(const UChar* src, int32_t srcLength,
     if(U_FAILURE(status)){
         goto CLEANUP;
     }
-    
+
     if(b2Len < destCapacity){
           convertASCIIToUChars(b2,dest,b2Len);
     }else{
@@ -213,7 +197,7 @@ CLEANUP:
 }
 
 static int32_t convertFromPuny(  const UChar* src, int32_t srcLength,
-                                                                UChar* dest, int32_t destCapacity,
+                                 UChar* dest, int32_t destCapacity,
                                  UErrorCode& status){
     char b1Stack[MAX_LABEL_BUFFER_SIZE];
     char* b1 = b1Stack;
@@ -252,18 +236,17 @@ CLEANUP:
     }
     uprv_free(caseFlags);
 
-    return destLen;   
+    return destLen;
 }
 
 
-
-U_CFUNC int32_t  
-idnaref_toASCII(const UChar* src, int32_t srcLength, 
-                         UChar* dest, int32_t destCapacity,
+U_CFUNC int32_t U_EXPORT2
+idnaref_toASCII(const UChar* src, int32_t srcLength,
+              UChar* dest, int32_t destCapacity,
               int32_t options,
-                         UParseError* parseError,
+              UParseError* parseError,
               UErrorCode* status){
-    
+
     if(status == NULL || U_FAILURE(*status)){
         return 0;
     }
@@ -274,32 +257,43 @@ idnaref_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, 
-            b1Capacity = MAX_LABEL_BUFFER_SIZE, 
+    int32_t b1Len=0, b2Len=0,
+            b1Capacity = MAX_LABEL_BUFFER_SIZE,
             b2Capacity = MAX_LABEL_BUFFER_SIZE ,
             reqLength=0;
 
-       //get the options
+    //get the options
     UBool allowUnassigned   = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
     UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
 
     UBool* caseFlags = NULL;
-    
+
     // assume the source contains all ascii codepoints
     UBool srcIsASCII  = TRUE;
     // assume the source contains all LDH codepoints
-    UBool srcIsLDH = TRUE; 
+    UBool srcIsLDH = TRUE;
     int32_t j=0;
-//    UParseError parseError;
+
+    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];
+    }
     // step 2
     NamePrepTransform* prep = TestIDNA::getInstance(*status);
 
     if(U_FAILURE(*status)){
         goto CLEANUP;
     }
-    
+
     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*/
@@ -310,7 +304,7 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
         }
 
         *status = U_ZERO_ERROR; // reset error
-        
+
         b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status);
     }
     // error bail out
@@ -318,12 +312,21 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
         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) srcIsASCII = FALSE;
-        srcIsLDH = prep->isLDHChar(b1[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 */
@@ -337,7 +340,7 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
             uprv_memmove(dest, b1, b1Len * U_SIZEOF_UCHAR);
             reqLength = b1Len;
         }else{
-                       reqLength = b1Len;
+            reqLength = b1Len;
             goto CLEANUP;
         }
     }else{
@@ -352,14 +355,14 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
             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); 
+                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 = convertToPuny(b1, b1Len, b2, b2Len, *status);
                 //b2Len = u_strToPunycode(b2,b2Len,b1,b1Len, caseFlags, status);
 
@@ -380,7 +383,7 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
             uprv_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len * U_SIZEOF_UCHAR);
 
         }else{
-            *status = U_IDNA_ACE_PREFIX_ERROR; 
+            *status = U_IDNA_ACE_PREFIX_ERROR;
             goto CLEANUP;
         }
     }
@@ -397,18 +400,18 @@ CLEANUP:
         uprv_free(b2);
     }
     uprv_free(caseFlags);
-    
+
 //    delete prep;
 
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
 
 
-U_CFUNC int32_t  
+U_CFUNC int32_t U_EXPORT2
 idnaref_toUnicode(const UChar* src, int32_t srcLength,
-                               UChar* dest, int32_t destCapacity,
+                UChar* dest, int32_t destCapacity,
                 int32_t options,
-                               UParseError* parseError,
+                UParseError* parseError,
                 UErrorCode* status){
 
     if(status == NULL || U_FAILURE(*status)){
@@ -420,23 +423,23 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
     }
 
 
-    
+
     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, 
+            b1Capacity = MAX_LABEL_BUFFER_SIZE,
             b2Capacity = MAX_LABEL_BUFFER_SIZE,
             b3Capacity = MAX_LABEL_BUFFER_SIZE,
             reqLength=0;
 //    UParseError parseError;
-    
+
     NamePrepTransform* prep = TestIDNA::getInstance(*status);
     b1Len = 0;
     UBool* caseFlags = NULL;
 
-       //get the options
+    //get the options
     UBool allowUnassigned   = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
     UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
 
@@ -447,17 +450,16 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
     if(U_FAILURE(*status)){
         goto CLEANUP;
     }
-    // step 1: find out if all the codepoints in src are ASCII  
+    // 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;
-            }
-            // 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){
+            }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;
             }
@@ -467,11 +469,10 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
         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(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;
             }
@@ -491,7 +492,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
             }
 
             *status = U_ZERO_ERROR; // reset error
-            
+
             b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status);
         }
         //bail out on error
@@ -535,12 +536,12 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
             }
 
             *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);
 
@@ -554,9 +555,9 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
             }
 
             *status = U_ZERO_ERROR; // reset error
-            
+
             b3Len =  idnaref_toASCII(b2,b2Len,b3,b3Len, options, parseError, status);
-        
+
         }
         //bail out on error
         if(U_FAILURE(*status)){
@@ -565,7 +566,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
 
         //step 7: verify
         if(compareCaseInsensitiveASCII(b1, b1Len, b3, b3Len) !=0){
-            *status = U_IDNA_VERIFICATION_ERROR; 
+            *status = U_IDNA_VERIFICATION_ERROR;
             goto CLEANUP;
         }
 
@@ -586,7 +587,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
                     // failPos is always set the index of failure
                     uprv_syntaxError(src,failPos, srcLength,parseError);
                 }else if(src[0] == HYPHEN){
-                    // fail position is 0 
+                    // fail position is 0
                     uprv_syntaxError(src,0,srcLength,parseError);
                 }else{
                     // the last index in the source is always length-1
@@ -612,9 +613,25 @@ 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
+    // 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) {
+            uprv_memmove(dest,src,u_strlen(src)* U_SIZEOF_UCHAR);
+          } else {
+            uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
+          }
+        }
+        reqLength = srcLength;
+        *status = U_ZERO_ERROR;
+    }
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
 
@@ -635,7 +652,7 @@ getNextSeparator(UChar *src,int32_t srcLength,NamePrepTransform* prep,
             if(prep->isLabelSeparator(src[i],*status)){
                 *limit = src + (i+1); // go past the delimiter
                 return i;
-                
+
             }
         }
     }else{
@@ -655,11 +672,11 @@ getNextSeparator(UChar *src,int32_t srcLength,NamePrepTransform* prep,
     }
 }
 
-U_CFUNC int32_t 
+U_CFUNC int32_t U_EXPORT2
 idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
-                                  UChar* dest, int32_t destCapacity,
-                                  int32_t options,
-                                  UParseError* parseError,
+                   UChar* dest, int32_t destCapacity,
+                   int32_t options,
+                   UParseError* parseError,
                    UErrorCode* status){
 
     if(status == NULL || U_FAILURE(*status)){
@@ -674,7 +691,7 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
 //    UParseError parseError;
 
     NamePrepTransform* prep = TestIDNA::getInstance(*status);
-    
+
     //initialize pointers to stack buffers
     UChar b1Stack[MAX_LABEL_BUFFER_SIZE];
     UChar  *b1 = b1Stack;
@@ -683,8 +700,8 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
     UChar* labelStart = (UChar*)src;
     int32_t remainingLen = srcLength;
     int32_t b1Capacity = MAX_LABEL_BUFFER_SIZE;
-    
-       //get the options
+
+    //get the options
 //    UBool allowUnassigned   = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
 //    UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
     UBool done = FALSE;
@@ -696,32 +713,35 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
 
     if(srcLength == -1){
         for(;;){
-            
+
             if(*delimiter == 0){
                 break;
             }
 
             labelLen = getNextSeparator(labelStart, -1, prep, &delimiter, &done, status);
-            
-            b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Capacity, 
-                                    options, parseError, status);
+            b1Len = 0;
+            if(!(labelLen==0 && done)){// make sure this is not a root label separator.
 
-            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 = idnaref_toASCII(labelStart, labelLen, b1, b1Len,
+                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 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
+                    if(b1==NULL){
+                        *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;
             }
@@ -745,13 +765,13 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
         }
     }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);
 
@@ -765,12 +785,12 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
                 }
 
                 *status = U_ZERO_ERROR; // reset error
-                
-                b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Len, 
+
+                b1Len = idnaref_toASCII(labelStart, labelLen, b1, b1Len,
                                         options, parseError, status);
-                
+
             }
-        
+
             if(U_FAILURE(*status)){
                 goto CLEANUP;
             }
@@ -797,23 +817,23 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
 
 
 CLEANUP:
-    
+
     if(b1 != b1Stack){
         uprv_free(b1);
     }
-    
+
 //   delete prep;
 
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
 
-U_CFUNC int32_t
+U_CFUNC int32_t U_EXPORT2
 idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
-                                    UChar* dest, int32_t destCapacity,
-                                        int32_t options,
-                                        UParseError* parseError,
+                     UChar* dest, int32_t destCapacity,
+                     int32_t options,
+                     UParseError* parseError,
                      UErrorCode* status){
-    
+
     if(status == NULL || U_FAILURE(*status)){
         return 0;
     }
@@ -823,11 +843,11 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
     }
 
     int32_t reqLength = 0;
-    
+
     UBool done = FALSE;
 
     NamePrepTransform* prep = TestIDNA::getInstance(*status);
-    
+
     //initialize pointers to stack buffers
     UChar b1Stack[MAX_LABEL_BUFFER_SIZE];
     UChar  *b1 = b1Stack;
@@ -836,24 +856,27 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
     UChar* labelStart = (UChar*)src;
     int32_t remainingLen = srcLength;
     int32_t b1Capacity = MAX_LABEL_BUFFER_SIZE;
-    
-       //get the options
+
+    //get the options
 //    UBool allowUnassigned   = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
 //    UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
-    
-       if(U_FAILURE(*status)){
+
+    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);
 
@@ -867,12 +890,12 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
                 }
 
                 *status = U_ZERO_ERROR; // reset error
-                
-                b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len, 
+
+                b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len,
                                            options, parseError, status);
-                
+
             }
-        
+
             if(U_FAILURE(*status)){
                 goto CLEANUP;
             }
@@ -895,13 +918,17 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
         }
     }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);
 
@@ -915,12 +942,12 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
                 }
 
                 *status = U_ZERO_ERROR; // reset error
-                
-                b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len, 
+
+                b1Len = idnaref_toUnicode( labelStart, labelLen, b1, b1Len,
                                            options, parseError, status);
-                
+
             }
-        
+
             if(U_FAILURE(*status)){
                 goto CLEANUP;
             }
@@ -946,20 +973,20 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
     }
 
 CLEANUP:
-    
+
     if(b1 != b1Stack){
         uprv_free(b1);
     }
-    
+
 //    delete prep;
-    
+
     return u_terminateUChars(dest, destCapacity, reqLength, status);
 }
 
-U_CFUNC int32_t
+U_CFUNC int32_t U_EXPORT2
 idnaref_compare(  const UChar *s1, int32_t length1,
                 const UChar *s2, int32_t length2,
-                               int32_t options,
+                int32_t options,
                 UErrorCode* status){
 
     if(status == NULL || U_FAILURE(*status)){
@@ -970,8 +997,8 @@ idnaref_compare(  const UChar *s1, int32_t length1,
     UChar *b1 = b1Stack, *b2 = b2Stack;
     int32_t b1Len, b2Len, b1Capacity = MAX_IDN_BUFFER_SIZE, b2Capacity = MAX_IDN_BUFFER_SIZE;
     int32_t result = -1;
-    
-       UParseError parseError;
+
+    UParseError parseError;
 
     b1Len = idnaref_IDNToASCII(s1, length1, b1, b1Capacity, options, &parseError, status);
     if(*status == U_BUFFER_OVERFLOW_ERROR){
@@ -984,9 +1011,9 @@ idnaref_compare(  const UChar *s1, int32_t length1,
         }
 
         *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);
@@ -1000,9 +1027,9 @@ idnaref_compare(  const UChar *s1, int32_t length1,
         }
 
         *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);