1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2003-2011, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: idnaref.cpp
12 * tab size: 8 (not used)
15 * created on: 2003feb1
16 * created by: Ram Viswanadha
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
30 #include "unicode/ustring.h"
32 /* it is official IDNA ACE Prefix is "xn--" */
33 static const UChar ACE_PREFIX
[] ={ 0x0078,0x006E,0x002d,0x002d } ;
34 #define ACE_PREFIX_LENGTH 4
36 #define MAX_LABEL_LENGTH 63
38 /* The Max length of the labels should not be more than 64 */
39 #define MAX_LABEL_BUFFER_SIZE 100
40 #define MAX_IDN_BUFFER_SIZE 300
42 #define CAPITAL_A 0x0041
43 #define CAPITAL_Z 0x005A
44 #define LOWER_CASE_DELTA 0x0020
45 #define FULL_STOP 0x002E
49 startsWithPrefix(const UChar
* src
, int32_t srcLength
){
50 UBool startsWithPrefix
= TRUE
;
52 if(srcLength
< ACE_PREFIX_LENGTH
){
56 for(int8_t i
=0; i
< ACE_PREFIX_LENGTH
; i
++){
57 if(u_tolower(src
[i
]) != ACE_PREFIX
[i
]){
58 startsWithPrefix
= FALSE
;
61 return startsWithPrefix
;
65 toASCIILower(UChar ch
){
66 if(CAPITAL_A
<= ch
&& ch
<= CAPITAL_Z
){
67 return ch
+ LOWER_CASE_DELTA
;
73 compareCaseInsensitiveASCII(const UChar
* s1
, int32_t s1Len
,
74 const UChar
* s2
, int32_t s2Len
){
76 return (s1Len
> s2Len
) ? s1Len
: s2Len
;
81 for(int32_t i
=0;/* no condition */;i
++) {
82 /* If we reach the ends of both strings then they match */
90 /* Case-insensitive comparison */
92 rc
=(int32_t)toASCIILower(c1
)-(int32_t)toASCIILower(c2
);
101 static UErrorCode
getError(enum punycode_status status
){
103 case punycode_success
:
105 case punycode_bad_input
: /* Input is invalid. */
106 return U_INVALID_CHAR_FOUND
;
107 case punycode_big_output
: /* Output would exceed the space provided. */
108 return U_BUFFER_OVERFLOW_ERROR
;
109 case punycode_overflow
: /* Input requires wider integers to process. */
110 return U_INDEX_OUTOFBOUNDS_ERROR
;
112 return U_INTERNAL_PROGRAM_ERROR
;
116 static inline int32_t convertASCIIToUChars(const char* src
,UChar
* dest
, int32_t length
){
118 for(i
=0;i
<length
;i
++){
123 static inline int32_t convertUCharsToASCII(const UChar
* src
,char* dest
, int32_t length
){
125 for(i
=0;i
<length
;i
++){
126 dest
[i
] = (char)src
[i
];
130 // wrapper around the reference Punycode implementation
131 static int32_t convertToPuny(const UChar
* src
, int32_t srcLength
,
132 UChar
* dest
, int32_t destCapacity
,
134 uint32_t b1Stack
[MAX_LABEL_BUFFER_SIZE
];
135 int32_t b1Len
= 0, b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
136 uint32_t* b1
= b1Stack
;
137 char b2Stack
[MAX_LABEL_BUFFER_SIZE
];
139 int32_t b2Len
=MAX_LABEL_BUFFER_SIZE
;
140 punycode_status error
;
141 unsigned char* caseFlags
= NULL
;
143 u_strToUTF32((UChar32
*)b1
,b1Capacity
,&b1Len
,src
,srcLength
,&status
);
144 if(status
== U_BUFFER_OVERFLOW_ERROR
){
145 // redo processing of string
146 /* we do not have enough room so grow the buffer*/
147 b1
= (uint32_t*) uprv_malloc(b1Len
* sizeof(uint32_t));
149 status
= U_MEMORY_ALLOCATION_ERROR
;
153 status
= U_ZERO_ERROR
; // reset error
155 u_strToUTF32((UChar32
*)b1
,b1Len
,&b1Len
,src
,srcLength
,&status
);
157 if(U_FAILURE(status
)){
161 //caseFlags = (unsigned char*) uprv_malloc(b1Len *sizeof(unsigned char));
163 error
= punycode_encode(b1Len
,b1
,caseFlags
, (uint32_t*)&b2Len
, b2
);
164 status
= getError(error
);
166 if(status
== U_BUFFER_OVERFLOW_ERROR
){
167 /* we do not have enough room so grow the buffer*/
168 b2
= (char*) uprv_malloc( b2Len
* sizeof(char));
170 status
= U_MEMORY_ALLOCATION_ERROR
;
174 status
= U_ZERO_ERROR
; // reset error
176 punycode_status error
= punycode_encode(b1Len
,b1
,caseFlags
, (uint32_t*)&b2Len
, b2
);
177 status
= getError(error
);
179 if(U_FAILURE(status
)){
183 if(b2Len
< destCapacity
){
184 convertASCIIToUChars(b2
,dest
,b2Len
);
186 status
=U_BUFFER_OVERFLOW_ERROR
;
196 uprv_free(caseFlags
);
201 static int32_t convertFromPuny( const UChar
* src
, int32_t srcLength
,
202 UChar
* dest
, int32_t destCapacity
,
204 char b1Stack
[MAX_LABEL_BUFFER_SIZE
];
208 convertUCharsToASCII(src
, b1
,srcLength
);
210 uint32_t b2Stack
[MAX_LABEL_BUFFER_SIZE
];
211 uint32_t* b2
= b2Stack
;
212 int32_t b2Len
=MAX_LABEL_BUFFER_SIZE
;
213 unsigned char* caseFlags
= NULL
; //(unsigned char*) uprv_malloc(srcLength * sizeof(unsigned char*));
214 punycode_status error
= punycode_decode(srcLength
,b1
,(uint32_t*)&b2Len
,b2
,caseFlags
);
215 status
= getError(error
);
216 if(status
== U_BUFFER_OVERFLOW_ERROR
){
217 b2
= (uint32_t*) uprv_malloc(b2Len
* sizeof(uint32_t));
219 status
= U_MEMORY_ALLOCATION_ERROR
;
222 error
= punycode_decode(srcLength
,b1
,(uint32_t*)&b2Len
,b2
,caseFlags
);
223 status
= getError(error
);
226 if(U_FAILURE(status
)){
230 u_strFromUTF32(dest
,destCapacity
,&destLen
,(UChar32
*)b2
,b2Len
,&status
);
239 uprv_free(caseFlags
);
245 U_CFUNC
int32_t U_EXPORT2
246 idnaref_toASCII(const UChar
* src
, int32_t srcLength
,
247 UChar
* dest
, int32_t destCapacity
,
249 UParseError
* parseError
,
252 if(status
== NULL
|| U_FAILURE(*status
)){
255 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
256 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
259 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
], b2Stack
[MAX_LABEL_BUFFER_SIZE
];
260 //initialize pointers to stack buffers
261 UChar
*b1
= b1Stack
, *b2
= b2Stack
;
262 int32_t b1Len
=0, b2Len
=0,
263 b1Capacity
= MAX_LABEL_BUFFER_SIZE
,
264 b2Capacity
= MAX_LABEL_BUFFER_SIZE
,
268 UBool allowUnassigned
= (UBool
)((options
& IDNAREF_ALLOW_UNASSIGNED
) != 0);
269 UBool useSTD3ASCIIRules
= (UBool
)((options
& IDNAREF_USE_STD3_RULES
) != 0);
271 UBool
* caseFlags
= NULL
;
273 // assume the source contains all ascii codepoints
274 UBool srcIsASCII
= TRUE
;
275 // assume the source contains all LDH codepoints
276 UBool srcIsLDH
= TRUE
;
280 srcLength
= u_strlen(src
);
284 for( j
=0;j
<srcLength
;j
++){
288 b1
[b1Len
++] = src
[j
];
291 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
292 if(U_FAILURE(*status
)){
296 // step 2 is performed only if the source contains non ASCII
298 b1Len
= prep
->process(src
,srcLength
,b1
, b1Capacity
,allowUnassigned
,parseError
,*status
);
300 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
301 // redo processing of string
302 /* we do not have enough room so grow the buffer*/
303 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
305 *status
= U_MEMORY_ALLOCATION_ERROR
;
309 *status
= U_ZERO_ERROR
; // reset error
311 b1Len
= prep
->process(src
,srcLength
,b1
, b1Len
,allowUnassigned
, parseError
, *status
);
314 if(U_FAILURE(*status
)){
320 *status
= U_IDNA_ZERO_LENGTH_LABEL_ERROR
;
326 for( j
=0;j
<b1Len
;j
++){
327 if(b1
[j
] > 0x7F){// check if output of usprep_prepare is all ASCII
329 }else if(prep
->isLDHChar(b1
[j
])==FALSE
){ // if the char is in ASCII range verify that it is an LDH character{
334 if(useSTD3ASCIIRules
== TRUE
){
336 if( srcIsLDH
== FALSE
/* source contains some non-LDH characters */
337 || b1
[0] == HYPHEN
|| b1
[b1Len
-1] == HYPHEN
){
338 *status
= U_IDNA_STD3_ASCII_RULES_ERROR
;
343 if(b1Len
<= destCapacity
){
344 u_memmove(dest
, b1
, b1Len
);
351 // step 5 : verify the sequence does not begin with ACE prefix
352 if(!startsWithPrefix(b1
,b1Len
)){
354 //step 6: encode the sequence with punycode
355 //caseFlags = (UBool*) uprv_malloc(b1Len * sizeof(UBool));
357 b2Len
= convertToPuny(b1
,b1Len
, b2
,b2Capacity
,*status
);
358 //b2Len = u_strToPunycode(b2,b2Capacity,b1,b1Len, caseFlags, status);
359 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
360 // redo processing of string
361 /* we do not have enough room so grow the buffer*/
362 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
364 *status
= U_MEMORY_ALLOCATION_ERROR
;
368 *status
= U_ZERO_ERROR
; // reset error
370 b2Len
= convertToPuny(b1
, b1Len
, b2
, b2Len
, *status
);
371 //b2Len = u_strToPunycode(b2,b2Len,b1,b1Len, caseFlags, status);
375 if(U_FAILURE(*status
)){
378 reqLength
= b2Len
+ACE_PREFIX_LENGTH
;
380 if(reqLength
> destCapacity
){
381 *status
= U_BUFFER_OVERFLOW_ERROR
;
384 //Step 7: prepend the ACE prefix
385 u_memcpy(dest
, ACE_PREFIX
, ACE_PREFIX_LENGTH
);
386 //Step 6: copy the contents in b2 into dest
387 u_memcpy(dest
+ACE_PREFIX_LENGTH
, b2
, b2Len
);
390 *status
= U_IDNA_ACE_PREFIX_ERROR
;
395 if(reqLength
> MAX_LABEL_LENGTH
){
396 *status
= U_IDNA_LABEL_TOO_LONG_ERROR
;
406 uprv_free(caseFlags
);
410 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
414 U_CFUNC
int32_t U_EXPORT2
415 idnaref_toUnicode(const UChar
* src
, int32_t srcLength
,
416 UChar
* dest
, int32_t destCapacity
,
418 UParseError
* parseError
,
421 if(status
== NULL
|| U_FAILURE(*status
)){
424 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
425 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
431 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
], b2Stack
[MAX_LABEL_BUFFER_SIZE
], b3Stack
[MAX_LABEL_BUFFER_SIZE
];
433 //initialize pointers to stack buffers
434 UChar
*b1
= b1Stack
, *b2
= b2Stack
, *b1Prime
=NULL
, *b3
=b3Stack
;
435 int32_t b1Len
, b2Len
, b1PrimeLen
, b3Len
,
436 b1Capacity
= MAX_LABEL_BUFFER_SIZE
,
437 b2Capacity
= MAX_LABEL_BUFFER_SIZE
,
438 b3Capacity
= MAX_LABEL_BUFFER_SIZE
,
440 // UParseError parseError;
442 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
444 UBool
* caseFlags
= NULL
;
447 UBool allowUnassigned
= (UBool
)((options
& IDNAREF_ALLOW_UNASSIGNED
) != 0);
448 UBool useSTD3ASCIIRules
= (UBool
)((options
& IDNAREF_USE_STD3_RULES
) != 0);
450 UBool srcIsASCII
= TRUE
;
451 UBool srcIsLDH
= TRUE
;
454 if(U_FAILURE(*status
)){
457 // step 1: find out if all the codepoints in src are ASCII
460 for(;src
[srcLength
]!=0;){
461 if(src
[srcLength
]> 0x7f){
463 }if(prep
->isLDHChar(src
[srcLength
])==FALSE
){
464 // here we do not assemble surrogates
465 // since we know that LDH code points
466 // are in the ASCII range only
473 for(int32_t j
=0; j
<srcLength
; j
++){
476 }else if(prep
->isLDHChar(src
[j
])==FALSE
){
477 // here we do not assemble surrogates
478 // since we know that LDH code points
479 // are in the ASCII range only
486 if(srcIsASCII
== FALSE
){
487 // step 2: process the string
488 b1Len
= prep
->process(src
,srcLength
,b1
,b1Capacity
,allowUnassigned
, parseError
, *status
);
489 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
490 // redo processing of string
491 /* we do not have enough room so grow the buffer*/
492 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
494 *status
= U_MEMORY_ALLOCATION_ERROR
;
498 *status
= U_ZERO_ERROR
; // reset error
500 b1Len
= prep
->process(src
,srcLength
,b1
, b1Len
,allowUnassigned
, parseError
, *status
);
503 if(U_FAILURE(*status
)){
508 // copy everything to b1
509 if(srcLength
< b1Capacity
){
510 u_memmove(b1
, src
, srcLength
);
512 /* we do not have enough room so grow the buffer*/
513 b1
= (UChar
*) uprv_malloc(srcLength
* U_SIZEOF_UCHAR
);
515 *status
= U_MEMORY_ALLOCATION_ERROR
;
518 u_memmove(b1
, src
, srcLength
);
522 //step 3: verify ACE Prefix
523 if(startsWithPrefix(src
,srcLength
)){
525 //step 4: Remove the ACE Prefix
526 b1Prime
= b1
+ ACE_PREFIX_LENGTH
;
527 b1PrimeLen
= b1Len
- ACE_PREFIX_LENGTH
;
529 //step 5: Decode using punycode
530 b2Len
= convertFromPuny(b1Prime
,b1PrimeLen
, b2
, b2Capacity
, *status
);
531 //b2Len = u_strFromPunycode(b2, b2Capacity,b1Prime,b1PrimeLen, caseFlags, status);
533 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
534 // redo processing of string
535 /* we do not have enough room so grow the buffer*/
536 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
538 *status
= U_MEMORY_ALLOCATION_ERROR
;
542 *status
= U_ZERO_ERROR
; // reset error
544 b2Len
= convertFromPuny(b1Prime
,b1PrimeLen
, b2
, b2Len
, *status
);
545 //b2Len = u_strFromPunycode(b2, b2Len,b1Prime,b1PrimeLen,caseFlags, status);
549 //step 6:Apply toASCII
550 b3Len
= idnaref_toASCII(b2
,b2Len
,b3
,b3Capacity
,options
,parseError
, status
);
552 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
553 // redo processing of string
554 /* we do not have enough room so grow the buffer*/
555 b3
= (UChar
*) uprv_malloc(b3Len
* U_SIZEOF_UCHAR
);
557 *status
= U_MEMORY_ALLOCATION_ERROR
;
561 *status
= U_ZERO_ERROR
; // reset error
563 b3Len
= idnaref_toASCII(b2
,b2Len
,b3
,b3Len
, options
, parseError
, status
);
567 if(U_FAILURE(*status
)){
572 if(compareCaseInsensitiveASCII(b1
, b1Len
, b3
, b3Len
) !=0){
573 *status
= U_IDNA_VERIFICATION_ERROR
;
577 //step 8: return output of step 5
579 if(b2Len
<= destCapacity
) {
580 u_memmove(dest
, b2
, b2Len
);
583 // verify that STD3 ASCII rules are satisfied
584 if(useSTD3ASCIIRules
== TRUE
){
585 if( srcIsLDH
== FALSE
/* source contains some non-LDH characters */
586 || src
[0] == HYPHEN
|| src
[srcLength
-1] == HYPHEN
){
587 *status
= U_IDNA_STD3_ASCII_RULES_ERROR
;
589 /* populate the parseError struct */
591 // failPos is always set the index of failure
592 uprv_syntaxError(src
,failPos
, srcLength
,parseError
);
593 }else if(src
[0] == HYPHEN
){
594 // fail position is 0
595 uprv_syntaxError(src
,0,srcLength
,parseError
);
597 // the last index in the source is always length-1
598 uprv_syntaxError(src
, (srcLength
>0) ? srcLength
-1 : srcLength
, srcLength
,parseError
);
604 //copy the source to destination
605 if(srcLength
<= destCapacity
){
606 u_memmove(dest
, src
, srcLength
);
608 reqLength
= srcLength
;
619 uprv_free(caseFlags
);
621 // The RFC states that
623 // ToUnicode never fails. If any step fails, then the original input
624 // is returned immediately in that step.
626 // So if any step fails lets copy source to destination
627 if(U_FAILURE(*status
)){
628 //copy the source to destination
629 if(dest
&& srcLength
<= destCapacity
){
630 if(srcLength
== -1) {
631 u_memmove(dest
, src
, u_strlen(src
));
633 u_memmove(dest
, src
, srcLength
);
636 reqLength
= srcLength
;
637 *status
= U_ZERO_ERROR
;
639 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
644 getNextSeparator(UChar
*src
,int32_t srcLength
,NamePrepTransform
* prep
,
652 *limit
= src
+ i
; // point to null
656 if(prep
->isLabelSeparator(src
[i
],*status
)){
657 *limit
= src
+ (i
+1); // go past the delimiter
664 for(i
=0;i
<srcLength
;i
++){
665 if(prep
->isLabelSeparator(src
[i
],*status
)){
666 *limit
= src
+ (i
+1); // go past the delimiter
670 // we have not found the delimiter
672 *limit
= src
+srcLength
;
679 U_CFUNC
int32_t U_EXPORT2
680 idnaref_IDNToASCII( const UChar
* src
, int32_t srcLength
,
681 UChar
* dest
, int32_t destCapacity
,
683 UParseError
* parseError
,
686 if(status
== NULL
|| U_FAILURE(*status
)){
689 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
690 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
694 int32_t reqLength
= 0;
695 // UParseError parseError;
697 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
699 //initialize pointers to stack buffers
700 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
];
702 int32_t b1Len
, labelLen
;
703 UChar
* delimiter
= (UChar
*)src
;
704 UChar
* labelStart
= (UChar
*)src
;
705 int32_t remainingLen
= srcLength
;
706 int32_t b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
709 // UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
710 // UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
713 if(U_FAILURE(*status
)){
725 labelLen
= getNextSeparator(labelStart
, -1, prep
, &delimiter
, &done
, status
);
727 if(!(labelLen
==0 && done
)){// make sure this is not a root label separator.
729 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Capacity
,
730 options
, parseError
, status
);
732 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
733 // redo processing of string
734 /* we do not have enough room so grow the buffer*/
735 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
737 *status
= U_MEMORY_ALLOCATION_ERROR
;
741 *status
= U_ZERO_ERROR
; // reset error
743 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Len
,
744 options
, parseError
, status
);
749 if(U_FAILURE(*status
)){
752 int32_t tempLen
= (reqLength
+ b1Len
);
754 if( tempLen
< destCapacity
){
755 u_memmove(dest
+reqLength
, b1
, b1Len
);
760 // add the label separator
762 if(reqLength
< destCapacity
){
763 dest
[reqLength
] = FULL_STOP
;
768 labelStart
= delimiter
;
773 if(delimiter
== src
+srcLength
){
777 labelLen
= getNextSeparator(labelStart
, remainingLen
, prep
, &delimiter
, &done
, status
);
779 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Capacity
,
780 options
,parseError
, status
);
782 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
783 // redo processing of string
784 /* we do not have enough room so grow the buffer*/
785 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
787 *status
= U_MEMORY_ALLOCATION_ERROR
;
791 *status
= U_ZERO_ERROR
; // reset error
793 b1Len
= idnaref_toASCII(labelStart
, labelLen
, b1
, b1Len
,
794 options
, parseError
, status
);
798 if(U_FAILURE(*status
)){
801 int32_t tempLen
= (reqLength
+ b1Len
);
803 if( tempLen
< destCapacity
){
804 u_memmove(dest
+reqLength
, b1
, b1Len
);
809 // add the label separator
811 if(reqLength
< destCapacity
){
812 dest
[reqLength
] = FULL_STOP
;
817 labelStart
= delimiter
;
818 remainingLen
= srcLength
- (delimiter
- src
);
831 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
834 U_CFUNC
int32_t U_EXPORT2
835 idnaref_IDNToUnicode( const UChar
* src
, int32_t srcLength
,
836 UChar
* dest
, int32_t destCapacity
,
838 UParseError
* parseError
,
841 if(status
== NULL
|| U_FAILURE(*status
)){
844 if((src
== NULL
) || (srcLength
< -1) || (destCapacity
<0) || (!dest
&& destCapacity
> 0)){
845 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
849 int32_t reqLength
= 0;
853 NamePrepTransform
* prep
= TestIDNA::getInstance(*status
);
855 //initialize pointers to stack buffers
856 UChar b1Stack
[MAX_LABEL_BUFFER_SIZE
];
858 int32_t b1Len
, labelLen
;
859 UChar
* delimiter
= (UChar
*)src
;
860 UChar
* labelStart
= (UChar
*)src
;
861 int32_t remainingLen
= srcLength
;
862 int32_t b1Capacity
= MAX_LABEL_BUFFER_SIZE
;
865 // UBool allowUnassigned = (UBool)((options & IDNAREF_ALLOW_UNASSIGNED) != 0);
866 // UBool useSTD3ASCIIRules = (UBool)((options & IDNAREF_USE_STD3_RULES) != 0);
868 if(U_FAILURE(*status
)){
879 labelLen
= getNextSeparator(labelStart
, -1, prep
, &delimiter
, &done
, status
);
881 if(labelLen
==0 && done
==FALSE
){
882 *status
= U_IDNA_ZERO_LENGTH_LABEL_ERROR
;
884 b1Len
= idnaref_toUnicode(labelStart
, labelLen
, b1
, b1Capacity
,
885 options
, parseError
, status
);
887 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
888 // redo processing of string
889 /* we do not have enough room so grow the buffer*/
890 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
892 *status
= U_MEMORY_ALLOCATION_ERROR
;
896 *status
= U_ZERO_ERROR
; // reset error
898 b1Len
= idnaref_toUnicode( labelStart
, labelLen
, b1
, b1Len
,
899 options
, parseError
, status
);
903 if(U_FAILURE(*status
)){
906 int32_t tempLen
= (reqLength
+ b1Len
);
908 if( tempLen
< destCapacity
){
909 u_memmove(dest
+reqLength
, b1
, b1Len
);
913 // add the label separator
915 if(reqLength
< destCapacity
){
916 dest
[reqLength
] = FULL_STOP
;
921 labelStart
= delimiter
;
926 if(delimiter
== src
+srcLength
){
930 labelLen
= getNextSeparator(labelStart
, remainingLen
, prep
, &delimiter
, &done
, status
);
932 if(labelLen
==0 && done
==FALSE
){
933 *status
= U_IDNA_ZERO_LENGTH_LABEL_ERROR
;
936 b1Len
= idnaref_toUnicode( labelStart
,labelLen
, b1
, b1Capacity
,
937 options
, parseError
, status
);
939 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
940 // redo processing of string
941 /* we do not have enough room so grow the buffer*/
942 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
944 *status
= U_MEMORY_ALLOCATION_ERROR
;
948 *status
= U_ZERO_ERROR
; // reset error
950 b1Len
= idnaref_toUnicode( labelStart
, labelLen
, b1
, b1Len
,
951 options
, parseError
, status
);
955 if(U_FAILURE(*status
)){
958 int32_t tempLen
= (reqLength
+ b1Len
);
960 if( tempLen
< destCapacity
){
961 u_memmove(dest
+reqLength
, b1
, b1Len
);
966 // add the label separator
968 if(reqLength
< destCapacity
){
969 dest
[reqLength
] = FULL_STOP
;
974 labelStart
= delimiter
;
975 remainingLen
= srcLength
- (delimiter
- src
);
987 return u_terminateUChars(dest
, destCapacity
, reqLength
, status
);
990 U_CFUNC
int32_t U_EXPORT2
991 idnaref_compare( const UChar
*s1
, int32_t length1
,
992 const UChar
*s2
, int32_t length2
,
996 if(status
== NULL
|| U_FAILURE(*status
)){
1000 UChar b1Stack
[MAX_IDN_BUFFER_SIZE
], b2Stack
[MAX_IDN_BUFFER_SIZE
];
1001 UChar
*b1
= b1Stack
, *b2
= b2Stack
;
1002 int32_t b1Len
, b2Len
, b1Capacity
= MAX_IDN_BUFFER_SIZE
, b2Capacity
= MAX_IDN_BUFFER_SIZE
;
1003 int32_t result
= -1;
1005 UParseError parseError
;
1007 b1Len
= idnaref_IDNToASCII(s1
, length1
, b1
, b1Capacity
, options
, &parseError
, status
);
1008 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
1009 // redo processing of string
1010 /* we do not have enough room so grow the buffer*/
1011 b1
= (UChar
*) uprv_malloc(b1Len
* U_SIZEOF_UCHAR
);
1013 *status
= U_MEMORY_ALLOCATION_ERROR
;
1017 *status
= U_ZERO_ERROR
; // reset error
1019 b1Len
= idnaref_IDNToASCII(s1
,length1
,b1
,b1Len
, options
, &parseError
, status
);
1023 b2Len
= idnaref_IDNToASCII(s2
,length2
,b2
,b2Capacity
,options
, &parseError
, status
);
1024 if(*status
== U_BUFFER_OVERFLOW_ERROR
){
1025 // redo processing of string
1026 /* we do not have enough room so grow the buffer*/
1027 b2
= (UChar
*) uprv_malloc(b2Len
* U_SIZEOF_UCHAR
);
1029 *status
= U_MEMORY_ALLOCATION_ERROR
;
1033 *status
= U_ZERO_ERROR
; // reset error
1035 b2Len
= idnaref_IDNToASCII(s2
,length2
,b2
,b2Len
,options
, &parseError
, status
);
1038 // when toASCII is applied all label separators are replaced with FULL_STOP
1039 result
= compareCaseInsensitiveASCII(b1
,b1Len
,b2
,b2Len
);
1052 #endif /* #if !UCONFIG_NO_IDNA */