2 *******************************************************************************
4 * Copyright (C) 2003-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: idnaref.cpp
10 * tab size: 8 (not used)
13 * created on: 2003feb1
14 * created by: Ram Viswanadha
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
28 #include "unicode/ustring.h"
30 /* it is official IDNA ACE Prefix is "xn--" */
31 static const UChar ACE_PREFIX
[] ={ 0x0078,0x006E,0x002d,0x002d } ;
32 #define ACE_PREFIX_LENGTH 4
34 #define MAX_LABEL_LENGTH 63
36 /* The Max length of the labels should not be more than 64 */
37 #define MAX_LABEL_BUFFER_SIZE 100
38 #define MAX_IDN_BUFFER_SIZE 300
40 #define CAPITAL_A 0x0041
41 #define CAPITAL_Z 0x005A
42 #define LOWER_CASE_DELTA 0x0020
43 #define FULL_STOP 0x002E
47 startsWithPrefix(const UChar
* src
, int32_t srcLength
){
48 UBool startsWithPrefix
= TRUE
;
50 if(srcLength
< ACE_PREFIX_LENGTH
){
54 for(int8_t i
=0; i
< ACE_PREFIX_LENGTH
; i
++){
55 if(u_tolower(src
[i
]) != ACE_PREFIX
[i
]){
56 startsWithPrefix
= FALSE
;
59 return startsWithPrefix
;
63 toASCIILower(UChar ch
){
64 if(CAPITAL_A
<= ch
&& ch
<= CAPITAL_Z
){
65 return ch
+ LOWER_CASE_DELTA
;
71 compareCaseInsensitiveASCII(const UChar
* s1
, int32_t s1Len
,
72 const UChar
* s2
, int32_t s2Len
){
74 return (s1Len
> s2Len
) ? s1Len
: s2Len
;
79 for(int32_t i
=0;/* no condition */;i
++) {
80 /* If we reach the ends of both strings then they match */
88 /* Case-insensitive comparison */
90 rc
=(int32_t)toASCIILower(c1
)-(int32_t)toASCIILower(c2
);
99 static UErrorCode
getError(enum punycode_status status
){
101 case punycode_success
:
103 case punycode_bad_input
: /* Input is invalid. */
104 return U_INVALID_CHAR_FOUND
;
105 case punycode_big_output
: /* Output would exceed the space provided. */
106 return U_BUFFER_OVERFLOW_ERROR
;
107 case punycode_overflow
: /* Input requires wider integers to process. */
108 return U_INDEX_OUTOFBOUNDS_ERROR
;
110 return U_INTERNAL_PROGRAM_ERROR
;
114 static inline int32_t convertASCIIToUChars(const char* src
,UChar
* dest
, int32_t length
){
116 for(i
=0;i
<length
;i
++){
121 static inline int32_t convertUCharsToASCII(const UChar
* src
,char* dest
, int32_t length
){
123 for(i
=0;i
<length
;i
++){
124 dest
[i
] = (char)src
[i
];
128 // wrapper around the reference Punycode implementation
129 static int32_t convertToPuny(const UChar
* src
, int32_t srcLength
,
130 UChar
* dest
, int32_t destCapacity
,
132 uint32_t b1Stack
[MAX_LABEL_BUFFER_SIZE
];
133 int32_t b1Len
= 0, b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
134 uint32_t* b1
= b1Stack
;
135 char b2Stack
[MAX_LABEL_BUFFER_SIZE
];
137 int32_t b2Len
=MAX_LABEL_BUFFER_SIZE
;
138 punycode_status error
;
139 unsigned char* caseFlags
= NULL
;
141 u_strToUTF32((UChar32
*)b1
,b1Capacity
,&b1Len
,src
,srcLength
,&status
);
142 if(status
== U_BUFFER_OVERFLOW_ERROR
){
143 // redo processing of string
144 /* we do not have enough room so grow the buffer*/
145 b1
= (uint32_t*) uprv_malloc(b1Len
* sizeof(uint32_t));
147 status
= U_MEMORY_ALLOCATION_ERROR
;
151 status
= U_ZERO_ERROR
; // reset error
153 u_strToUTF32((UChar32
*)b1
,b1Len
,&b1Len
,src
,srcLength
,&status
);
155 if(U_FAILURE(status
)){
159 //caseFlags = (unsigned char*) uprv_malloc(b1Len *sizeof(unsigned char));
161 error
= punycode_encode(b1Len
,b1
,caseFlags
, (uint32_t*)&b2Len
, b2
);
162 status
= getError(error
);
164 if(status
== U_BUFFER_OVERFLOW_ERROR
){
165 /* we do not have enough room so grow the buffer*/
166 b2
= (char*) uprv_malloc( b2Len
* sizeof(char));
168 status
= U_MEMORY_ALLOCATION_ERROR
;
172 status
= U_ZERO_ERROR
; // reset error
174 punycode_status error
= punycode_encode(b1Len
,b1
,caseFlags
, (uint32_t*)&b2Len
, b2
);
175 status
= getError(error
);
177 if(U_FAILURE(status
)){
181 if(b2Len
< destCapacity
){
182 convertASCIIToUChars(b2
,dest
,b2Len
);
184 status
=U_BUFFER_OVERFLOW_ERROR
;
194 uprv_free(caseFlags
);
199 static int32_t convertFromPuny( const UChar
* src
, int32_t srcLength
,
200 UChar
* dest
, int32_t destCapacity
,
202 char b1Stack
[MAX_LABEL_BUFFER_SIZE
];
206 convertUCharsToASCII(src
, b1
,srcLength
);
208 uint32_t b2Stack
[MAX_LABEL_BUFFER_SIZE
];
209 uint32_t* b2
= b2Stack
;
210 int32_t b2Len
=MAX_LABEL_BUFFER_SIZE
;
211 unsigned char* caseFlags
= NULL
; //(unsigned char*) uprv_malloc(srcLength * sizeof(unsigned char*));
212 punycode_status error
= punycode_decode(srcLength
,b1
,(uint32_t*)&b2Len
,b2
,caseFlags
);
213 status
= getError(error
);
214 if(status
== U_BUFFER_OVERFLOW_ERROR
){
215 b2
= (uint32_t*) uprv_malloc(b2Len
* sizeof(uint32_t));
217 status
= U_MEMORY_ALLOCATION_ERROR
;
220 error
= punycode_decode(srcLength
,b1
,(uint32_t*)&b2Len
,b2
,caseFlags
);
221 status
= getError(error
);
224 if(U_FAILURE(status
)){
228 u_strFromUTF32(dest
,destCapacity
,&destLen
,(UChar32
*)b2
,b2Len
,&status
);
237 uprv_free(caseFlags
);
244 U_CFUNC
int32_t U_EXPORT2
245 idnaref_toASCII(const UChar
* src
, int32_t srcLength
,
246 UChar
* dest
, int32_t destCapacity
,
248 UParseError
* parseError
,
251 if(status
== NULL
|| U_FAILURE(*status
)){
254 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
255 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
258 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
], b2Stack
[MAX_LABEL_BUFFER_SIZE
];
259 //initialize pointers to stack buffers
260 UChar
*b1
= b1Stack
, *b2
= b2Stack
;
261 int32_t b1Len
, b2Len
,
262 b1Capacity
= MAX_LABEL_BUFFER_SIZE
,
263 b2Capacity
= MAX_LABEL_BUFFER_SIZE
,
267 UBool allowUnassigned
= (UBool
)((options
& IDNAREF_ALLOW_UNASSIGNED
) != 0);
268 UBool useSTD3ASCIIRules
= (UBool
)((options
& IDNAREF_USE_STD3_RULES
) != 0);
270 UBool
* caseFlags
= NULL
;
272 // assume the source contains all ascii codepoints
273 UBool srcIsASCII
= TRUE
;
274 // assume the source contains all LDH codepoints
275 UBool srcIsLDH
= TRUE
;
277 // UParseError parseError;
279 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
281 if(U_FAILURE(*status
)){
285 b1Len
= prep
->process(src
,srcLength
,b1
, b1Capacity
,allowUnassigned
,parseError
,*status
);
287 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
288 // redo processing of string
289 /* we do not have enough room so grow the buffer*/
290 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
292 *status
= U_MEMORY_ALLOCATION_ERROR
;
296 *status
= U_ZERO_ERROR
; // reset error
298 b1Len
= prep
->process(src
,srcLength
,b1
, b1Len
,allowUnassigned
, parseError
, *status
);
301 if(U_FAILURE(*status
)){
306 for( j
=0;j
<b1Len
;j
++){
309 }else if(prep
->isLDHChar(b1
[j
])==FALSE
){ // if the char is in ASCII range verify that it is an LDH character{
314 if(useSTD3ASCIIRules
== TRUE
){
316 if( srcIsLDH
== FALSE
/* source contains some non-LDH characters */
317 || b1
[0] == HYPHEN
|| b1
[b1Len
-1] == HYPHEN
){
318 *status
= U_IDNA_STD3_ASCII_RULES_ERROR
;
323 if(b1Len
<= destCapacity
){
324 uprv_memmove(dest
, b1
, b1Len
* U_SIZEOF_UCHAR
);
331 // step 5 : verify the sequence does not begin with ACE prefix
332 if(!startsWithPrefix(b1
,b1Len
)){
334 //step 6: encode the sequence with punycode
335 //caseFlags = (UBool*) uprv_malloc(b1Len * sizeof(UBool));
337 b2Len
= convertToPuny(b1
,b1Len
, b2
,b2Capacity
,*status
);
338 //b2Len = u_strToPunycode(b2,b2Capacity,b1,b1Len, caseFlags, status);
339 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
340 // redo processing of string
341 /* we do not have enough room so grow the buffer*/
342 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
344 *status
= U_MEMORY_ALLOCATION_ERROR
;
348 *status
= U_ZERO_ERROR
; // reset error
350 b2Len
= convertToPuny(b1
, b1Len
, b2
, b2Len
, *status
);
351 //b2Len = u_strToPunycode(b2,b2Len,b1,b1Len, caseFlags, status);
355 if(U_FAILURE(*status
)){
358 reqLength
= b2Len
+ACE_PREFIX_LENGTH
;
360 if(reqLength
> destCapacity
){
361 *status
= U_BUFFER_OVERFLOW_ERROR
;
364 //Step 7: prepend the ACE prefix
365 uprv_memcpy(dest
,ACE_PREFIX
,ACE_PREFIX_LENGTH
* U_SIZEOF_UCHAR
);
366 //Step 6: copy the contents in b2 into dest
367 uprv_memcpy(dest
+ACE_PREFIX_LENGTH
, b2
, b2Len
* U_SIZEOF_UCHAR
);
370 *status
= U_IDNA_ACE_PREFIX_ERROR
;
375 if(reqLength
> MAX_LABEL_LENGTH
){
376 *status
= U_IDNA_LABEL_TOO_LONG_ERROR
;
386 uprv_free(caseFlags
);
390 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
394 U_CFUNC
int32_t U_EXPORT2
395 idnaref_toUnicode(const UChar
* src
, int32_t srcLength
,
396 UChar
* dest
, int32_t destCapacity
,
398 UParseError
* parseError
,
401 if(status
== NULL
|| U_FAILURE(*status
)){
404 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
405 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
411 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
], b2Stack
[MAX_LABEL_BUFFER_SIZE
], b3Stack
[MAX_LABEL_BUFFER_SIZE
];
413 //initialize pointers to stack buffers
414 UChar
*b1
= b1Stack
, *b2
= b2Stack
, *b1Prime
=NULL
, *b3
=b3Stack
;
415 int32_t b1Len
, b2Len
, b1PrimeLen
, b3Len
,
416 b1Capacity
= MAX_LABEL_BUFFER_SIZE
,
417 b2Capacity
= MAX_LABEL_BUFFER_SIZE
,
418 b3Capacity
= MAX_LABEL_BUFFER_SIZE
,
420 // UParseError parseError;
422 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
424 UBool
* caseFlags
= NULL
;
427 UBool allowUnassigned
= (UBool
)((options
& IDNAREF_ALLOW_UNASSIGNED
) != 0);
428 UBool useSTD3ASCIIRules
= (UBool
)((options
& IDNAREF_USE_STD3_RULES
) != 0);
430 UBool srcIsASCII
= TRUE
;
431 UBool srcIsLDH
= TRUE
;
434 if(U_FAILURE(*status
)){
437 // step 1: find out if all the codepoints in src are ASCII
440 for(;src
[srcLength
]!=0;){
441 if(src
[srcLength
]> 0x7f){
443 }if(prep
->isLDHChar(src
[srcLength
])==FALSE
){
444 // here we do not assemble surrogates
445 // since we know that LDH code points
446 // are in the ASCII range only
453 for(int32_t j
=0; j
<srcLength
; j
++){
456 }else if(prep
->isLDHChar(src
[j
])==FALSE
){
457 // here we do not assemble surrogates
458 // since we know that LDH code points
459 // are in the ASCII range only
466 if(srcIsASCII
== FALSE
){
467 // step 2: process the string
468 b1Len
= prep
->process(src
,srcLength
,b1
,b1Capacity
,allowUnassigned
, parseError
, *status
);
469 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
470 // redo processing of string
471 /* we do not have enough room so grow the buffer*/
472 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
474 *status
= U_MEMORY_ALLOCATION_ERROR
;
478 *status
= U_ZERO_ERROR
; // reset error
480 b1Len
= prep
->process(src
,srcLength
,b1
, b1Len
,allowUnassigned
, parseError
, *status
);
483 if(U_FAILURE(*status
)){
488 // copy everything to b1
489 if(srcLength
< b1Capacity
){
490 uprv_memmove(b1
,src
, srcLength
* U_SIZEOF_UCHAR
);
492 /* we do not have enough room so grow the buffer*/
493 b1
= (UChar
*) uprv_malloc(srcLength
* U_SIZEOF_UCHAR
);
495 *status
= U_MEMORY_ALLOCATION_ERROR
;
498 uprv_memmove(b1
,src
, srcLength
* U_SIZEOF_UCHAR
);
502 //step 3: verify ACE Prefix
503 if(startsWithPrefix(src
,srcLength
)){
505 //step 4: Remove the ACE Prefix
506 b1Prime
= b1
+ ACE_PREFIX_LENGTH
;
507 b1PrimeLen
= b1Len
- ACE_PREFIX_LENGTH
;
509 //step 5: Decode using punycode
510 b2Len
= convertFromPuny(b1Prime
,b1PrimeLen
, b2
, b2Capacity
, *status
);
511 //b2Len = u_strFromPunycode(b2, b2Capacity,b1Prime,b1PrimeLen, caseFlags, status);
513 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
514 // redo processing of string
515 /* we do not have enough room so grow the buffer*/
516 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
518 *status
= U_MEMORY_ALLOCATION_ERROR
;
522 *status
= U_ZERO_ERROR
; // reset error
524 b2Len
= convertFromPuny(b1Prime
,b1PrimeLen
, b2
, b2Len
, *status
);
525 //b2Len = u_strFromPunycode(b2, b2Len,b1Prime,b1PrimeLen,caseFlags, status);
529 //step 6:Apply toASCII
530 b3Len
= idnaref_toASCII(b2
,b2Len
,b3
,b3Capacity
,options
,parseError
, status
);
532 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
533 // redo processing of string
534 /* we do not have enough room so grow the buffer*/
535 b3
= (UChar
*) uprv_malloc(b3Len
* U_SIZEOF_UCHAR
);
537 *status
= U_MEMORY_ALLOCATION_ERROR
;
541 *status
= U_ZERO_ERROR
; // reset error
543 b3Len
= idnaref_toASCII(b2
,b2Len
,b3
,b3Len
, options
, parseError
, status
);
547 if(U_FAILURE(*status
)){
552 if(compareCaseInsensitiveASCII(b1
, b1Len
, b3
, b3Len
) !=0){
553 *status
= U_IDNA_VERIFICATION_ERROR
;
557 //step 8: return output of step 5
559 if(b2Len
<= destCapacity
) {
560 uprv_memmove(dest
, b2
, b2Len
* U_SIZEOF_UCHAR
);
563 // verify that STD3 ASCII rules are satisfied
564 if(useSTD3ASCIIRules
== TRUE
){
565 if( srcIsLDH
== FALSE
/* source contains some non-LDH characters */
566 || src
[0] == HYPHEN
|| src
[srcLength
-1] == HYPHEN
){
567 *status
= U_IDNA_STD3_ASCII_RULES_ERROR
;
569 /* populate the parseError struct */
571 // failPos is always set the index of failure
572 uprv_syntaxError(src
,failPos
, srcLength
,parseError
);
573 }else if(src
[0] == HYPHEN
){
574 // fail position is 0
575 uprv_syntaxError(src
,0,srcLength
,parseError
);
577 // the last index in the source is always length-1
578 uprv_syntaxError(src
, (srcLength
>0) ? srcLength
-1 : srcLength
, srcLength
,parseError
);
584 //copy the source to destination
585 if(srcLength
<= destCapacity
){
586 uprv_memmove(dest
,src
,srcLength
* U_SIZEOF_UCHAR
);
588 reqLength
= srcLength
;
599 uprv_free(caseFlags
);
603 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
608 getNextSeparator(UChar
*src
,int32_t srcLength
,NamePrepTransform
* prep
,
616 *limit
= src
+ i
; // point to null
620 if(prep
->isLabelSeparator(src
[i
],*status
)){
621 *limit
= src
+ (i
+1); // go past the delimiter
628 for(i
=0;i
<srcLength
;i
++){
629 if(prep
->isLabelSeparator(src
[i
],*status
)){
630 *limit
= src
+ (i
+1); // go past the delimiter
634 // we have not found the delimiter
636 *limit
= src
+srcLength
;
643 U_CFUNC
int32_t U_EXPORT2
644 idnaref_IDNToASCII( const UChar
* src
, int32_t srcLength
,
645 UChar
* dest
, int32_t destCapacity
,
647 UParseError
* parseError
,
650 if(status
== NULL
|| U_FAILURE(*status
)){
653 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
654 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
658 int32_t reqLength
= 0;
659 // UParseError parseError;
661 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
663 //initialize pointers to stack buffers
664 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
];
666 int32_t b1Len
, labelLen
;
667 UChar
* delimiter
= (UChar
*)src
;
668 UChar
* labelStart
= (UChar
*)src
;
669 int32_t remainingLen
= srcLength
;
670 int32_t b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
673 // UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
674 // UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
677 if(U_FAILURE(*status
)){
689 labelLen
= getNextSeparator(labelStart
, -1, prep
, &delimiter
, &done
, status
);
691 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Capacity
,
692 options
, parseError
, status
);
694 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
695 // redo processing of string
696 /* we do not have enough room so grow the buffer*/
697 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
699 *status
= U_MEMORY_ALLOCATION_ERROR
;
703 *status
= U_ZERO_ERROR
; // reset error
705 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Len
,
706 options
, parseError
, status
);
710 if(U_FAILURE(*status
)){
713 int32_t tempLen
= (reqLength
+ b1Len
);
715 if( tempLen
< destCapacity
){
716 uprv_memmove(dest
+reqLength
, b1
, b1Len
* U_SIZEOF_UCHAR
);
721 // add the label separator
723 if(reqLength
< destCapacity
){
724 dest
[reqLength
] = FULL_STOP
;
729 labelStart
= delimiter
;
734 if(delimiter
== src
+srcLength
){
738 labelLen
= getNextSeparator(labelStart
, remainingLen
, prep
, &delimiter
, &done
, status
);
740 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Capacity
,
741 options
,parseError
, status
);
743 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
744 // redo processing of string
745 /* we do not have enough room so grow the buffer*/
746 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
748 *status
= U_MEMORY_ALLOCATION_ERROR
;
752 *status
= U_ZERO_ERROR
; // reset error
754 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Len
,
755 options
, parseError
, status
);
759 if(U_FAILURE(*status
)){
762 int32_t tempLen
= (reqLength
+ b1Len
);
764 if( tempLen
< destCapacity
){
765 uprv_memmove(dest
+reqLength
, b1
, b1Len
* U_SIZEOF_UCHAR
);
770 // add the label separator
772 if(reqLength
< destCapacity
){
773 dest
[reqLength
] = FULL_STOP
;
778 labelStart
= delimiter
;
779 remainingLen
= srcLength
- (delimiter
- src
);
792 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
795 U_CFUNC
int32_t U_EXPORT2
796 idnaref_IDNToUnicode( const UChar
* src
, int32_t srcLength
,
797 UChar
* dest
, int32_t destCapacity
,
799 UParseError
* parseError
,
802 if(status
== NULL
|| U_FAILURE(*status
)){
805 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
806 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
810 int32_t reqLength
= 0;
814 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
816 //initialize pointers to stack buffers
817 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
];
819 int32_t b1Len
, labelLen
;
820 UChar
* delimiter
= (UChar
*)src
;
821 UChar
* labelStart
= (UChar
*)src
;
822 int32_t remainingLen
= srcLength
;
823 int32_t b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
826 // UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
827 // UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
829 if(U_FAILURE(*status
)){
840 labelLen
= getNextSeparator(labelStart
, -1, prep
, &delimiter
, &done
, status
);
842 b1Len
= idnaref_toUnicode(labelStart
, labelLen
, b1
, b1Capacity
,
843 options
, parseError
, status
);
845 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
846 // redo processing of string
847 /* we do not have enough room so grow the buffer*/
848 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
850 *status
= U_MEMORY_ALLOCATION_ERROR
;
854 *status
= U_ZERO_ERROR
; // reset error
856 b1Len
= idnaref_toUnicode( labelStart
, labelLen
, b1
, b1Len
,
857 options
, parseError
, status
);
861 if(U_FAILURE(*status
)){
864 int32_t tempLen
= (reqLength
+ b1Len
);
866 if( tempLen
< destCapacity
){
867 uprv_memmove(dest
+reqLength
, b1
, b1Len
* U_SIZEOF_UCHAR
);
871 // add the label separator
873 if(reqLength
< destCapacity
){
874 dest
[reqLength
] = FULL_STOP
;
879 labelStart
= delimiter
;
884 if(delimiter
== src
+srcLength
){
888 labelLen
= getNextSeparator(labelStart
, remainingLen
, prep
, &delimiter
, &done
, status
);
890 b1Len
= idnaref_toUnicode( labelStart
,labelLen
, b1
, b1Capacity
,
891 options
, parseError
, status
);
893 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
894 // redo processing of string
895 /* we do not have enough room so grow the buffer*/
896 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
898 *status
= U_MEMORY_ALLOCATION_ERROR
;
902 *status
= U_ZERO_ERROR
; // reset error
904 b1Len
= idnaref_toUnicode( labelStart
, labelLen
, b1
, b1Len
,
905 options
, parseError
, status
);
909 if(U_FAILURE(*status
)){
912 int32_t tempLen
= (reqLength
+ b1Len
);
914 if( tempLen
< destCapacity
){
915 uprv_memmove(dest
+reqLength
, b1
, b1Len
* U_SIZEOF_UCHAR
);
920 // add the label separator
922 if(reqLength
< destCapacity
){
923 dest
[reqLength
] = FULL_STOP
;
928 labelStart
= delimiter
;
929 remainingLen
= srcLength
- (delimiter
- src
);
941 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
944 U_CFUNC
int32_t U_EXPORT2
945 idnaref_compare( const UChar
*s1
, int32_t length1
,
946 const UChar
*s2
, int32_t length2
,
950 if(status
== NULL
|| U_FAILURE(*status
)){
954 UChar b1Stack
[MAX_IDN_BUFFER_SIZE
], b2Stack
[MAX_IDN_BUFFER_SIZE
];
955 UChar
*b1
= b1Stack
, *b2
= b2Stack
;
956 int32_t b1Len
, b2Len
, b1Capacity
= MAX_IDN_BUFFER_SIZE
, b2Capacity
= MAX_IDN_BUFFER_SIZE
;
959 UParseError parseError
;
961 b1Len
= idnaref_IDNToASCII(s1
, length1
, b1
, b1Capacity
, options
, &parseError
, status
);
962 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
963 // redo processing of string
964 /* we do not have enough room so grow the buffer*/
965 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
967 *status
= U_MEMORY_ALLOCATION_ERROR
;
971 *status
= U_ZERO_ERROR
; // reset error
973 b1Len
= idnaref_IDNToASCII(s1
,length1
,b1
,b1Len
, options
, &parseError
, status
);
977 b2Len
= idnaref_IDNToASCII(s2
,length2
,b2
,b2Capacity
,options
, &parseError
, status
);
978 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
979 // redo processing of string
980 /* we do not have enough room so grow the buffer*/
981 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
983 *status
= U_MEMORY_ALLOCATION_ERROR
;
987 *status
= U_ZERO_ERROR
; // reset error
989 b2Len
= idnaref_IDNToASCII(s2
,length2
,b2
,b2Len
,options
, &parseError
, status
);
992 // when toASCII is applied all label separators are replaced with FULL_STOP
993 result
= compareCaseInsensitiveASCII(b1
,b1Len
,b2
,b2Len
);
1006 #endif /* #if !UCONFIG_NO_IDNA */