2 *******************************************************************************
3 * Copyright (C) 2010-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 * tab size: 8 (not used)
11 * created on: 2010mar09
12 * created by: Markus W. Scherer
15 #include "unicode/utypes.h"
19 #include "unicode/idna.h"
20 #include "unicode/normalizer2.h"
21 #include "unicode/uscript.h"
22 #include "unicode/ustring.h"
23 #include "unicode/utf16.h"
27 #include "ubidi_props.h"
30 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
32 // Note about tests for UIDNA_ERROR_DOMAIN_NAME_TOO_LONG:
34 // The domain name length limit is 255 octets in an internal DNS representation
35 // where the last ("root") label is the empty label
36 // represented by length byte 0 alone.
37 // In a conventional string, this translates to 253 characters, or 254
38 // if there is a trailing dot for the root label.
42 // Severe errors which usually result in a U+FFFD replacement character in the result string.
43 const uint32_t severeErrors
=
44 UIDNA_ERROR_LEADING_COMBINING_MARK
|
45 UIDNA_ERROR_DISALLOWED
|
47 UIDNA_ERROR_LABEL_HAS_DOT
|
48 UIDNA_ERROR_INVALID_ACE_LABEL
;
51 isASCIIString(const UnicodeString
&dest
) {
52 const UChar
*s
=dest
.getBuffer();
53 const UChar
*limit
=s
+dest
.length();
63 isASCIIOkBiDi(const UChar
*s
, int32_t length
);
66 isASCIIOkBiDi(const char *s
, int32_t length
);
68 // IDNA class default implementations -------------------------------------- ***
73 IDNA::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
74 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
75 if(U_SUCCESS(errorCode
)) {
76 UnicodeString destString
;
77 labelToASCII(UnicodeString::fromUTF8(label
), destString
,
78 info
, errorCode
).toUTF8(dest
);
83 IDNA::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
84 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
85 if(U_SUCCESS(errorCode
)) {
86 UnicodeString destString
;
87 labelToUnicode(UnicodeString::fromUTF8(label
), destString
,
88 info
, errorCode
).toUTF8(dest
);
93 IDNA::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
94 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
95 if(U_SUCCESS(errorCode
)) {
96 UnicodeString destString
;
97 nameToASCII(UnicodeString::fromUTF8(name
), destString
,
98 info
, errorCode
).toUTF8(dest
);
103 IDNA::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
104 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
105 if(U_SUCCESS(errorCode
)) {
106 UnicodeString destString
;
107 nameToUnicode(UnicodeString::fromUTF8(name
), destString
,
108 info
, errorCode
).toUTF8(dest
);
112 // UTS46 class declaration ------------------------------------------------- ***
114 class UTS46
: public IDNA
{
116 UTS46(uint32_t options
, UErrorCode
&errorCode
);
119 virtual UnicodeString
&
120 labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
121 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
123 virtual UnicodeString
&
124 labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
125 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
127 virtual UnicodeString
&
128 nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
129 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
131 virtual UnicodeString
&
132 nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
133 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
136 labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
137 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
140 labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
141 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
144 nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
145 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
148 nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
149 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
153 process(const UnicodeString
&src
,
154 UBool isLabel
, UBool toASCII
,
156 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
159 processUTF8(const StringPiece
&src
,
160 UBool isLabel
, UBool toASCII
,
162 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
165 processUnicode(const UnicodeString
&src
,
166 int32_t labelStart
, int32_t mappingStart
,
167 UBool isLabel
, UBool toASCII
,
169 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
171 // returns the new dest.length()
173 mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
174 UErrorCode
&errorCode
) const;
176 // returns the new label length
178 processLabel(UnicodeString
&dest
,
179 int32_t labelStart
, int32_t labelLength
,
181 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
183 markBadACELabel(UnicodeString
&dest
,
184 int32_t labelStart
, int32_t labelLength
,
185 UBool toASCII
, IDNAInfo
&info
) const;
188 checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const;
191 isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const;
194 checkLabelContextO(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const;
196 const Normalizer2
&uts46Norm2
; // uts46.nrm
201 IDNA::createUTS46Instance(uint32_t options
, UErrorCode
&errorCode
) {
202 if(U_SUCCESS(errorCode
)) {
203 IDNA
*idna
=new UTS46(options
, errorCode
);
205 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
206 } else if(U_FAILURE(errorCode
)) {
216 // UTS46 implementation ---------------------------------------------------- ***
218 UTS46::UTS46(uint32_t opt
, UErrorCode
&errorCode
)
219 : uts46Norm2(*Normalizer2::getInstance(NULL
, "uts46", UNORM2_COMPOSE
, errorCode
)),
225 UTS46::labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
226 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
227 return process(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
231 UTS46::labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
232 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
233 return process(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
237 UTS46::nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
238 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
239 process(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
240 if( dest
.length()>=254 && (info
.errors
&UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
)==0 &&
241 isASCIIString(dest
) &&
242 (dest
.length()>254 || dest
[253]!=0x2e)
244 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
250 UTS46::nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
251 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
252 return process(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
256 UTS46::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
257 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
258 processUTF8(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
262 UTS46::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
263 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
264 processUTF8(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
268 UTS46::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
269 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
270 processUTF8(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
274 UTS46::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
275 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
276 processUTF8(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
279 // UTS #46 data for ASCII characters.
280 // The normalizer (using uts46.nrm) maps uppercase ASCII letters to lowercase
281 // and passes through all other ASCII characters.
282 // If UIDNA_USE_STD3_RULES is set, then non-LDH characters are disallowed
284 // The ASCII fastpath also uses this data.
285 // Values: -1=disallowed 0==valid 1==mapped (lowercase)
286 static const int8_t asciiData
[128]={
287 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
288 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
289 // 002D..002E; valid # HYPHEN-MINUS..FULL STOP
290 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1,
291 // 0030..0039; valid # DIGIT ZERO..DIGIT NINE
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
293 // 0041..005A; mapped # LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z
294 -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
295 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
296 // 0061..007A; valid # LATIN SMALL LETTER A..LATIN SMALL LETTER Z
297 -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1
302 UTS46::process(const UnicodeString
&src
,
303 UBool isLabel
, UBool toASCII
,
305 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
306 // uts46Norm2.normalize() would do all of this error checking and setup,
307 // but with the ASCII fastpath we do not always call it, and do not
309 if(U_FAILURE(errorCode
)) {
313 const UChar
*srcArray
=src
.getBuffer();
314 if(&dest
==&src
|| srcArray
==NULL
) {
315 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
319 // Arguments are fine, reset output values.
322 int32_t srcLength
=src
.length();
325 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
329 UChar
*destArray
=dest
.getBuffer(srcLength
);
330 if(destArray
==NULL
) {
331 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
335 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
336 int32_t labelStart
=0;
341 if((i
-labelStart
)>63) {
342 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
344 // There is a trailing dot if labelStart==i.
345 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
346 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
349 info
.errors
|=info
.labelErrors
;
350 dest
.releaseBuffer(i
);
357 int cData
=asciiData
[c
];
359 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
360 } else if(cData
<0 && disallowNonLDHDot
) {
361 break; // Replacing with U+FFFD can be complicated for toASCII.
364 if(c
==0x2d) { // hyphen
365 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
366 // "??--..." is Punycode or forbidden.
367 ++i
; // '-' was copied to dest already
371 // label starts with "-"
372 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
374 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
375 // label ends with "-"
376 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
378 } else if(c
==0x2e) { // dot
380 // Replacing with U+FFFD can be complicated for toASCII.
381 ++i
; // '.' was copied to dest already
386 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
387 } else if((i
-labelStart
)>63) {
388 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
391 info
.errors
|=info
.labelErrors
;
397 info
.errors
|=info
.labelErrors
;
398 dest
.releaseBuffer(i
);
399 processUnicode(src
, labelStart
, i
, isLabel
, toASCII
, dest
, info
, errorCode
);
400 if( info
.isBiDi
&& U_SUCCESS(errorCode
) && (info
.errors
&severeErrors
)==0 &&
401 (!info
.isOkBiDi
|| (labelStart
>0 && !isASCIIOkBiDi(dest
.getBuffer(), labelStart
)))
403 info
.errors
|=UIDNA_ERROR_BIDI
;
409 UTS46::processUTF8(const StringPiece
&src
,
410 UBool isLabel
, UBool toASCII
,
412 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
413 if(U_FAILURE(errorCode
)) {
416 const char *srcArray
=src
.data();
417 int32_t srcLength
=src
.length();
418 if(srcArray
==NULL
&& srcLength
!=0) {
419 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
422 // Arguments are fine, reset output values.
426 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
431 UnicodeString destString
;
432 int32_t labelStart
=0;
433 if(srcLength
<=256) { // length of stackArray[]
435 char stackArray
[256];
436 int32_t destCapacity
;
437 char *destArray
=dest
.GetAppendBuffer(srcLength
, srcLength
+20,
438 stackArray
, LENGTHOF(stackArray
), &destCapacity
);
439 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
444 if((i
-labelStart
)>63) {
445 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
447 // There is a trailing dot if labelStart==i.
448 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
449 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
452 info
.errors
|=info
.labelErrors
;
453 dest
.Append(destArray
, i
);
458 if((int8_t)c
<0) { // (uint8_t)c>0x7f
461 int cData
=asciiData
[(int)c
]; // Cast: gcc warns about indexing with a char.
463 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
464 } else if(cData
<0 && disallowNonLDHDot
) {
465 break; // Replacing with U+FFFD can be complicated for toASCII.
468 if(c
==0x2d) { // hyphen
469 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
470 // "??--..." is Punycode or forbidden.
474 // label starts with "-"
475 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
477 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
478 // label ends with "-"
479 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
481 } else if(c
==0x2e) { // dot
483 break; // Replacing with U+FFFD can be complicated for toASCII.
487 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
488 } else if((i
-labelStart
)>63) {
489 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
492 info
.errors
|=info
.labelErrors
;
498 info
.errors
|=info
.labelErrors
;
499 // Convert the processed ASCII prefix of the current label to UTF-16.
500 int32_t mappingStart
=i
-labelStart
;
501 destString
=UnicodeString::fromUTF8(StringPiece(destArray
+labelStart
, mappingStart
));
502 // Output the previous ASCII labels and process the rest of src in UTF-16.
503 dest
.Append(destArray
, labelStart
);
504 processUnicode(UnicodeString::fromUTF8(StringPiece(src
, labelStart
)), 0, mappingStart
,
506 destString
, info
, errorCode
);
508 // src is too long for the ASCII fastpath implementation.
509 processUnicode(UnicodeString::fromUTF8(src
), 0, 0,
511 destString
, info
, errorCode
);
513 destString
.toUTF8(dest
); // calls dest.Flush()
514 if(toASCII
&& !isLabel
) {
515 // length==labelStart==254 means that there is a trailing dot (ok) and
516 // destString is empty (do not index at 253-labelStart).
517 int32_t length
=labelStart
+destString
.length();
518 if( length
>=254 && isASCIIString(destString
) &&
520 (labelStart
<254 && destString
[253-labelStart
]!=0x2e))
522 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
525 if( info
.isBiDi
&& U_SUCCESS(errorCode
) && (info
.errors
&severeErrors
)==0 &&
526 (!info
.isOkBiDi
|| (labelStart
>0 && !isASCIIOkBiDi(srcArray
, labelStart
)))
528 info
.errors
|=UIDNA_ERROR_BIDI
;
533 UTS46::processUnicode(const UnicodeString
&src
,
534 int32_t labelStart
, int32_t mappingStart
,
535 UBool isLabel
, UBool toASCII
,
537 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
538 if(mappingStart
==0) {
539 uts46Norm2
.normalize(src
, dest
, errorCode
);
541 uts46Norm2
.normalizeSecondAndAppend(dest
, src
.tempSubString(mappingStart
), errorCode
);
543 if(U_FAILURE(errorCode
)) {
547 toASCII
? (options
&UIDNA_NONTRANSITIONAL_TO_ASCII
)==0 :
548 (options
&UIDNA_NONTRANSITIONAL_TO_UNICODE
)==0;
549 const UChar
*destArray
=dest
.getBuffer();
550 int32_t destLength
=dest
.length();
551 int32_t labelLimit
=labelStart
;
552 while(labelLimit
<destLength
) {
553 UChar c
=destArray
[labelLimit
];
554 if(c
==0x2e && !isLabel
) {
555 int32_t labelLength
=labelLimit
-labelStart
;
556 int32_t newLength
=processLabel(dest
, labelStart
, labelLength
,
557 toASCII
, info
, errorCode
);
558 info
.errors
|=info
.labelErrors
;
560 if(U_FAILURE(errorCode
)) {
563 destArray
=dest
.getBuffer();
564 destLength
+=newLength
-labelLength
;
565 labelLimit
=labelStart
+=newLength
+1;
566 } else if(0xdf<=c
&& c
<=0x200d && (c
==0xdf || c
==0x3c2 || c
>=0x200c)) {
567 info
.isTransDiff
=TRUE
;
569 destLength
=mapDevChars(dest
, labelStart
, labelLimit
, errorCode
);
570 if(U_FAILURE(errorCode
)) {
573 destArray
=dest
.getBuffer();
574 // Do not increment labelLimit in case c was removed.
575 // All deviation characters have been mapped, no need to check for them again.
584 // Permit an empty label at the end (0<labelStart==labelLimit==destLength is ok)
585 // but not an empty label elsewhere nor a completely empty domain name.
586 // processLabel() sets UIDNA_ERROR_EMPTY_LABEL when labelLength==0.
587 if(0==labelStart
|| labelStart
<labelLimit
) {
588 processLabel(dest
, labelStart
, labelLimit
-labelStart
,
589 toASCII
, info
, errorCode
);
590 info
.errors
|=info
.labelErrors
;
596 UTS46::mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
597 UErrorCode
&errorCode
) const {
598 int32_t length
=dest
.length();
599 UChar
*s
=dest
.getBuffer(dest
[mappingStart
]==0xdf ? length
+1 : length
);
601 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
604 int32_t capacity
=dest
.getCapacity();
605 UBool didMapDevChars
=FALSE
;
606 int32_t readIndex
=mappingStart
, writeIndex
=mappingStart
;
608 UChar c
=s
[readIndex
++];
611 // Map sharp s to ss.
613 s
[writeIndex
++]=0x73; // Replace sharp s with first s.
614 // Insert second s and account for possible buffer reallocation.
615 if(writeIndex
==readIndex
) {
616 if(length
==capacity
) {
617 dest
.releaseBuffer(length
);
618 s
=dest
.getBuffer(length
+1);
620 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
623 capacity
=dest
.getCapacity();
625 u_memmove(s
+writeIndex
+1, s
+writeIndex
, length
-writeIndex
);
628 s
[writeIndex
++]=0x73;
631 case 0x3c2: // Map final sigma to nonfinal sigma.
633 s
[writeIndex
++]=0x3c3;
635 case 0x200c: // Ignore/remove ZWNJ.
636 case 0x200d: // Ignore/remove ZWJ.
641 // Only really necessary if writeIndex was different from readIndex.
645 } while(writeIndex
<length
);
646 dest
.releaseBuffer(length
);
648 // Mapping deviation characters might have resulted in an un-NFC string.
649 // We could use either the NFC or the UTS #46 normalizer.
650 // By using the UTS #46 normalizer again, we avoid having to load a second .nrm data file.
651 UnicodeString normalized
;
652 uts46Norm2
.normalize(dest
.tempSubString(labelStart
), normalized
, errorCode
);
653 if(U_SUCCESS(errorCode
)) {
654 dest
.replace(labelStart
, 0x7fffffff, normalized
);
655 return dest
.length();
661 // Some non-ASCII characters are equivalent to sequences with
662 // non-LDH ASCII characters. To find them:
663 // grep disallowed_STD3_valid IdnaMappingTable.txt (or uts46.txt)
665 isNonASCIIDisallowedSTD3Valid(UChar32 c
) {
666 return c
==0x2260 || c
==0x226E || c
==0x226F;
669 // Replace the label in dest with the label string, if the label was modified.
670 // If &label==&dest then the label was modified in-place and labelLength
671 // is the new label length, different from label.length().
672 // If &label!=&dest then labelLength==label.length().
673 // Returns labelLength (= the new label length).
675 replaceLabel(UnicodeString
&dest
, int32_t destLabelStart
, int32_t destLabelLength
,
676 const UnicodeString
&label
, int32_t labelLength
) {
678 dest
.replace(destLabelStart
, destLabelLength
, label
);
684 UTS46::processLabel(UnicodeString
&dest
,
685 int32_t labelStart
, int32_t labelLength
,
687 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
688 UnicodeString fromPunycode
;
689 UnicodeString
*labelString
;
690 const UChar
*label
=dest
.getBuffer()+labelStart
;
691 int32_t destLabelStart
=labelStart
;
692 int32_t destLabelLength
=labelLength
;
694 if(labelLength
>=4 && label
[0]==0x78 && label
[1]==0x6e && label
[2]==0x2d && label
[3]==0x2d) {
695 // Label starts with "xn--", try to un-Punycode it.
697 UChar
*unicodeBuffer
=fromPunycode
.getBuffer(-1); // capacity==-1: most labels should fit
698 if(unicodeBuffer
==NULL
) {
699 // Should never occur if we used capacity==-1 which uses the internal buffer.
700 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
703 UErrorCode punycodeErrorCode
=U_ZERO_ERROR
;
704 int32_t unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
705 unicodeBuffer
, fromPunycode
.getCapacity(),
706 NULL
, &punycodeErrorCode
);
707 if(punycodeErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
708 fromPunycode
.releaseBuffer(0);
709 unicodeBuffer
=fromPunycode
.getBuffer(unicodeLength
);
710 if(unicodeBuffer
==NULL
) {
711 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
714 punycodeErrorCode
=U_ZERO_ERROR
;
715 unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
716 unicodeBuffer
, fromPunycode
.getCapacity(),
717 NULL
, &punycodeErrorCode
);
719 fromPunycode
.releaseBuffer(unicodeLength
);
720 if(U_FAILURE(punycodeErrorCode
)) {
721 info
.labelErrors
|=UIDNA_ERROR_PUNYCODE
;
722 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
724 // Check for NFC, and for characters that are not
725 // valid or deviation characters according to the normalizer.
726 // If there is something wrong, then the string will change.
727 // Note that the normalizer passes through non-LDH ASCII and deviation characters.
728 // Deviation characters are ok in Punycode even in transitional processing.
729 // In the code further below, if we find non-LDH ASCII and we have UIDNA_USE_STD3_RULES
730 // then we will set UIDNA_ERROR_INVALID_ACE_LABEL there too.
731 UBool isValid
=uts46Norm2
.isNormalized(fromPunycode
, errorCode
);
732 if(U_FAILURE(errorCode
)) {
736 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
737 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
739 labelString
=&fromPunycode
;
740 label
=fromPunycode
.getBuffer();
742 labelLength
=fromPunycode
.length();
750 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
752 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
755 if(labelLength
>=4 && label
[2]==0x2d && label
[3]==0x2d) {
756 // label starts with "??--"
757 info
.labelErrors
|=UIDNA_ERROR_HYPHEN_3_4
;
760 // label starts with "-"
761 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
763 if(label
[labelLength
-1]==0x2d) {
764 // label ends with "-"
765 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
767 // If the label was not a Punycode label, then it was the result of
768 // mapping, normalization and label segmentation.
769 // If the label was in Punycode, then we mapped it again above
770 // and checked its validity.
771 // Now we handle the STD3 restriction to LDH characters (if set)
772 // and we look for U+FFFD which indicates disallowed characters
773 // in a non-Punycode label or U+FFFD itself in a Punycode label.
774 // We also check for dots which can come from the input to a single-label function.
775 // Ok to cast away const because we own the UnicodeString.
776 UChar
*s
=(UChar
*)label
;
777 const UChar
*limit
=label
+labelLength
;
779 // If we enforce STD3 rules, then ASCII characters other than LDH and dot are disallowed.
780 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
785 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
787 } else if(disallowNonLDHDot
&& asciiData
[c
]<0) {
788 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
793 if(disallowNonLDHDot
&& isNonASCIIDisallowedSTD3Valid(c
)) {
794 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
796 } else if(c
==0xfffd) {
797 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
802 // Check for a leading combining mark after other validity checks
803 // so that we don't report UIDNA_ERROR_DISALLOWED for the U+FFFD from here.
806 // "Unsafe" is ok because unpaired surrogates were mapped to U+FFFD.
807 U16_NEXT_UNSAFE(label
, cpLength
, c
);
808 if((U_GET_GC_MASK(c
)&U_GC_M_MASK
)!=0) {
809 info
.labelErrors
|=UIDNA_ERROR_LEADING_COMBINING_MARK
;
810 labelString
->replace(labelStart
, cpLength
, (UChar
)0xfffd);
811 label
=labelString
->getBuffer()+labelStart
;
812 labelLength
+=1-cpLength
;
813 if(labelString
==&dest
) {
814 destLabelLength
=labelLength
;
817 if((info
.labelErrors
&severeErrors
)==0) {
818 // Do contextual checks only if we do not have U+FFFD from a severe error
819 // because U+FFFD can make these checks fail.
820 if((options
&UIDNA_CHECK_BIDI
)!=0 && (!info
.isBiDi
|| info
.isOkBiDi
)) {
821 checkLabelBiDi(label
, labelLength
, info
);
823 if( (options
&UIDNA_CHECK_CONTEXTJ
)!=0 && (oredChars
&0x200c)==0x200c &&
824 !isLabelOkContextJ(label
, labelLength
)
826 info
.labelErrors
|=UIDNA_ERROR_CONTEXTJ
;
828 if((options
&UIDNA_CHECK_CONTEXTO
)!=0 && oredChars
>=0xb7) {
829 checkLabelContextO(label
, labelLength
, info
);
833 // Leave a Punycode label unchanged if it has no severe errors.
834 if(destLabelLength
>63) {
835 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
837 return destLabelLength
;
838 } else if(oredChars
>=0x80) {
839 // Contains non-ASCII characters.
840 UnicodeString punycode
;
841 UChar
*buffer
=punycode
.getBuffer(63); // 63==maximum DNS label length
843 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
844 return destLabelLength
;
846 buffer
[0]=0x78; // Write "xn--".
850 int32_t punycodeLength
=u_strToPunycode(label
, labelLength
,
851 buffer
+4, punycode
.getCapacity()-4,
853 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
854 errorCode
=U_ZERO_ERROR
;
855 punycode
.releaseBuffer(4);
856 buffer
=punycode
.getBuffer(4+punycodeLength
);
858 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
859 return destLabelLength
;
861 punycodeLength
=u_strToPunycode(label
, labelLength
,
862 buffer
+4, punycode
.getCapacity()-4,
866 punycode
.releaseBuffer(punycodeLength
);
867 if(U_FAILURE(errorCode
)) {
868 return destLabelLength
;
870 if(punycodeLength
>63) {
871 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
873 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
874 punycode
, punycodeLength
);
878 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
883 // If a Punycode label has severe errors,
884 // then leave it but make sure it does not look valid.
886 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
887 return markBadACELabel(dest
, destLabelStart
, destLabelLength
, toASCII
, info
);
890 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
893 // Make sure an ACE label does not look valid.
894 // Append U+FFFD if the label has only LDH characters.
895 // If UIDNA_USE_STD3_RULES, also replace disallowed ASCII characters with U+FFFD.
897 UTS46::markBadACELabel(UnicodeString
&dest
,
898 int32_t labelStart
, int32_t labelLength
,
899 UBool toASCII
, IDNAInfo
&info
) const {
900 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
903 const UChar
*label
=dest
.getBuffer()+labelStart
;
904 // Ok to cast away const because we own the UnicodeString.
905 UChar
*s
=(UChar
*)label
+4; // After the initial "xn--".
906 const UChar
*limit
=label
+labelLength
;
911 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
913 isASCII
=onlyLDH
=FALSE
;
914 } else if(asciiData
[c
]<0) {
916 if(disallowNonLDHDot
) {
922 isASCII
=onlyLDH
=FALSE
;
926 dest
.insert(labelStart
+labelLength
, (UChar
)0xfffd);
929 if(toASCII
&& isASCII
&& labelLength
>63) {
930 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
936 const uint32_t L_MASK
=U_MASK(U_LEFT_TO_RIGHT
);
937 const uint32_t R_AL_MASK
=U_MASK(U_RIGHT_TO_LEFT
)|U_MASK(U_RIGHT_TO_LEFT_ARABIC
);
938 const uint32_t L_R_AL_MASK
=L_MASK
|R_AL_MASK
;
940 const uint32_t R_AL_AN_MASK
=R_AL_MASK
|U_MASK(U_ARABIC_NUMBER
);
942 const uint32_t EN_AN_MASK
=U_MASK(U_EUROPEAN_NUMBER
)|U_MASK(U_ARABIC_NUMBER
);
943 const uint32_t R_AL_EN_AN_MASK
=R_AL_MASK
|EN_AN_MASK
;
944 const uint32_t L_EN_MASK
=L_MASK
|U_MASK(U_EUROPEAN_NUMBER
);
946 const uint32_t ES_CS_ET_ON_BN_NSM_MASK
=
947 U_MASK(U_EUROPEAN_NUMBER_SEPARATOR
)|
948 U_MASK(U_COMMON_NUMBER_SEPARATOR
)|
949 U_MASK(U_EUROPEAN_NUMBER_TERMINATOR
)|
950 U_MASK(U_OTHER_NEUTRAL
)|
951 U_MASK(U_BOUNDARY_NEUTRAL
)|
952 U_MASK(U_DIR_NON_SPACING_MARK
);
953 const uint32_t L_EN_ES_CS_ET_ON_BN_NSM_MASK
=L_EN_MASK
|ES_CS_ET_ON_BN_NSM_MASK
;
954 const uint32_t R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK
=R_AL_MASK
|EN_AN_MASK
|ES_CS_ET_ON_BN_NSM_MASK
;
956 // We scan the whole label and check both for whether it contains RTL characters
957 // and whether it passes the BiDi Rule.
958 // In a BiDi domain name, all labels must pass the BiDi Rule, but we might find
959 // that a domain name is a BiDi domain name (has an RTL label) only after
960 // processing several earlier labels.
962 UTS46::checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
963 // IDNA2008 BiDi rule
964 // Get the directionality of the first character.
967 U16_NEXT_UNSAFE(label
, i
, c
);
968 uint32_t firstMask
=U_MASK(u_charDirection(c
));
969 // 1. The first character must be a character with BIDI property L, R
970 // or AL. If it has the R or AL property, it is an RTL label; if it
971 // has the L property, it is an LTR label.
972 if((firstMask
&~L_R_AL_MASK
)!=0) {
975 // Get the directionality of the last non-NSM character.
982 U16_PREV_UNSAFE(label
, labelLength
, c
);
983 UCharDirection dir
=u_charDirection(c
);
984 if(dir
!=U_DIR_NON_SPACING_MARK
) {
985 lastMask
=U_MASK(dir
);
989 // 3. In an RTL label, the end of the label must be a character with
990 // BIDI property R, AL, EN or AN, followed by zero or more
991 // characters with BIDI property NSM.
992 // 6. In an LTR label, the end of the label must be a character with
993 // BIDI property L or EN, followed by zero or more characters with
994 // BIDI property NSM.
995 if( (firstMask
&L_MASK
)!=0 ?
996 (lastMask
&~L_EN_MASK
)!=0 :
997 (lastMask
&~R_AL_EN_AN_MASK
)!=0
1001 // Get the directionalities of the intervening characters.
1003 while(i
<labelLength
) {
1004 U16_NEXT_UNSAFE(label
, i
, c
);
1005 mask
|=U_MASK(u_charDirection(c
));
1007 if(firstMask
&L_MASK
) {
1008 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1009 // ES, CS, ET, ON, BN and NSM are allowed.
1010 if((mask
&~L_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1011 info
.isOkBiDi
=FALSE
;
1014 // 2. In an RTL label, only characters with the BIDI properties R, AL,
1015 // AN, EN, ES, CS, ET, ON, BN and NSM are allowed.
1016 if((mask
&~R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1017 info
.isOkBiDi
=FALSE
;
1019 // 4. In an RTL label, if an EN is present, no AN may be present, and
1021 if((mask
&EN_AN_MASK
)==EN_AN_MASK
) {
1022 info
.isOkBiDi
=FALSE
;
1025 // An RTL label is a label that contains at least one character of type
1026 // R, AL or AN. [...]
1027 // A "BIDI domain name" is a domain name that contains at least one RTL
1029 // The following rule, consisting of six conditions, applies to labels
1030 // in BIDI domain names.
1031 if(((firstMask
|mask
|lastMask
)&R_AL_AN_MASK
)!=0) {
1036 // Special code for the ASCII prefix of a BiDi domain name.
1037 // The ASCII prefix is all-LTR.
1039 // IDNA2008 BiDi rule, parts relevant to ASCII labels:
1040 // 1. The first character must be a character with BIDI property L [...]
1041 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1042 // ES, CS, ET, ON, BN and NSM are allowed.
1043 // 6. In an LTR label, the end of the label must be a character with
1044 // BIDI property L or EN [...]
1046 // UTF-16 version, called for mapped ASCII prefix.
1047 // Cannot contain uppercase A-Z.
1048 // s[length-1] must be the trailing dot.
1050 isASCIIOkBiDi(const UChar
*s
, int32_t length
) {
1051 int32_t labelStart
=0;
1052 for(int32_t i
=0; i
<length
; ++i
) {
1054 if(c
==0x2e) { // dot
1057 if(!(0x61<=c
&& c
<=0x7a) && !(0x30<=c
&& c
<=0x39)) {
1058 // Last character in the label is not an L or EN.
1063 } else if(i
==labelStart
) {
1064 if(!(0x61<=c
&& c
<=0x7a)) {
1065 // First character in the label is not an L.
1069 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1070 // Intermediate character in the label is a B, S or WS.
1078 // UTF-8 version, called for source ASCII prefix.
1079 // Can contain uppercase A-Z.
1080 // s[length-1] must be the trailing dot.
1082 isASCIIOkBiDi(const char *s
, int32_t length
) {
1083 int32_t labelStart
=0;
1084 for(int32_t i
=0; i
<length
; ++i
) {
1086 if(c
==0x2e) { // dot
1089 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a) && !(0x30<=c
&& c
<=0x39)) {
1090 // Last character in the label is not an L or EN.
1095 } else if(i
==labelStart
) {
1096 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a)) {
1097 // First character in the label is not an L.
1101 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1102 // Intermediate character in the label is a B, S or WS.
1111 UTS46::isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const {
1112 const UBiDiProps
*bdp
=ubidi_getSingleton();
1113 // [IDNA2008-Tables]
1114 // 200C..200D ; CONTEXTJ # ZERO WIDTH NON-JOINER..ZERO WIDTH JOINER
1115 for(int32_t i
=0; i
<labelLength
; ++i
) {
1116 if(label
[i
]==0x200c) {
1117 // Appendix A.1. ZERO WIDTH NON-JOINER
1120 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1121 // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
1122 // (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
1128 U16_PREV_UNSAFE(label
, j
, c
);
1129 if(uts46Norm2
.getCombiningClass(c
)==9) {
1132 // check precontext (Joining_Type:{L,D})(Joining_Type:T)*
1134 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1135 if(type
==U_JT_TRANSPARENT
) {
1139 U16_PREV_UNSAFE(label
, j
, c
);
1140 } else if(type
==U_JT_LEFT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1141 break; // precontext fulfilled
1146 // check postcontext (Joining_Type:T)*(Joining_Type:{R,D})
1148 if(j
==labelLength
) {
1151 U16_NEXT_UNSAFE(label
, j
, c
);
1152 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1153 if(type
==U_JT_TRANSPARENT
) {
1154 // just skip this character
1155 } else if(type
==U_JT_RIGHT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1156 break; // postcontext fulfilled
1161 } else if(label
[i
]==0x200d) {
1162 // Appendix A.2. ZERO WIDTH JOINER (U+200D)
1165 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1171 U16_PREV_UNSAFE(label
, j
, c
);
1172 if(uts46Norm2
.getCombiningClass(c
)!=9) {
1181 UTS46::checkLabelContextO(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
1182 int32_t labelEnd
=labelLength
-1; // inclusive
1183 int32_t arabicDigits
=0; // -1 for 066x, +1 for 06Fx
1184 for(int32_t i
=0; i
<=labelEnd
; ++i
) {
1188 } else if(c
<=0x6f9) {
1190 // Appendix A.3. MIDDLE DOT (U+00B7)
1193 // If Before(cp) .eq. U+006C And
1194 // After(cp) .eq. U+006C Then True;
1195 if(!(0<i
&& label
[i
-1]==0x6c &&
1196 i
<labelEnd
&& label
[i
+1]==0x6c)) {
1197 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1199 } else if(c
==0x375) {
1200 // Appendix A.4. GREEK LOWER NUMERAL SIGN (KERAIA) (U+0375)
1203 // If Script(After(cp)) .eq. Greek Then True;
1204 UScriptCode script
=USCRIPT_INVALID_CODE
;
1206 UErrorCode errorCode
=U_ZERO_ERROR
;
1208 U16_NEXT(label
, j
, labelLength
, c
);
1209 script
=uscript_getScript(c
, &errorCode
);
1211 if(script
!=USCRIPT_GREEK
) {
1212 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1214 } else if(c
==0x5f3 || c
==0x5f4) {
1215 // Appendix A.5. HEBREW PUNCTUATION GERESH (U+05F3)
1218 // If Script(Before(cp)) .eq. Hebrew Then True;
1220 // Appendix A.6. HEBREW PUNCTUATION GERSHAYIM (U+05F4)
1223 // If Script(Before(cp)) .eq. Hebrew Then True;
1224 UScriptCode script
=USCRIPT_INVALID_CODE
;
1226 UErrorCode errorCode
=U_ZERO_ERROR
;
1228 U16_PREV(label
, 0, j
, c
);
1229 script
=uscript_getScript(c
, &errorCode
);
1231 if(script
!=USCRIPT_HEBREW
) {
1232 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1234 } else if(0x660<=c
/* && c<=0x6f9 */) {
1235 // Appendix A.8. ARABIC-INDIC DIGITS (0660..0669)
1238 // For All Characters:
1239 // If cp .in. 06F0..06F9 Then False;
1242 // Appendix A.9. EXTENDED ARABIC-INDIC DIGITS (06F0..06F9)
1245 // For All Characters:
1246 // If cp .in. 0660..0669 Then False;
1249 if(arabicDigits
>0) {
1250 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1253 } else if(0x6f0<=c
) {
1254 if(arabicDigits
<0) {
1255 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1260 } else if(c
==0x30fb) {
1261 // Appendix A.7. KATAKANA MIDDLE DOT (U+30FB)
1264 // For All Characters:
1265 // If Script(cp) .in. {Hiragana, Katakana, Han} Then True;
1267 UErrorCode errorCode
=U_ZERO_ERROR
;
1270 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1273 U16_NEXT(label
, j
, labelLength
, c
);
1274 UScriptCode script
=uscript_getScript(c
, &errorCode
);
1275 if(script
==USCRIPT_HIRAGANA
|| script
==USCRIPT_KATAKANA
|| script
==USCRIPT_HAN
) {
1285 // C API ------------------------------------------------------------------- ***
1289 U_CAPI UIDNA
* U_EXPORT2
1290 uidna_openUTS46(uint32_t options
, UErrorCode
*pErrorCode
) {
1291 return reinterpret_cast<UIDNA
*>(IDNA::createUTS46Instance(options
, *pErrorCode
));
1294 U_CAPI
void U_EXPORT2
1295 uidna_close(UIDNA
*idna
) {
1296 delete reinterpret_cast<IDNA
*>(idna
);
1300 checkArgs(const void *label
, int32_t length
,
1301 void *dest
, int32_t capacity
,
1302 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1303 if(U_FAILURE(*pErrorCode
)) {
1306 // sizeof(UIDNAInfo)=16 in the first API version.
1307 if(pInfo
==NULL
|| pInfo
->size
<16) {
1308 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1311 if( (label
==NULL
? length
!=0 : length
<-1) ||
1312 (dest
==NULL
? capacity
!=0 : capacity
<0) ||
1313 (dest
==label
&& label
!=NULL
)
1315 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1318 // Set all *pInfo bytes to 0 except for the size field itself.
1319 uprv_memset(&pInfo
->size
+1, 0, pInfo
->size
-sizeof(pInfo
->size
));
1324 idnaInfoToStruct(IDNAInfo
&info
, UIDNAInfo
*pInfo
) {
1325 pInfo
->isTransitionalDifferent
=info
.isTransitionalDifferent();
1326 pInfo
->errors
=info
.getErrors();
1329 U_CAPI
int32_t U_EXPORT2
1330 uidna_labelToASCII(const UIDNA
*idna
,
1331 const UChar
*label
, int32_t length
,
1332 UChar
*dest
, int32_t capacity
,
1333 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1334 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1337 UnicodeString
src((UBool
)(length
<0), label
, length
);
1338 UnicodeString
destString(dest
, 0, capacity
);
1340 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII(src
, destString
, info
, *pErrorCode
);
1341 idnaInfoToStruct(info
, pInfo
);
1342 return destString
.extract(dest
, capacity
, *pErrorCode
);
1345 U_CAPI
int32_t U_EXPORT2
1346 uidna_labelToUnicode(const UIDNA
*idna
,
1347 const UChar
*label
, int32_t length
,
1348 UChar
*dest
, int32_t capacity
,
1349 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1350 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1353 UnicodeString
src((UBool
)(length
<0), label
, length
);
1354 UnicodeString
destString(dest
, 0, capacity
);
1356 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicode(src
, destString
, info
, *pErrorCode
);
1357 idnaInfoToStruct(info
, pInfo
);
1358 return destString
.extract(dest
, capacity
, *pErrorCode
);
1361 U_CAPI
int32_t U_EXPORT2
1362 uidna_nameToASCII(const UIDNA
*idna
,
1363 const UChar
*name
, int32_t length
,
1364 UChar
*dest
, int32_t capacity
,
1365 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1366 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1369 UnicodeString
src((UBool
)(length
<0), name
, length
);
1370 UnicodeString
destString(dest
, 0, capacity
);
1372 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII(src
, destString
, info
, *pErrorCode
);
1373 idnaInfoToStruct(info
, pInfo
);
1374 return destString
.extract(dest
, capacity
, *pErrorCode
);
1377 U_CAPI
int32_t U_EXPORT2
1378 uidna_nameToUnicode(const UIDNA
*idna
,
1379 const UChar
*name
, int32_t length
,
1380 UChar
*dest
, int32_t capacity
,
1381 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1382 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1385 UnicodeString
src((UBool
)(length
<0), name
, length
);
1386 UnicodeString
destString(dest
, 0, capacity
);
1388 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicode(src
, destString
, info
, *pErrorCode
);
1389 idnaInfoToStruct(info
, pInfo
);
1390 return destString
.extract(dest
, capacity
, *pErrorCode
);
1393 U_CAPI
int32_t U_EXPORT2
1394 uidna_labelToASCII_UTF8(const UIDNA
*idna
,
1395 const char *label
, int32_t length
,
1396 char *dest
, int32_t capacity
,
1397 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1398 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1401 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1402 CheckedArrayByteSink
sink(dest
, capacity
);
1404 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1405 idnaInfoToStruct(info
, pInfo
);
1406 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1409 U_CAPI
int32_t U_EXPORT2
1410 uidna_labelToUnicodeUTF8(const UIDNA
*idna
,
1411 const char *label
, int32_t length
,
1412 char *dest
, int32_t capacity
,
1413 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1414 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1417 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1418 CheckedArrayByteSink
sink(dest
, capacity
);
1420 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1421 idnaInfoToStruct(info
, pInfo
);
1422 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1425 U_CAPI
int32_t U_EXPORT2
1426 uidna_nameToASCII_UTF8(const UIDNA
*idna
,
1427 const char *name
, int32_t length
,
1428 char *dest
, int32_t capacity
,
1429 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1430 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1433 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1434 CheckedArrayByteSink
sink(dest
, capacity
);
1436 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1437 idnaInfoToStruct(info
, pInfo
);
1438 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1441 U_CAPI
int32_t U_EXPORT2
1442 uidna_nameToUnicodeUTF8(const UIDNA
*idna
,
1443 const char *name
, int32_t length
,
1444 char *dest
, int32_t capacity
,
1445 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1446 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1449 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1450 CheckedArrayByteSink
sink(dest
, capacity
);
1452 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1453 idnaInfoToStruct(info
, pInfo
);
1454 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1457 #endif // UCONFIG_NO_IDNA