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 // Note about tests for UIDNA_ERROR_DOMAIN_NAME_TOO_LONG:
32 // The domain name length limit is 255 octets in an internal DNS representation
33 // where the last ("root") label is the empty label
34 // represented by length byte 0 alone.
35 // In a conventional string, this translates to 253 characters, or 254
36 // if there is a trailing dot for the root label.
40 // Severe errors which usually result in a U+FFFD replacement character in the result string.
41 const uint32_t severeErrors
=
42 UIDNA_ERROR_LEADING_COMBINING_MARK
|
43 UIDNA_ERROR_DISALLOWED
|
45 UIDNA_ERROR_LABEL_HAS_DOT
|
46 UIDNA_ERROR_INVALID_ACE_LABEL
;
49 isASCIIString(const UnicodeString
&dest
) {
50 const UChar
*s
=dest
.getBuffer();
51 const UChar
*limit
=s
+dest
.length();
61 isASCIIOkBiDi(const UChar
*s
, int32_t length
);
64 isASCIIOkBiDi(const char *s
, int32_t length
);
66 // IDNA class default implementations -------------------------------------- ***
71 IDNA::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
72 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
73 if(U_SUCCESS(errorCode
)) {
74 UnicodeString destString
;
75 labelToASCII(UnicodeString::fromUTF8(label
), destString
,
76 info
, errorCode
).toUTF8(dest
);
81 IDNA::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
82 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
83 if(U_SUCCESS(errorCode
)) {
84 UnicodeString destString
;
85 labelToUnicode(UnicodeString::fromUTF8(label
), destString
,
86 info
, errorCode
).toUTF8(dest
);
91 IDNA::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
92 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
93 if(U_SUCCESS(errorCode
)) {
94 UnicodeString destString
;
95 nameToASCII(UnicodeString::fromUTF8(name
), destString
,
96 info
, errorCode
).toUTF8(dest
);
101 IDNA::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
102 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
103 if(U_SUCCESS(errorCode
)) {
104 UnicodeString destString
;
105 nameToUnicode(UnicodeString::fromUTF8(name
), destString
,
106 info
, errorCode
).toUTF8(dest
);
110 // UTS46 class declaration ------------------------------------------------- ***
112 class UTS46
: public IDNA
{
114 UTS46(uint32_t options
, UErrorCode
&errorCode
);
117 virtual UnicodeString
&
118 labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
119 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
121 virtual UnicodeString
&
122 labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
123 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
125 virtual UnicodeString
&
126 nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
127 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
129 virtual UnicodeString
&
130 nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
131 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
134 labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
135 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
138 labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
139 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
142 nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
143 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
146 nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
147 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
151 process(const UnicodeString
&src
,
152 UBool isLabel
, UBool toASCII
,
154 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
157 processUTF8(const StringPiece
&src
,
158 UBool isLabel
, UBool toASCII
,
160 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
163 processUnicode(const UnicodeString
&src
,
164 int32_t labelStart
, int32_t mappingStart
,
165 UBool isLabel
, UBool toASCII
,
167 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
169 // returns the new dest.length()
171 mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
172 UErrorCode
&errorCode
) const;
174 // returns the new label length
176 processLabel(UnicodeString
&dest
,
177 int32_t labelStart
, int32_t labelLength
,
179 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
181 markBadACELabel(UnicodeString
&dest
,
182 int32_t labelStart
, int32_t labelLength
,
183 UBool toASCII
, IDNAInfo
&info
) const;
186 checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const;
189 isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const;
192 checkLabelContextO(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const;
194 const Normalizer2
&uts46Norm2
; // uts46.nrm
199 IDNA::createUTS46Instance(uint32_t options
, UErrorCode
&errorCode
) {
200 if(U_SUCCESS(errorCode
)) {
201 IDNA
*idna
=new UTS46(options
, errorCode
);
203 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
204 } else if(U_FAILURE(errorCode
)) {
214 // UTS46 implementation ---------------------------------------------------- ***
216 UTS46::UTS46(uint32_t opt
, UErrorCode
&errorCode
)
217 : uts46Norm2(*Normalizer2::getInstance(NULL
, "uts46", UNORM2_COMPOSE
, errorCode
)),
223 UTS46::labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
224 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
225 return process(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
229 UTS46::labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
230 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
231 return process(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
235 UTS46::nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
236 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
237 process(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
238 if( dest
.length()>=254 && (info
.errors
&UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
)==0 &&
239 isASCIIString(dest
) &&
240 (dest
.length()>254 || dest
[253]!=0x2e)
242 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
248 UTS46::nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
249 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
250 return process(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
254 UTS46::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
255 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
256 processUTF8(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
260 UTS46::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
261 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
262 processUTF8(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
266 UTS46::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
267 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
268 processUTF8(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
272 UTS46::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
273 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
274 processUTF8(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
277 // UTS #46 data for ASCII characters.
278 // The normalizer (using uts46.nrm) maps uppercase ASCII letters to lowercase
279 // and passes through all other ASCII characters.
280 // If UIDNA_USE_STD3_RULES is set, then non-LDH characters are disallowed
282 // The ASCII fastpath also uses this data.
283 // Values: -1=disallowed 0==valid 1==mapped (lowercase)
284 static const int8_t asciiData
[128]={
285 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
286 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
287 // 002D..002E; valid # HYPHEN-MINUS..FULL STOP
288 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1,
289 // 0030..0039; valid # DIGIT ZERO..DIGIT NINE
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
291 // 0041..005A; mapped # LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z
292 -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
293 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
294 // 0061..007A; valid # LATIN SMALL LETTER A..LATIN SMALL LETTER Z
295 -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
296 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1
300 UTS46::process(const UnicodeString
&src
,
301 UBool isLabel
, UBool toASCII
,
303 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
304 // uts46Norm2.normalize() would do all of this error checking and setup,
305 // but with the ASCII fastpath we do not always call it, and do not
307 if(U_FAILURE(errorCode
)) {
311 const UChar
*srcArray
=src
.getBuffer();
312 if(&dest
==&src
|| srcArray
==NULL
) {
313 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
317 // Arguments are fine, reset output values.
320 int32_t srcLength
=src
.length();
322 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
325 UChar
*destArray
=dest
.getBuffer(srcLength
);
326 if(destArray
==NULL
) {
327 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
331 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
332 int32_t labelStart
=0;
337 if((i
-labelStart
)>63) {
338 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
340 // There is a trailing dot if labelStart==i.
341 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
342 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
345 info
.errors
|=info
.labelErrors
;
346 dest
.releaseBuffer(i
);
353 int cData
=asciiData
[c
];
355 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
356 } else if(cData
<0 && disallowNonLDHDot
) {
357 break; // Replacing with U+FFFD can be complicated for toASCII.
360 if(c
==0x2d) { // hyphen
361 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
362 // "??--..." is Punycode or forbidden.
363 ++i
; // '-' was copied to dest already
367 // label starts with "-"
368 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
370 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
371 // label ends with "-"
372 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
374 } else if(c
==0x2e) { // dot
376 // Replacing with U+FFFD can be complicated for toASCII.
377 ++i
; // '.' was copied to dest already
381 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
383 if(toASCII
&& (i
-labelStart
)>63) {
384 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
386 info
.errors
|=info
.labelErrors
;
392 info
.errors
|=info
.labelErrors
;
393 dest
.releaseBuffer(i
);
394 processUnicode(src
, labelStart
, i
, isLabel
, toASCII
, dest
, info
, errorCode
);
395 if( info
.isBiDi
&& U_SUCCESS(errorCode
) && (info
.errors
&severeErrors
)==0 &&
396 (!info
.isOkBiDi
|| (labelStart
>0 && !isASCIIOkBiDi(dest
.getBuffer(), labelStart
)))
398 info
.errors
|=UIDNA_ERROR_BIDI
;
404 UTS46::processUTF8(const StringPiece
&src
,
405 UBool isLabel
, UBool toASCII
,
407 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
408 if(U_FAILURE(errorCode
)) {
411 const char *srcArray
=src
.data();
412 int32_t srcLength
=src
.length();
413 if(srcArray
==NULL
&& srcLength
!=0) {
414 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
417 // Arguments are fine, reset output values.
420 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
424 UnicodeString destString
;
425 int32_t labelStart
=0;
426 if(srcLength
<=256) { // length of stackArray[]
428 char stackArray
[256];
429 int32_t destCapacity
;
430 char *destArray
=dest
.GetAppendBuffer(srcLength
, srcLength
+20,
431 stackArray
, UPRV_LENGTHOF(stackArray
), &destCapacity
);
432 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
437 if((i
-labelStart
)>63) {
438 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
440 // There is a trailing dot if labelStart==i.
441 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
442 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
445 info
.errors
|=info
.labelErrors
;
446 dest
.Append(destArray
, i
);
451 if((int8_t)c
<0) { // (uint8_t)c>0x7f
454 int cData
=asciiData
[(int)c
]; // Cast: gcc warns about indexing with a char.
456 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
457 } else if(cData
<0 && disallowNonLDHDot
) {
458 break; // Replacing with U+FFFD can be complicated for toASCII.
461 if(c
==0x2d) { // hyphen
462 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
463 // "??--..." is Punycode or forbidden.
467 // label starts with "-"
468 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
470 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
471 // label ends with "-"
472 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
474 } else if(c
==0x2e) { // dot
476 break; // Replacing with U+FFFD can be complicated for toASCII.
479 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
481 if(toASCII
&& (i
-labelStart
)>63) {
482 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
484 info
.errors
|=info
.labelErrors
;
490 info
.errors
|=info
.labelErrors
;
491 // Convert the processed ASCII prefix of the current label to UTF-16.
492 int32_t mappingStart
=i
-labelStart
;
493 destString
=UnicodeString::fromUTF8(StringPiece(destArray
+labelStart
, mappingStart
));
494 // Output the previous ASCII labels and process the rest of src in UTF-16.
495 dest
.Append(destArray
, labelStart
);
496 processUnicode(UnicodeString::fromUTF8(StringPiece(src
, labelStart
)), 0, mappingStart
,
498 destString
, info
, errorCode
);
500 // src is too long for the ASCII fastpath implementation.
501 processUnicode(UnicodeString::fromUTF8(src
), 0, 0,
503 destString
, info
, errorCode
);
505 destString
.toUTF8(dest
); // calls dest.Flush()
506 if(toASCII
&& !isLabel
) {
507 // length==labelStart==254 means that there is a trailing dot (ok) and
508 // destString is empty (do not index at 253-labelStart).
509 int32_t length
=labelStart
+destString
.length();
510 if( length
>=254 && isASCIIString(destString
) &&
512 (labelStart
<254 && destString
[253-labelStart
]!=0x2e))
514 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
517 if( info
.isBiDi
&& U_SUCCESS(errorCode
) && (info
.errors
&severeErrors
)==0 &&
518 (!info
.isOkBiDi
|| (labelStart
>0 && !isASCIIOkBiDi(srcArray
, labelStart
)))
520 info
.errors
|=UIDNA_ERROR_BIDI
;
525 UTS46::processUnicode(const UnicodeString
&src
,
526 int32_t labelStart
, int32_t mappingStart
,
527 UBool isLabel
, UBool toASCII
,
529 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
530 if(mappingStart
==0) {
531 uts46Norm2
.normalize(src
, dest
, errorCode
);
533 uts46Norm2
.normalizeSecondAndAppend(dest
, src
.tempSubString(mappingStart
), errorCode
);
535 if(U_FAILURE(errorCode
)) {
539 toASCII
? (options
&UIDNA_NONTRANSITIONAL_TO_ASCII
)==0 :
540 (options
&UIDNA_NONTRANSITIONAL_TO_UNICODE
)==0;
541 const UChar
*destArray
=dest
.getBuffer();
542 int32_t destLength
=dest
.length();
543 int32_t labelLimit
=labelStart
;
544 while(labelLimit
<destLength
) {
545 UChar c
=destArray
[labelLimit
];
546 if(c
==0x2e && !isLabel
) {
547 int32_t labelLength
=labelLimit
-labelStart
;
548 int32_t newLength
=processLabel(dest
, labelStart
, labelLength
,
549 toASCII
, info
, errorCode
);
550 info
.errors
|=info
.labelErrors
;
552 if(U_FAILURE(errorCode
)) {
555 destArray
=dest
.getBuffer();
556 destLength
+=newLength
-labelLength
;
557 labelLimit
=labelStart
+=newLength
+1;
558 } else if(0xdf<=c
&& c
<=0x200d && (c
==0xdf || c
==0x3c2 || c
>=0x200c)) {
559 info
.isTransDiff
=TRUE
;
561 destLength
=mapDevChars(dest
, labelStart
, labelLimit
, errorCode
);
562 if(U_FAILURE(errorCode
)) {
565 destArray
=dest
.getBuffer();
566 // Do not increment labelLimit in case c was removed.
567 // All deviation characters have been mapped, no need to check for them again.
576 // Permit an empty label at the end (0<labelStart==labelLimit==destLength is ok)
577 // but not an empty label elsewhere nor a completely empty domain name.
578 // processLabel() sets UIDNA_ERROR_EMPTY_LABEL when labelLength==0.
579 if(0==labelStart
|| labelStart
<labelLimit
) {
580 processLabel(dest
, labelStart
, labelLimit
-labelStart
,
581 toASCII
, info
, errorCode
);
582 info
.errors
|=info
.labelErrors
;
588 UTS46::mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
589 UErrorCode
&errorCode
) const {
590 int32_t length
=dest
.length();
591 UChar
*s
=dest
.getBuffer(dest
[mappingStart
]==0xdf ? length
+1 : length
);
593 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
596 int32_t capacity
=dest
.getCapacity();
597 UBool didMapDevChars
=FALSE
;
598 int32_t readIndex
=mappingStart
, writeIndex
=mappingStart
;
600 UChar c
=s
[readIndex
++];
603 // Map sharp s to ss.
605 s
[writeIndex
++]=0x73; // Replace sharp s with first s.
606 // Insert second s and account for possible buffer reallocation.
607 if(writeIndex
==readIndex
) {
608 if(length
==capacity
) {
609 dest
.releaseBuffer(length
);
610 s
=dest
.getBuffer(length
+1);
612 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
615 capacity
=dest
.getCapacity();
617 u_memmove(s
+writeIndex
+1, s
+writeIndex
, length
-writeIndex
);
620 s
[writeIndex
++]=0x73;
623 case 0x3c2: // Map final sigma to nonfinal sigma.
625 s
[writeIndex
++]=0x3c3;
627 case 0x200c: // Ignore/remove ZWNJ.
628 case 0x200d: // Ignore/remove ZWJ.
633 // Only really necessary if writeIndex was different from readIndex.
637 } while(writeIndex
<length
);
638 dest
.releaseBuffer(length
);
640 // Mapping deviation characters might have resulted in an un-NFC string.
641 // We could use either the NFC or the UTS #46 normalizer.
642 // By using the UTS #46 normalizer again, we avoid having to load a second .nrm data file.
643 UnicodeString normalized
;
644 uts46Norm2
.normalize(dest
.tempSubString(labelStart
), normalized
, errorCode
);
645 if(U_SUCCESS(errorCode
)) {
646 dest
.replace(labelStart
, 0x7fffffff, normalized
);
647 return dest
.length();
653 // Some non-ASCII characters are equivalent to sequences with
654 // non-LDH ASCII characters. To find them:
655 // grep disallowed_STD3_valid IdnaMappingTable.txt (or uts46.txt)
657 isNonASCIIDisallowedSTD3Valid(UChar32 c
) {
658 return c
==0x2260 || c
==0x226E || c
==0x226F;
661 // Replace the label in dest with the label string, if the label was modified.
662 // If &label==&dest then the label was modified in-place and labelLength
663 // is the new label length, different from label.length().
664 // If &label!=&dest then labelLength==label.length().
665 // Returns labelLength (= the new label length).
667 replaceLabel(UnicodeString
&dest
, int32_t destLabelStart
, int32_t destLabelLength
,
668 const UnicodeString
&label
, int32_t labelLength
) {
670 dest
.replace(destLabelStart
, destLabelLength
, label
);
676 UTS46::processLabel(UnicodeString
&dest
,
677 int32_t labelStart
, int32_t labelLength
,
679 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
680 UnicodeString fromPunycode
;
681 UnicodeString
*labelString
;
682 const UChar
*label
=dest
.getBuffer()+labelStart
;
683 int32_t destLabelStart
=labelStart
;
684 int32_t destLabelLength
=labelLength
;
686 if(labelLength
>=4 && label
[0]==0x78 && label
[1]==0x6e && label
[2]==0x2d && label
[3]==0x2d) {
687 // Label starts with "xn--", try to un-Punycode it.
689 UChar
*unicodeBuffer
=fromPunycode
.getBuffer(-1); // capacity==-1: most labels should fit
690 if(unicodeBuffer
==NULL
) {
691 // Should never occur if we used capacity==-1 which uses the internal buffer.
692 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
695 UErrorCode punycodeErrorCode
=U_ZERO_ERROR
;
696 int32_t unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
697 unicodeBuffer
, fromPunycode
.getCapacity(),
698 NULL
, &punycodeErrorCode
);
699 if(punycodeErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
700 fromPunycode
.releaseBuffer(0);
701 unicodeBuffer
=fromPunycode
.getBuffer(unicodeLength
);
702 if(unicodeBuffer
==NULL
) {
703 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
706 punycodeErrorCode
=U_ZERO_ERROR
;
707 unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
708 unicodeBuffer
, fromPunycode
.getCapacity(),
709 NULL
, &punycodeErrorCode
);
711 fromPunycode
.releaseBuffer(unicodeLength
);
712 if(U_FAILURE(punycodeErrorCode
)) {
713 info
.labelErrors
|=UIDNA_ERROR_PUNYCODE
;
714 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
716 // Check for NFC, and for characters that are not
717 // valid or deviation characters according to the normalizer.
718 // If there is something wrong, then the string will change.
719 // Note that the normalizer passes through non-LDH ASCII and deviation characters.
720 // Deviation characters are ok in Punycode even in transitional processing.
721 // In the code further below, if we find non-LDH ASCII and we have UIDNA_USE_STD3_RULES
722 // then we will set UIDNA_ERROR_INVALID_ACE_LABEL there too.
723 UBool isValid
=uts46Norm2
.isNormalized(fromPunycode
, errorCode
);
724 if(U_FAILURE(errorCode
)) {
728 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
729 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
731 labelString
=&fromPunycode
;
732 label
=fromPunycode
.getBuffer();
734 labelLength
=fromPunycode
.length();
741 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
742 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
745 if(labelLength
>=4 && label
[2]==0x2d && label
[3]==0x2d) {
746 // label starts with "??--"
747 info
.labelErrors
|=UIDNA_ERROR_HYPHEN_3_4
;
750 // label starts with "-"
751 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
753 if(label
[labelLength
-1]==0x2d) {
754 // label ends with "-"
755 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
757 // If the label was not a Punycode label, then it was the result of
758 // mapping, normalization and label segmentation.
759 // If the label was in Punycode, then we mapped it again above
760 // and checked its validity.
761 // Now we handle the STD3 restriction to LDH characters (if set)
762 // and we look for U+FFFD which indicates disallowed characters
763 // in a non-Punycode label or U+FFFD itself in a Punycode label.
764 // We also check for dots which can come from the input to a single-label function.
765 // Ok to cast away const because we own the UnicodeString.
766 UChar
*s
=(UChar
*)label
;
767 const UChar
*limit
=label
+labelLength
;
769 // If we enforce STD3 rules, then ASCII characters other than LDH and dot are disallowed.
770 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
775 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
777 } else if(disallowNonLDHDot
&& asciiData
[c
]<0) {
778 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
783 if(disallowNonLDHDot
&& isNonASCIIDisallowedSTD3Valid(c
)) {
784 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
786 } else if(c
==0xfffd) {
787 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
792 // Check for a leading combining mark after other validity checks
793 // so that we don't report UIDNA_ERROR_DISALLOWED for the U+FFFD from here.
796 // "Unsafe" is ok because unpaired surrogates were mapped to U+FFFD.
797 U16_NEXT_UNSAFE(label
, cpLength
, c
);
798 if((U_GET_GC_MASK(c
)&U_GC_M_MASK
)!=0) {
799 info
.labelErrors
|=UIDNA_ERROR_LEADING_COMBINING_MARK
;
800 labelString
->replace(labelStart
, cpLength
, (UChar
)0xfffd);
801 label
=labelString
->getBuffer()+labelStart
;
802 labelLength
+=1-cpLength
;
803 if(labelString
==&dest
) {
804 destLabelLength
=labelLength
;
807 if((info
.labelErrors
&severeErrors
)==0) {
808 // Do contextual checks only if we do not have U+FFFD from a severe error
809 // because U+FFFD can make these checks fail.
810 if((options
&UIDNA_CHECK_BIDI
)!=0 && (!info
.isBiDi
|| info
.isOkBiDi
)) {
811 checkLabelBiDi(label
, labelLength
, info
);
813 if( (options
&UIDNA_CHECK_CONTEXTJ
)!=0 && (oredChars
&0x200c)==0x200c &&
814 !isLabelOkContextJ(label
, labelLength
)
816 info
.labelErrors
|=UIDNA_ERROR_CONTEXTJ
;
818 if((options
&UIDNA_CHECK_CONTEXTO
)!=0 && oredChars
>=0xb7) {
819 checkLabelContextO(label
, labelLength
, info
);
823 // Leave a Punycode label unchanged if it has no severe errors.
824 if(destLabelLength
>63) {
825 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
827 return destLabelLength
;
828 } else if(oredChars
>=0x80) {
829 // Contains non-ASCII characters.
830 UnicodeString punycode
;
831 UChar
*buffer
=punycode
.getBuffer(63); // 63==maximum DNS label length
833 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
834 return destLabelLength
;
836 buffer
[0]=0x78; // Write "xn--".
840 int32_t punycodeLength
=u_strToPunycode(label
, labelLength
,
841 buffer
+4, punycode
.getCapacity()-4,
843 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
844 errorCode
=U_ZERO_ERROR
;
845 punycode
.releaseBuffer(4);
846 buffer
=punycode
.getBuffer(4+punycodeLength
);
848 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
849 return destLabelLength
;
851 punycodeLength
=u_strToPunycode(label
, labelLength
,
852 buffer
+4, punycode
.getCapacity()-4,
856 punycode
.releaseBuffer(punycodeLength
);
857 if(U_FAILURE(errorCode
)) {
858 return destLabelLength
;
860 if(punycodeLength
>63) {
861 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
863 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
864 punycode
, punycodeLength
);
868 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
873 // If a Punycode label has severe errors,
874 // then leave it but make sure it does not look valid.
876 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
877 return markBadACELabel(dest
, destLabelStart
, destLabelLength
, toASCII
, info
);
880 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
883 // Make sure an ACE label does not look valid.
884 // Append U+FFFD if the label has only LDH characters.
885 // If UIDNA_USE_STD3_RULES, also replace disallowed ASCII characters with U+FFFD.
887 UTS46::markBadACELabel(UnicodeString
&dest
,
888 int32_t labelStart
, int32_t labelLength
,
889 UBool toASCII
, IDNAInfo
&info
) const {
890 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
893 const UChar
*label
=dest
.getBuffer()+labelStart
;
894 // Ok to cast away const because we own the UnicodeString.
895 UChar
*s
=(UChar
*)label
+4; // After the initial "xn--".
896 const UChar
*limit
=label
+labelLength
;
901 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
903 isASCII
=onlyLDH
=FALSE
;
904 } else if(asciiData
[c
]<0) {
906 if(disallowNonLDHDot
) {
912 isASCII
=onlyLDH
=FALSE
;
916 dest
.insert(labelStart
+labelLength
, (UChar
)0xfffd);
919 if(toASCII
&& isASCII
&& labelLength
>63) {
920 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
926 const uint32_t L_MASK
=U_MASK(U_LEFT_TO_RIGHT
);
927 const uint32_t R_AL_MASK
=U_MASK(U_RIGHT_TO_LEFT
)|U_MASK(U_RIGHT_TO_LEFT_ARABIC
);
928 const uint32_t L_R_AL_MASK
=L_MASK
|R_AL_MASK
;
930 const uint32_t R_AL_AN_MASK
=R_AL_MASK
|U_MASK(U_ARABIC_NUMBER
);
932 const uint32_t EN_AN_MASK
=U_MASK(U_EUROPEAN_NUMBER
)|U_MASK(U_ARABIC_NUMBER
);
933 const uint32_t R_AL_EN_AN_MASK
=R_AL_MASK
|EN_AN_MASK
;
934 const uint32_t L_EN_MASK
=L_MASK
|U_MASK(U_EUROPEAN_NUMBER
);
936 const uint32_t ES_CS_ET_ON_BN_NSM_MASK
=
937 U_MASK(U_EUROPEAN_NUMBER_SEPARATOR
)|
938 U_MASK(U_COMMON_NUMBER_SEPARATOR
)|
939 U_MASK(U_EUROPEAN_NUMBER_TERMINATOR
)|
940 U_MASK(U_OTHER_NEUTRAL
)|
941 U_MASK(U_BOUNDARY_NEUTRAL
)|
942 U_MASK(U_DIR_NON_SPACING_MARK
);
943 const uint32_t L_EN_ES_CS_ET_ON_BN_NSM_MASK
=L_EN_MASK
|ES_CS_ET_ON_BN_NSM_MASK
;
944 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
;
946 // We scan the whole label and check both for whether it contains RTL characters
947 // and whether it passes the BiDi Rule.
948 // In a BiDi domain name, all labels must pass the BiDi Rule, but we might find
949 // that a domain name is a BiDi domain name (has an RTL label) only after
950 // processing several earlier labels.
952 UTS46::checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
953 // IDNA2008 BiDi rule
954 // Get the directionality of the first character.
957 U16_NEXT_UNSAFE(label
, i
, c
);
958 uint32_t firstMask
=U_MASK(u_charDirection(c
));
959 // 1. The first character must be a character with BIDI property L, R
960 // or AL. If it has the R or AL property, it is an RTL label; if it
961 // has the L property, it is an LTR label.
962 if((firstMask
&~L_R_AL_MASK
)!=0) {
965 // Get the directionality of the last non-NSM character.
972 U16_PREV_UNSAFE(label
, labelLength
, c
);
973 UCharDirection dir
=u_charDirection(c
);
974 if(dir
!=U_DIR_NON_SPACING_MARK
) {
975 lastMask
=U_MASK(dir
);
979 // 3. In an RTL label, the end of the label must be a character with
980 // BIDI property R, AL, EN or AN, followed by zero or more
981 // characters with BIDI property NSM.
982 // 6. In an LTR label, the end of the label must be a character with
983 // BIDI property L or EN, followed by zero or more characters with
984 // BIDI property NSM.
985 if( (firstMask
&L_MASK
)!=0 ?
986 (lastMask
&~L_EN_MASK
)!=0 :
987 (lastMask
&~R_AL_EN_AN_MASK
)!=0
991 // Get the directionalities of the intervening characters.
993 while(i
<labelLength
) {
994 U16_NEXT_UNSAFE(label
, i
, c
);
995 mask
|=U_MASK(u_charDirection(c
));
997 if(firstMask
&L_MASK
) {
998 // 5. In an LTR label, only characters with the BIDI properties L, EN,
999 // ES, CS, ET, ON, BN and NSM are allowed.
1000 if((mask
&~L_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1001 info
.isOkBiDi
=FALSE
;
1004 // 2. In an RTL label, only characters with the BIDI properties R, AL,
1005 // AN, EN, ES, CS, ET, ON, BN and NSM are allowed.
1006 if((mask
&~R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1007 info
.isOkBiDi
=FALSE
;
1009 // 4. In an RTL label, if an EN is present, no AN may be present, and
1011 if((mask
&EN_AN_MASK
)==EN_AN_MASK
) {
1012 info
.isOkBiDi
=FALSE
;
1015 // An RTL label is a label that contains at least one character of type
1016 // R, AL or AN. [...]
1017 // A "BIDI domain name" is a domain name that contains at least one RTL
1019 // The following rule, consisting of six conditions, applies to labels
1020 // in BIDI domain names.
1021 if(((firstMask
|mask
|lastMask
)&R_AL_AN_MASK
)!=0) {
1026 // Special code for the ASCII prefix of a BiDi domain name.
1027 // The ASCII prefix is all-LTR.
1029 // IDNA2008 BiDi rule, parts relevant to ASCII labels:
1030 // 1. The first character must be a character with BIDI property L [...]
1031 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1032 // ES, CS, ET, ON, BN and NSM are allowed.
1033 // 6. In an LTR label, the end of the label must be a character with
1034 // BIDI property L or EN [...]
1036 // UTF-16 version, called for mapped ASCII prefix.
1037 // Cannot contain uppercase A-Z.
1038 // s[length-1] must be the trailing dot.
1040 isASCIIOkBiDi(const UChar
*s
, int32_t length
) {
1041 int32_t labelStart
=0;
1042 for(int32_t i
=0; i
<length
; ++i
) {
1044 if(c
==0x2e) { // dot
1047 if(!(0x61<=c
&& c
<=0x7a) && !(0x30<=c
&& c
<=0x39)) {
1048 // Last character in the label is not an L or EN.
1053 } else if(i
==labelStart
) {
1054 if(!(0x61<=c
&& c
<=0x7a)) {
1055 // First character in the label is not an L.
1059 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1060 // Intermediate character in the label is a B, S or WS.
1068 // UTF-8 version, called for source ASCII prefix.
1069 // Can contain uppercase A-Z.
1070 // s[length-1] must be the trailing dot.
1072 isASCIIOkBiDi(const char *s
, int32_t length
) {
1073 int32_t labelStart
=0;
1074 for(int32_t i
=0; i
<length
; ++i
) {
1076 if(c
==0x2e) { // dot
1079 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a) && !(0x30<=c
&& c
<=0x39)) {
1080 // Last character in the label is not an L or EN.
1085 } else if(i
==labelStart
) {
1086 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a)) {
1087 // First character in the label is not an L.
1091 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1092 // Intermediate character in the label is a B, S or WS.
1101 UTS46::isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const {
1102 const UBiDiProps
*bdp
=ubidi_getSingleton();
1103 // [IDNA2008-Tables]
1104 // 200C..200D ; CONTEXTJ # ZERO WIDTH NON-JOINER..ZERO WIDTH JOINER
1105 for(int32_t i
=0; i
<labelLength
; ++i
) {
1106 if(label
[i
]==0x200c) {
1107 // Appendix A.1. ZERO WIDTH NON-JOINER
1110 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1111 // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
1112 // (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
1118 U16_PREV_UNSAFE(label
, j
, c
);
1119 if(uts46Norm2
.getCombiningClass(c
)==9) {
1122 // check precontext (Joining_Type:{L,D})(Joining_Type:T)*
1124 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1125 if(type
==U_JT_TRANSPARENT
) {
1129 U16_PREV_UNSAFE(label
, j
, c
);
1130 } else if(type
==U_JT_LEFT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1131 break; // precontext fulfilled
1136 // check postcontext (Joining_Type:T)*(Joining_Type:{R,D})
1138 if(j
==labelLength
) {
1141 U16_NEXT_UNSAFE(label
, j
, c
);
1142 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1143 if(type
==U_JT_TRANSPARENT
) {
1144 // just skip this character
1145 } else if(type
==U_JT_RIGHT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1146 break; // postcontext fulfilled
1151 } else if(label
[i
]==0x200d) {
1152 // Appendix A.2. ZERO WIDTH JOINER (U+200D)
1155 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1161 U16_PREV_UNSAFE(label
, j
, c
);
1162 if(uts46Norm2
.getCombiningClass(c
)!=9) {
1171 UTS46::checkLabelContextO(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
1172 int32_t labelEnd
=labelLength
-1; // inclusive
1173 int32_t arabicDigits
=0; // -1 for 066x, +1 for 06Fx
1174 for(int32_t i
=0; i
<=labelEnd
; ++i
) {
1178 } else if(c
<=0x6f9) {
1180 // Appendix A.3. MIDDLE DOT (U+00B7)
1183 // If Before(cp) .eq. U+006C And
1184 // After(cp) .eq. U+006C Then True;
1185 if(!(0<i
&& label
[i
-1]==0x6c &&
1186 i
<labelEnd
&& label
[i
+1]==0x6c)) {
1187 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1189 } else if(c
==0x375) {
1190 // Appendix A.4. GREEK LOWER NUMERAL SIGN (KERAIA) (U+0375)
1193 // If Script(After(cp)) .eq. Greek Then True;
1194 UScriptCode script
=USCRIPT_INVALID_CODE
;
1196 UErrorCode errorCode
=U_ZERO_ERROR
;
1198 U16_NEXT(label
, j
, labelLength
, c
);
1199 script
=uscript_getScript(c
, &errorCode
);
1201 if(script
!=USCRIPT_GREEK
) {
1202 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1204 } else if(c
==0x5f3 || c
==0x5f4) {
1205 // Appendix A.5. HEBREW PUNCTUATION GERESH (U+05F3)
1208 // If Script(Before(cp)) .eq. Hebrew Then True;
1210 // Appendix A.6. HEBREW PUNCTUATION GERSHAYIM (U+05F4)
1213 // If Script(Before(cp)) .eq. Hebrew Then True;
1214 UScriptCode script
=USCRIPT_INVALID_CODE
;
1216 UErrorCode errorCode
=U_ZERO_ERROR
;
1218 U16_PREV(label
, 0, j
, c
);
1219 script
=uscript_getScript(c
, &errorCode
);
1221 if(script
!=USCRIPT_HEBREW
) {
1222 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1224 } else if(0x660<=c
/* && c<=0x6f9 */) {
1225 // Appendix A.8. ARABIC-INDIC DIGITS (0660..0669)
1228 // For All Characters:
1229 // If cp .in. 06F0..06F9 Then False;
1232 // Appendix A.9. EXTENDED ARABIC-INDIC DIGITS (06F0..06F9)
1235 // For All Characters:
1236 // If cp .in. 0660..0669 Then False;
1239 if(arabicDigits
>0) {
1240 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1243 } else if(0x6f0<=c
) {
1244 if(arabicDigits
<0) {
1245 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1250 } else if(c
==0x30fb) {
1251 // Appendix A.7. KATAKANA MIDDLE DOT (U+30FB)
1254 // For All Characters:
1255 // If Script(cp) .in. {Hiragana, Katakana, Han} Then True;
1257 UErrorCode errorCode
=U_ZERO_ERROR
;
1260 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1263 U16_NEXT(label
, j
, labelLength
, c
);
1264 UScriptCode script
=uscript_getScript(c
, &errorCode
);
1265 if(script
==USCRIPT_HIRAGANA
|| script
==USCRIPT_KATAKANA
|| script
==USCRIPT_HAN
) {
1275 // C API ------------------------------------------------------------------- ***
1279 U_CAPI UIDNA
* U_EXPORT2
1280 uidna_openUTS46(uint32_t options
, UErrorCode
*pErrorCode
) {
1281 return reinterpret_cast<UIDNA
*>(IDNA::createUTS46Instance(options
, *pErrorCode
));
1284 U_CAPI
void U_EXPORT2
1285 uidna_close(UIDNA
*idna
) {
1286 delete reinterpret_cast<IDNA
*>(idna
);
1290 checkArgs(const void *label
, int32_t length
,
1291 void *dest
, int32_t capacity
,
1292 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1293 if(U_FAILURE(*pErrorCode
)) {
1296 // sizeof(UIDNAInfo)=16 in the first API version.
1297 if(pInfo
==NULL
|| pInfo
->size
<16) {
1298 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1301 if( (label
==NULL
? length
!=0 : length
<-1) ||
1302 (dest
==NULL
? capacity
!=0 : capacity
<0) ||
1303 (dest
==label
&& label
!=NULL
)
1305 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1308 // Set all *pInfo bytes to 0 except for the size field itself.
1309 uprv_memset(&pInfo
->size
+1, 0, pInfo
->size
-sizeof(pInfo
->size
));
1314 idnaInfoToStruct(IDNAInfo
&info
, UIDNAInfo
*pInfo
) {
1315 pInfo
->isTransitionalDifferent
=info
.isTransitionalDifferent();
1316 pInfo
->errors
=info
.getErrors();
1319 U_CAPI
int32_t U_EXPORT2
1320 uidna_labelToASCII(const UIDNA
*idna
,
1321 const UChar
*label
, int32_t length
,
1322 UChar
*dest
, int32_t capacity
,
1323 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1324 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1327 UnicodeString
src((UBool
)(length
<0), label
, length
);
1328 UnicodeString
destString(dest
, 0, capacity
);
1330 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII(src
, destString
, info
, *pErrorCode
);
1331 idnaInfoToStruct(info
, pInfo
);
1332 return destString
.extract(dest
, capacity
, *pErrorCode
);
1335 U_CAPI
int32_t U_EXPORT2
1336 uidna_labelToUnicode(const UIDNA
*idna
,
1337 const UChar
*label
, int32_t length
,
1338 UChar
*dest
, int32_t capacity
,
1339 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1340 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1343 UnicodeString
src((UBool
)(length
<0), label
, length
);
1344 UnicodeString
destString(dest
, 0, capacity
);
1346 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicode(src
, destString
, info
, *pErrorCode
);
1347 idnaInfoToStruct(info
, pInfo
);
1348 return destString
.extract(dest
, capacity
, *pErrorCode
);
1351 U_CAPI
int32_t U_EXPORT2
1352 uidna_nameToASCII(const UIDNA
*idna
,
1353 const UChar
*name
, int32_t length
,
1354 UChar
*dest
, int32_t capacity
,
1355 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1356 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1359 UnicodeString
src((UBool
)(length
<0), name
, length
);
1360 UnicodeString
destString(dest
, 0, capacity
);
1362 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII(src
, destString
, info
, *pErrorCode
);
1363 idnaInfoToStruct(info
, pInfo
);
1364 return destString
.extract(dest
, capacity
, *pErrorCode
);
1367 U_CAPI
int32_t U_EXPORT2
1368 uidna_nameToUnicode(const UIDNA
*idna
,
1369 const UChar
*name
, int32_t length
,
1370 UChar
*dest
, int32_t capacity
,
1371 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1372 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1375 UnicodeString
src((UBool
)(length
<0), name
, length
);
1376 UnicodeString
destString(dest
, 0, capacity
);
1378 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicode(src
, destString
, info
, *pErrorCode
);
1379 idnaInfoToStruct(info
, pInfo
);
1380 return destString
.extract(dest
, capacity
, *pErrorCode
);
1383 U_CAPI
int32_t U_EXPORT2
1384 uidna_labelToASCII_UTF8(const UIDNA
*idna
,
1385 const char *label
, int32_t length
,
1386 char *dest
, int32_t capacity
,
1387 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1388 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1391 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1392 CheckedArrayByteSink
sink(dest
, capacity
);
1394 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1395 idnaInfoToStruct(info
, pInfo
);
1396 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1399 U_CAPI
int32_t U_EXPORT2
1400 uidna_labelToUnicodeUTF8(const UIDNA
*idna
,
1401 const char *label
, int32_t length
,
1402 char *dest
, int32_t capacity
,
1403 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1404 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1407 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1408 CheckedArrayByteSink
sink(dest
, capacity
);
1410 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1411 idnaInfoToStruct(info
, pInfo
);
1412 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1415 U_CAPI
int32_t U_EXPORT2
1416 uidna_nameToASCII_UTF8(const UIDNA
*idna
,
1417 const char *name
, int32_t length
,
1418 char *dest
, int32_t capacity
,
1419 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1420 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1423 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1424 CheckedArrayByteSink
sink(dest
, capacity
);
1426 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1427 idnaInfoToStruct(info
, pInfo
);
1428 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1431 U_CAPI
int32_t U_EXPORT2
1432 uidna_nameToUnicodeUTF8(const UIDNA
*idna
,
1433 const char *name
, int32_t length
,
1434 char *dest
, int32_t capacity
,
1435 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1436 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1439 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1440 CheckedArrayByteSink
sink(dest
, capacity
);
1442 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1443 idnaInfoToStruct(info
, pInfo
);
1444 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1447 #endif // UCONFIG_NO_IDNA