]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/idnaref.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / idnaref.cpp
index 8db476e8fb853d889947ea31574e408269ef6025..a292bb8482ab571fc14ff606732aaf3beecebbbe 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *******************************************************************************
  *
- *   Copyright (C) 2003-2007, International Business Machines
+ *   Copyright (C) 2003-2011, International Business Machines
  *   Corporation and others.  All Rights Reserved.
  *
  *******************************************************************************
@@ -274,23 +274,10 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
     UBool srcIsLDH = TRUE;
     int32_t j=0;
 
-    NamePrepTransform* prep = TestIDNA::getInstance(*status);
-
-    if(U_FAILURE(*status)){
-        goto CLEANUP;
-    }
-    
     if(srcLength == -1){
         srcLength = u_strlen(src);
     }
-    if(srcLength > b1Capacity){
-        b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
-        if(b1==NULL){
-            *status = U_MEMORY_ALLOCATION_ERROR;
-            goto CLEANUP;
-        }
-        b1Capacity = srcLength;
-    }
+
     // step 1
     for( j=0;j<srcLength;j++){
         if(src[j] > 0x7F){
@@ -298,29 +285,33 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
         }
         b1[b1Len++] = src[j];
     }
-    
-    // step 2
-    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*/
-         if(b1 != b1Stack){
-                uprv_free(b1);
+    NamePrepTransform* prep = TestIDNA::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 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
+            if(b1==NULL){
+                *status = U_MEMORY_ALLOCATION_ERROR;
+                goto CLEANUP;
+            }
+
+            *status = U_ZERO_ERROR; // reset error
+
+            b1Len = prep->process(src,srcLength,b1, b1Len,allowUnassigned, parseError, *status);
         }
-        b1 = (UChar*) uprv_malloc(b1Len * U_SIZEOF_UCHAR);
-        if(b1==NULL){
-            *status = U_MEMORY_ALLOCATION_ERROR;
+        // error bail out
+        if(U_FAILURE(*status)){
             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){
@@ -348,7 +339,7 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
     }
     if(srcIsASCII){
         if(b1Len <= destCapacity){
-            uprv_memmove(dest, b1, b1Len * U_SIZEOF_UCHAR);
+            u_memmove(dest, b1, b1Len);
             reqLength = b1Len;
         }else{
             reqLength = b1Len;
@@ -389,9 +380,9 @@ idnaref_toASCII(const UChar* src, int32_t srcLength,
                 goto CLEANUP;
             }
             //Step 7: prepend the ACE prefix
-            uprv_memcpy(dest,ACE_PREFIX,ACE_PREFIX_LENGTH * U_SIZEOF_UCHAR);
+            u_memcpy(dest, ACE_PREFIX, ACE_PREFIX_LENGTH);
             //Step 6: copy the contents in b2 into dest
-            uprv_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len * U_SIZEOF_UCHAR);
+            u_memcpy(dest+ACE_PREFIX_LENGTH, b2, b2Len);
 
         }else{
             *status = U_IDNA_ACE_PREFIX_ERROR;
@@ -514,7 +505,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
 
         // copy everything to b1
         if(srcLength < b1Capacity){
-            uprv_memmove(b1,src, srcLength * U_SIZEOF_UCHAR);
+            u_memmove(b1, src, srcLength);
         }else{
             /* we do not have enough room so grow the buffer*/
             b1 = (UChar*) uprv_malloc(srcLength * U_SIZEOF_UCHAR);
@@ -522,7 +513,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
                 *status = U_MEMORY_ALLOCATION_ERROR;
                 goto CLEANUP;
             }
-            uprv_memmove(b1,src, srcLength * U_SIZEOF_UCHAR);
+            u_memmove(b1, src, srcLength);
         }
         b1Len = srcLength;
     }
@@ -584,7 +575,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
         //step 8: return output of step 5
         reqLength = b2Len;
         if(b2Len <= destCapacity) {
-            uprv_memmove(dest, b2, b2Len * U_SIZEOF_UCHAR);
+            u_memmove(dest, b2, b2Len);
         }
     }else{
         // verify that STD3 ASCII rules are satisfied
@@ -610,7 +601,7 @@ idnaref_toUnicode(const UChar* src, int32_t srcLength,
         }
         //copy the source to destination
         if(srcLength <= destCapacity){
-            uprv_memmove(dest,src,srcLength * U_SIZEOF_UCHAR);
+            u_memmove(dest, src, srcLength);
         }
         reqLength = srcLength;
     }
@@ -625,8 +616,24 @@ CLEANUP:
     }
     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) {
+            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);
 }
 
@@ -743,7 +750,7 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
             int32_t tempLen = (reqLength + b1Len );
             // copy to dest
             if( tempLen< destCapacity){
-                uprv_memmove(dest+reqLength, b1, b1Len * U_SIZEOF_UCHAR);
+                u_memmove(dest+reqLength, b1, b1Len);
             }
 
             reqLength = tempLen;
@@ -792,7 +799,7 @@ idnaref_IDNToASCII(  const UChar* src, int32_t srcLength,
             int32_t tempLen = (reqLength + b1Len );
             // copy to dest
             if( tempLen< destCapacity){
-                uprv_memmove(dest+reqLength, b1, b1Len * U_SIZEOF_UCHAR);
+                u_memmove(dest+reqLength, b1, b1Len);
             }
 
             reqLength = tempLen;
@@ -897,7 +904,7 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
             int32_t tempLen = (reqLength + b1Len );
             // copy to dest
             if( tempLen< destCapacity){
-                uprv_memmove(dest+reqLength, b1, b1Len * U_SIZEOF_UCHAR);
+                u_memmove(dest+reqLength, b1, b1Len);
             }
 
             reqLength = tempLen;
@@ -949,7 +956,7 @@ idnaref_IDNToUnicode(  const UChar* src, int32_t srcLength,
             int32_t tempLen = (reqLength + b1Len );
             // copy to dest
             if( tempLen< destCapacity){
-                uprv_memmove(dest+reqLength, b1, b1Len * U_SIZEOF_UCHAR);
+                u_memmove(dest+reqLength, b1, b1Len);
             }
 
             reqLength = tempLen;