2 *******************************************************************************
3 * Copyright (C) 2010, 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/ustring.h"
27 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
29 // Note about tests for UIDNA_ERROR_DOMAIN_NAME_TOO_LONG:
31 // The domain name length limit is 255 octets in an internal DNS representation
32 // where the last ("root") label is the empty label
33 // represented by length byte 0 alone.
34 // In a conventional string, this translates to 253 characters, or 254
35 // if there is a trailing dot for the root label.
39 // Severe errors which usually result in a U+FFFD replacement character in the result string.
40 const uint32_t severeErrors
=
41 UIDNA_ERROR_LEADING_COMBINING_MARK
|
42 UIDNA_ERROR_DISALLOWED
|
44 UIDNA_ERROR_LABEL_HAS_DOT
|
45 UIDNA_ERROR_INVALID_ACE_LABEL
;
48 isASCIIString(const UnicodeString
&dest
) {
49 const UChar
*s
=dest
.getBuffer();
50 const UChar
*limit
=s
+dest
.length();
60 isASCIIOkBiDi(const UChar
*s
, int32_t length
);
63 isASCIIOkBiDi(const char *s
, int32_t length
);
65 // IDNA class default implementations -------------------------------------- ***
68 IDNA::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
69 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
70 if(U_SUCCESS(errorCode
)) {
71 UnicodeString destString
;
72 labelToASCII(UnicodeString::fromUTF8(label
), destString
,
73 info
, errorCode
).toUTF8(dest
);
78 IDNA::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
79 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
80 if(U_SUCCESS(errorCode
)) {
81 UnicodeString destString
;
82 labelToUnicode(UnicodeString::fromUTF8(label
), destString
,
83 info
, errorCode
).toUTF8(dest
);
88 IDNA::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
89 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
90 if(U_SUCCESS(errorCode
)) {
91 UnicodeString destString
;
92 nameToASCII(UnicodeString::fromUTF8(name
), destString
,
93 info
, errorCode
).toUTF8(dest
);
98 IDNA::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
99 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
100 if(U_SUCCESS(errorCode
)) {
101 UnicodeString destString
;
102 nameToUnicode(UnicodeString::fromUTF8(name
), destString
,
103 info
, errorCode
).toUTF8(dest
);
107 UOBJECT_DEFINE_NO_RTTI_IMPLEMENTATION(IDNA
)
109 // UTS46 class declaration ------------------------------------------------- ***
111 class UTS46
: public IDNA
{
113 UTS46(uint32_t options
, UErrorCode
&errorCode
);
116 virtual UnicodeString
&
117 labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
118 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
120 virtual UnicodeString
&
121 labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
122 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
124 virtual UnicodeString
&
125 nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
126 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
128 virtual UnicodeString
&
129 nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
130 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
133 labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
134 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
137 labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
138 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
141 nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
142 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
145 nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
146 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
150 process(const UnicodeString
&src
,
151 UBool isLabel
, UBool toASCII
,
153 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
156 processUTF8(const StringPiece
&src
,
157 UBool isLabel
, UBool toASCII
,
159 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
162 processUnicode(const UnicodeString
&src
,
163 int32_t labelStart
, int32_t mappingStart
,
164 UBool isLabel
, UBool toASCII
,
166 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
168 // returns the new dest.length()
170 mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
171 UErrorCode
&errorCode
) const;
173 // returns the new label length
175 processLabel(UnicodeString
&dest
,
176 int32_t labelStart
, int32_t labelLength
,
178 IDNAInfo
&info
, UErrorCode
&errorCode
) const;
180 markBadACELabel(UnicodeString
&dest
,
181 int32_t labelStart
, int32_t labelLength
,
182 UBool toASCII
, IDNAInfo
&info
) const;
185 checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const;
188 isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const;
190 const Normalizer2
&uts46Norm2
; // uts46.nrm
195 IDNA::createUTS46Instance(uint32_t options
, UErrorCode
&errorCode
) {
196 if(U_SUCCESS(errorCode
)) {
197 IDNA
*idna
=new UTS46(options
, errorCode
);
199 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
200 } else if(U_FAILURE(errorCode
)) {
210 // UTS46 implementation ---------------------------------------------------- ***
212 UTS46::UTS46(uint32_t opt
, UErrorCode
&errorCode
)
213 : uts46Norm2(*Normalizer2::getInstance(NULL
, "uts46", UNORM2_COMPOSE
, errorCode
)),
219 UTS46::labelToASCII(const UnicodeString
&label
, UnicodeString
&dest
,
220 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
221 return process(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
225 UTS46::labelToUnicode(const UnicodeString
&label
, UnicodeString
&dest
,
226 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
227 return process(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
231 UTS46::nameToASCII(const UnicodeString
&name
, UnicodeString
&dest
,
232 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
233 process(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
234 if( dest
.length()>=254 && (info
.errors
&UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
)==0 &&
235 isASCIIString(dest
) &&
236 (dest
.length()>254 || dest
[253]!=0x2e)
238 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
244 UTS46::nameToUnicode(const UnicodeString
&name
, UnicodeString
&dest
,
245 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
246 return process(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
250 UTS46::labelToASCII_UTF8(const StringPiece
&label
, ByteSink
&dest
,
251 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
252 processUTF8(label
, TRUE
, TRUE
, dest
, info
, errorCode
);
256 UTS46::labelToUnicodeUTF8(const StringPiece
&label
, ByteSink
&dest
,
257 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
258 processUTF8(label
, TRUE
, FALSE
, dest
, info
, errorCode
);
262 UTS46::nameToASCII_UTF8(const StringPiece
&name
, ByteSink
&dest
,
263 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
264 processUTF8(name
, FALSE
, TRUE
, dest
, info
, errorCode
);
268 UTS46::nameToUnicodeUTF8(const StringPiece
&name
, ByteSink
&dest
,
269 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
270 processUTF8(name
, FALSE
, FALSE
, dest
, info
, errorCode
);
273 // UTS #46 data for ASCII characters.
274 // The normalizer (using uts46.nrm) maps uppercase ASCII letters to lowercase
275 // and passes through all other ASCII characters.
276 // If UIDNA_USE_STD3_RULES is set, then non-LDH characters are disallowed
278 // The ASCII fastpath also uses this data.
279 // Values: -1=disallowed 0==valid 1==mapped (lowercase)
280 static const int8_t asciiData
[128]={
281 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
282 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
283 // 002D..002E; valid # HYPHEN-MINUS..FULL STOP
284 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1,
285 // 0030..0039; valid # DIGIT ZERO..DIGIT NINE
286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
287 // 0041..005A; mapped # LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z
288 -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
289 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
290 // 0061..007A; valid # LATIN SMALL LETTER A..LATIN SMALL LETTER Z
291 -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1
296 UTS46::process(const UnicodeString
&src
,
297 UBool isLabel
, UBool toASCII
,
299 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
300 // uts46Norm2.normalize() would do all of this error checking and setup,
301 // but with the ASCII fastpath we do not always call it, and do not
303 if(U_FAILURE(errorCode
)) {
307 const UChar
*srcArray
=src
.getBuffer();
308 if(&dest
==&src
|| srcArray
==NULL
) {
309 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
313 // Arguments are fine, reset output values.
316 int32_t srcLength
=src
.length();
319 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
323 UChar
*destArray
=dest
.getBuffer(srcLength
);
324 if(destArray
==NULL
) {
325 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
329 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
330 int32_t labelStart
=0;
335 if((i
-labelStart
)>63) {
336 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
338 // There is a trailing dot if labelStart==i.
339 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
340 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
343 info
.errors
|=info
.labelErrors
;
344 dest
.releaseBuffer(i
);
351 int cData
=asciiData
[c
];
353 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
354 } else if(cData
<0 && disallowNonLDHDot
) {
355 break; // Replacing with U+FFFD can be complicated for toASCII.
358 if(c
==0x2d) { // hyphen
359 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
360 // "??--..." is Punycode or forbidden.
361 ++i
; // '-' was copied to dest already
365 // label starts with "-"
366 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
368 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
369 // label ends with "-"
370 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
372 } else if(c
==0x2e) { // dot
374 // Replacing with U+FFFD can be complicated for toASCII.
375 ++i
; // '.' was copied to dest already
379 // Permit an empty label at the end but not elsewhere.
380 if(i
==labelStart
&& i
<(srcLength
-1)) {
381 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
382 } else if((i
-labelStart
)>63) {
383 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.
421 info
.errors
|=UIDNA_ERROR_EMPTY_LABEL
;
426 UnicodeString destString
;
427 int32_t labelStart
=0;
428 if(srcLength
<=256) { // length of stackArray[]
430 char stackArray
[256];
431 int32_t destCapacity
;
432 char *destArray
=dest
.GetAppendBuffer(srcLength
, srcLength
+20,
433 stackArray
, LENGTHOF(stackArray
), &destCapacity
);
434 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
439 if((i
-labelStart
)>63) {
440 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
442 // There is a trailing dot if labelStart==i.
443 if(!isLabel
&& i
>=254 && (i
>254 || labelStart
<i
)) {
444 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
447 info
.errors
|=info
.labelErrors
;
448 dest
.Append(destArray
, i
);
453 if((int8_t)c
<0) { // (uint8_t)c>0x7f
456 int cData
=asciiData
[(int)c
]; // Cast: gcc warns about indexing with a char.
458 destArray
[i
]=c
+0x20; // Lowercase an uppercase ASCII letter.
459 } else if(cData
<0 && disallowNonLDHDot
) {
460 break; // Replacing with U+FFFD can be complicated for toASCII.
463 if(c
==0x2d) { // hyphen
464 if(i
==(labelStart
+3) && srcArray
[i
-1]==0x2d) {
465 // "??--..." is Punycode or forbidden.
469 // label starts with "-"
470 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
472 if((i
+1)==srcLength
|| srcArray
[i
+1]==0x2e) {
473 // label ends with "-"
474 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
476 } else if(c
==0x2e) { // dot
478 break; // Replacing with U+FFFD can be complicated for toASCII.
481 // Permit an empty label at the end but not elsewhere.
482 if(i
==labelStart
&& i
<(srcLength
-1)) {
483 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
484 } else if((i
-labelStart
)>63) {
485 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
488 info
.errors
|=info
.labelErrors
;
494 info
.errors
|=info
.labelErrors
;
495 // Convert the processed ASCII prefix of the current label to UTF-16.
496 int32_t mappingStart
=i
-labelStart
;
497 destString
=UnicodeString::fromUTF8(StringPiece(destArray
+labelStart
, mappingStart
));
498 // Output the previous ASCII labels and process the rest of src in UTF-16.
499 dest
.Append(destArray
, labelStart
);
500 processUnicode(UnicodeString::fromUTF8(StringPiece(src
, labelStart
)), 0, mappingStart
,
502 destString
, info
, errorCode
);
504 // src is too long for the ASCII fastpath implementation.
505 processUnicode(UnicodeString::fromUTF8(src
), 0, 0,
507 destString
, info
, errorCode
);
509 destString
.toUTF8(dest
); // calls dest.Flush()
510 if(toASCII
&& !isLabel
) {
511 // length==labelStart==254 means that there is a trailing dot (ok) and
512 // destString is empty (do not index at 253-labelStart).
513 int32_t length
=labelStart
+destString
.length();
514 if( length
>=254 && isASCIIString(destString
) &&
516 (labelStart
<254 && destString
[253-labelStart
]!=0x2e))
518 info
.errors
|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG
;
521 if( info
.isBiDi
&& U_SUCCESS(errorCode
) && (info
.errors
&severeErrors
)==0 &&
522 (!info
.isOkBiDi
|| (labelStart
>0 && !isASCIIOkBiDi(srcArray
, labelStart
)))
524 info
.errors
|=UIDNA_ERROR_BIDI
;
529 UTS46::processUnicode(const UnicodeString
&src
,
530 int32_t labelStart
, int32_t mappingStart
,
531 UBool isLabel
, UBool toASCII
,
533 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
534 if(mappingStart
==0) {
535 uts46Norm2
.normalize(src
, dest
, errorCode
);
537 uts46Norm2
.normalizeSecondAndAppend(dest
, src
.tempSubString(mappingStart
), errorCode
);
539 if(U_FAILURE(errorCode
)) {
543 toASCII
? (options
&UIDNA_NONTRANSITIONAL_TO_ASCII
)==0 :
544 (options
&UIDNA_NONTRANSITIONAL_TO_UNICODE
)==0;
545 const UChar
*destArray
=dest
.getBuffer();
546 int32_t destLength
=dest
.length();
547 int32_t labelLimit
=labelStart
;
548 while(labelLimit
<destLength
) {
549 UChar c
=destArray
[labelLimit
];
550 if(c
==0x2e && !isLabel
) {
551 int32_t labelLength
=labelLimit
-labelStart
;
552 int32_t newLength
=processLabel(dest
, labelStart
, labelLength
,
553 toASCII
, info
, errorCode
);
554 info
.errors
|=info
.labelErrors
;
556 if(U_FAILURE(errorCode
)) {
559 destArray
=dest
.getBuffer();
560 destLength
+=newLength
-labelLength
;
561 labelLimit
=labelStart
+=newLength
+1;
562 } else if(0xdf<=c
&& c
<=0x200d && (c
==0xdf || c
==0x3c2 || c
>=0x200c)) {
563 info
.isTransDiff
=TRUE
;
565 destLength
=mapDevChars(dest
, labelStart
, labelLimit
, errorCode
);
566 if(U_FAILURE(errorCode
)) {
569 destArray
=dest
.getBuffer();
570 // Do not increment labelLimit in case c was removed.
571 // All deviation characters have been mapped, no need to check for them again.
580 // Permit an empty label at the end (0<labelStart==labelLimit==destLength is ok)
581 // but not an empty label elsewhere nor a completely empty domain name.
582 // processLabel() sets UIDNA_ERROR_EMPTY_LABEL when labelLength==0.
583 if(0==labelStart
|| labelStart
<labelLimit
) {
584 processLabel(dest
, labelStart
, labelLimit
-labelStart
,
585 toASCII
, info
, errorCode
);
586 info
.errors
|=info
.labelErrors
;
592 UTS46::mapDevChars(UnicodeString
&dest
, int32_t labelStart
, int32_t mappingStart
,
593 UErrorCode
&errorCode
) const {
594 int32_t length
=dest
.length();
595 UChar
*s
=dest
.getBuffer(dest
[mappingStart
]==0xdf ? length
+1 : length
);
597 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
600 int32_t capacity
=dest
.getCapacity();
601 UBool didMapDevChars
=FALSE
;
602 int32_t readIndex
=mappingStart
, writeIndex
=mappingStart
;
604 UChar c
=s
[readIndex
++];
607 // Map sharp s to ss.
609 s
[writeIndex
++]=0x73; // Replace sharp s with first s.
610 // Insert second s and account for possible buffer reallocation.
611 if(writeIndex
==readIndex
) {
612 if(length
==capacity
) {
613 dest
.releaseBuffer(length
);
614 s
=dest
.getBuffer(length
+1);
616 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
619 capacity
=dest
.getCapacity();
621 u_memmove(s
+writeIndex
+1, s
+writeIndex
, length
-writeIndex
);
624 s
[writeIndex
++]=0x73;
627 case 0x3c2: // Map final sigma to nonfinal sigma.
629 s
[writeIndex
++]=0x3c3;
631 case 0x200c: // Ignore/remove ZWNJ.
632 case 0x200d: // Ignore/remove ZWJ.
637 // Only really necessary if writeIndex was different from readIndex.
641 } while(writeIndex
<length
);
642 dest
.releaseBuffer(length
);
644 // Mapping deviation characters might have resulted in an un-NFC string.
645 // We could use either the NFC or the UTS #46 normalizer.
646 // By using the UTS #46 normalizer again, we avoid having to load a second .nrm data file.
647 UnicodeString normalized
;
648 uts46Norm2
.normalize(dest
.tempSubString(labelStart
), normalized
, errorCode
);
649 if(U_SUCCESS(errorCode
)) {
650 dest
.replace(labelStart
, 0x7fffffff, normalized
);
651 return dest
.length();
657 // Some non-ASCII characters are equivalent to sequences with
658 // non-LDH ASCII characters. To find them:
659 // grep disallowed_STD3_valid IdnaMappingTable.txt (or uts46.txt)
661 isNonASCIIDisallowedSTD3Valid(UChar32 c
) {
662 return c
==0x2260 || c
==0x226E || c
==0x226F;
665 // Replace the label in dest with the label string, if the label was modified.
666 // If &label==&dest then the label was modified in-place and labelLength
667 // is the new label length, different from label.length().
668 // If &label!=&dest then labelLength==label.length().
669 // Returns labelLength (= the new label length).
671 replaceLabel(UnicodeString
&dest
, int32_t destLabelStart
, int32_t destLabelLength
,
672 const UnicodeString
&label
, int32_t labelLength
) {
674 dest
.replace(destLabelStart
, destLabelLength
, label
);
680 UTS46::processLabel(UnicodeString
&dest
,
681 int32_t labelStart
, int32_t labelLength
,
683 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
684 UnicodeString fromPunycode
;
685 UnicodeString
*labelString
;
686 const UChar
*label
=dest
.getBuffer()+labelStart
;
687 int32_t destLabelStart
=labelStart
;
688 int32_t destLabelLength
=labelLength
;
690 if(labelLength
>=4 && label
[0]==0x78 && label
[1]==0x6e && label
[2]==0x2d && label
[3]==0x2d) {
691 // Label starts with "xn--", try to un-Punycode it.
693 UChar
*unicodeBuffer
=fromPunycode
.getBuffer(-1); // capacity==-1: most labels should fit
694 if(unicodeBuffer
==NULL
) {
695 // Should never occur if we used capacity==-1 which uses the internal buffer.
696 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
699 UErrorCode punycodeErrorCode
=U_ZERO_ERROR
;
700 int32_t unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
701 unicodeBuffer
, fromPunycode
.getCapacity(),
702 NULL
, &punycodeErrorCode
);
703 if(punycodeErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
704 fromPunycode
.releaseBuffer(0);
705 unicodeBuffer
=fromPunycode
.getBuffer(unicodeLength
);
706 if(unicodeBuffer
==NULL
) {
707 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
710 punycodeErrorCode
=U_ZERO_ERROR
;
711 unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
712 unicodeBuffer
, fromPunycode
.getCapacity(),
713 NULL
, &punycodeErrorCode
);
715 fromPunycode
.releaseBuffer(unicodeLength
);
716 if(U_FAILURE(punycodeErrorCode
)) {
717 info
.labelErrors
|=UIDNA_ERROR_PUNYCODE
;
718 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
720 // Check for NFC, and for characters that are not
721 // valid or deviation characters according to the normalizer.
722 // If there is something wrong, then the string will change.
723 // Note that the normalizer passes through non-LDH ASCII and deviation characters.
724 // Deviation characters are ok in Punycode even in transitional processing.
725 // In the code further below, if we find non-LDH ASCII and we have UIDNA_USE_STD3_RULES
726 // then we will set UIDNA_ERROR_INVALID_ACE_LABEL there too.
727 UBool isValid
=uts46Norm2
.isNormalized(fromPunycode
, errorCode
);
728 if(U_FAILURE(errorCode
)) {
732 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
733 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
);
735 labelString
=&fromPunycode
;
736 label
=fromPunycode
.getBuffer();
738 labelLength
=fromPunycode
.length();
746 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
748 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
751 if(labelLength
>=4 && label
[2]==0x2d && label
[3]==0x2d) {
752 // label starts with "??--"
753 info
.labelErrors
|=UIDNA_ERROR_HYPHEN_3_4
;
756 // label starts with "-"
757 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
759 if(label
[labelLength
-1]==0x2d) {
760 // label ends with "-"
761 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
763 // If the label was not a Punycode label, then it was the result of
764 // mapping, normalization and label segmentation.
765 // If the label was in Punycode, then we mapped it again above
766 // and checked its validity.
767 // Now we handle the STD3 restriction to LDH characters (if set)
768 // and we look for U+FFFD which indicates disallowed characters
769 // in a non-Punycode label or U+FFFD itself in a Punycode label.
770 // We also check for dots which can come from the input to a single-label function.
771 // Ok to cast away const because we own the UnicodeString.
772 UChar
*s
=(UChar
*)label
;
773 const UChar
*limit
=label
+labelLength
;
775 // If we enforce STD3 rules, then ASCII characters other than LDH and dot are disallowed.
776 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
781 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
783 } else if(disallowNonLDHDot
&& asciiData
[c
]<0) {
784 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
789 if(disallowNonLDHDot
&& isNonASCIIDisallowedSTD3Valid(c
)) {
790 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
792 } else if(c
==0xfffd) {
793 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
798 // Check for a leading combining mark after other validity checks
799 // so that we don't report UIDNA_ERROR_DISALLOWED for the U+FFFD from here.
802 // "Unsafe" is ok because unpaired surrogates were mapped to U+FFFD.
803 U16_NEXT_UNSAFE(label
, cpLength
, c
);
804 if((U_GET_GC_MASK(c
)&U_GC_M_MASK
)!=0) {
805 info
.labelErrors
|=UIDNA_ERROR_LEADING_COMBINING_MARK
;
806 labelString
->replace(labelStart
, cpLength
, (UChar
)0xfffd);
807 label
=labelString
->getBuffer()+labelStart
;
808 labelLength
+=1-cpLength
;
809 if(labelString
==&dest
) {
810 destLabelLength
=labelLength
;
813 if((info
.labelErrors
&severeErrors
)==0) {
814 // Do contextual checks only if we do not have U+FFFD from a severe error
815 // because U+FFFD can make these checks fail.
816 if((options
&UIDNA_CHECK_BIDI
)!=0 && (!info
.isBiDi
|| info
.isOkBiDi
)) {
817 checkLabelBiDi(label
, labelLength
, info
);
819 if( (options
&UIDNA_CHECK_CONTEXTJ
)!=0 && (oredChars
&0x200c)==0x200c &&
820 !isLabelOkContextJ(label
, labelLength
)
822 info
.labelErrors
|=UIDNA_ERROR_CONTEXTJ
;
826 // Leave a Punycode label unchanged if it has no severe errors.
827 if(destLabelLength
>63) {
828 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
830 return destLabelLength
;
831 } else if(oredChars
>=0x80) {
832 // Contains non-ASCII characters.
833 UnicodeString punycode
;
834 UChar
*buffer
=punycode
.getBuffer(63); // 63==maximum DNS label length
836 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
837 return destLabelLength
;
839 buffer
[0]=0x78; // Write "xn--".
843 int32_t punycodeLength
=u_strToPunycode(label
, labelLength
,
844 buffer
+4, punycode
.getCapacity()-4,
846 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
847 errorCode
=U_ZERO_ERROR
;
848 punycode
.releaseBuffer(4);
849 buffer
=punycode
.getBuffer(4+punycodeLength
);
851 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
852 return destLabelLength
;
854 punycodeLength
=u_strToPunycode(label
, labelLength
,
855 buffer
+4, punycode
.getCapacity()-4,
859 punycode
.releaseBuffer(punycodeLength
);
860 if(U_FAILURE(errorCode
)) {
861 return destLabelLength
;
863 if(punycodeLength
>63) {
864 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
866 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
867 punycode
, punycodeLength
);
871 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
876 // If a Punycode label has severe errors,
877 // then leave it but make sure it does not look valid.
879 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
880 return markBadACELabel(dest
, destLabelStart
, destLabelLength
, toASCII
, info
);
883 return replaceLabel(dest
, destLabelStart
, destLabelLength
, *labelString
, labelLength
);
886 // Make sure an ACE label does not look valid.
887 // Append U+FFFD if the label has only LDH characters.
888 // If UIDNA_USE_STD3_RULES, also replace disallowed ASCII characters with U+FFFD.
890 UTS46::markBadACELabel(UnicodeString
&dest
,
891 int32_t labelStart
, int32_t labelLength
,
892 UBool toASCII
, IDNAInfo
&info
) const {
893 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
896 const UChar
*label
=dest
.getBuffer()+labelStart
;
897 // Ok to cast away const because we own the UnicodeString.
898 UChar
*s
=(UChar
*)label
+4; // After the initial "xn--".
899 const UChar
*limit
=label
+labelLength
;
904 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
906 isASCII
=onlyLDH
=FALSE
;
907 } else if(asciiData
[c
]<0) {
909 if(disallowNonLDHDot
) {
915 isASCII
=onlyLDH
=FALSE
;
919 dest
.insert(labelStart
+labelLength
, (UChar
)0xfffd);
922 if(toASCII
&& isASCII
&& labelLength
>63) {
923 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
929 const uint32_t L_MASK
=U_MASK(U_LEFT_TO_RIGHT
);
930 const uint32_t R_AL_MASK
=U_MASK(U_RIGHT_TO_LEFT
)|U_MASK(U_RIGHT_TO_LEFT_ARABIC
);
931 const uint32_t L_R_AL_MASK
=L_MASK
|R_AL_MASK
;
933 const uint32_t R_AL_AN_MASK
=R_AL_MASK
|U_MASK(U_ARABIC_NUMBER
);
935 const uint32_t EN_AN_MASK
=U_MASK(U_EUROPEAN_NUMBER
)|U_MASK(U_ARABIC_NUMBER
);
936 const uint32_t R_AL_EN_AN_MASK
=R_AL_MASK
|EN_AN_MASK
;
937 const uint32_t L_EN_MASK
=L_MASK
|U_MASK(U_EUROPEAN_NUMBER
);
939 const uint32_t ES_CS_ET_ON_BN_NSM_MASK
=
940 U_MASK(U_EUROPEAN_NUMBER_SEPARATOR
)|
941 U_MASK(U_COMMON_NUMBER_SEPARATOR
)|
942 U_MASK(U_EUROPEAN_NUMBER_TERMINATOR
)|
943 U_MASK(U_OTHER_NEUTRAL
)|
944 U_MASK(U_BOUNDARY_NEUTRAL
)|
945 U_MASK(U_DIR_NON_SPACING_MARK
);
946 const uint32_t L_EN_ES_CS_ET_ON_BN_NSM_MASK
=L_EN_MASK
|ES_CS_ET_ON_BN_NSM_MASK
;
947 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
;
949 // We scan the whole label and check both for whether it contains RTL characters
950 // and whether it passes the BiDi Rule.
951 // In a BiDi domain name, all labels must pass the BiDi Rule, but we might find
952 // that a domain name is a BiDi domain name (has an RTL label) only after
953 // processing several earlier labels.
955 UTS46::checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
956 // IDNA2008 BiDi rule
957 // Get the directionality of the first character.
960 U16_NEXT_UNSAFE(label
, i
, c
);
961 uint32_t firstMask
=U_MASK(u_charDirection(c
));
962 // 1. The first character must be a character with BIDI property L, R
963 // or AL. If it has the R or AL property, it is an RTL label; if it
964 // has the L property, it is an LTR label.
965 if((firstMask
&~L_R_AL_MASK
)!=0) {
968 // Get the directionality of the last non-NSM character.
975 U16_PREV_UNSAFE(label
, labelLength
, c
);
976 UCharDirection dir
=u_charDirection(c
);
977 if(dir
!=U_DIR_NON_SPACING_MARK
) {
978 lastMask
=U_MASK(dir
);
982 // 3. In an RTL label, the end of the label must be a character with
983 // BIDI property R, AL, EN or AN, followed by zero or more
984 // characters with BIDI property NSM.
985 // 6. In an LTR label, the end of the label must be a character with
986 // BIDI property L or EN, followed by zero or more characters with
987 // BIDI property NSM.
988 if( (firstMask
&L_MASK
)!=0 ?
989 (lastMask
&~L_EN_MASK
)!=0 :
990 (lastMask
&~R_AL_EN_AN_MASK
)!=0
994 // Get the directionalities of the intervening characters.
996 while(i
<labelLength
) {
997 U16_NEXT_UNSAFE(label
, i
, c
);
998 mask
|=U_MASK(u_charDirection(c
));
1000 if(firstMask
&L_MASK
) {
1001 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1002 // ES, CS, ET, ON, BN and NSM are allowed.
1003 if((mask
&~L_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1004 info
.isOkBiDi
=FALSE
;
1007 // 2. In an RTL label, only characters with the BIDI properties R, AL,
1008 // AN, EN, ES, CS, ET, ON, BN and NSM are allowed.
1009 if((mask
&~R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1010 info
.isOkBiDi
=FALSE
;
1012 // 4. In an RTL label, if an EN is present, no AN may be present, and
1014 if((mask
&EN_AN_MASK
)==EN_AN_MASK
) {
1015 info
.isOkBiDi
=FALSE
;
1018 // An RTL label is a label that contains at least one character of type
1019 // R, AL or AN. [...]
1020 // A "BIDI domain name" is a domain name that contains at least one RTL
1022 // The following rule, consisting of six conditions, applies to labels
1023 // in BIDI domain names.
1024 if(((firstMask
|mask
|lastMask
)&R_AL_AN_MASK
)!=0) {
1029 // Special code for the ASCII prefix of a BiDi domain name.
1030 // The ASCII prefix is all-LTR.
1032 // IDNA2008 BiDi rule, parts relevant to ASCII labels:
1033 // 1. The first character must be a character with BIDI property L [...]
1034 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1035 // ES, CS, ET, ON, BN and NSM are allowed.
1036 // 6. In an LTR label, the end of the label must be a character with
1037 // BIDI property L or EN [...]
1039 // UTF-16 version, called for mapped ASCII prefix.
1040 // Cannot contain uppercase A-Z.
1041 // s[length-1] must be the trailing dot.
1043 isASCIIOkBiDi(const UChar
*s
, int32_t length
) {
1044 int32_t labelStart
=0;
1045 for(int32_t i
=0; i
<length
; ++i
) {
1047 if(c
==0x2e) { // dot
1050 if(!(0x61<=c
&& c
<=0x7a) && !(0x30<=c
&& c
<=0x39)) {
1051 // Last character in the label is not an L or EN.
1056 } else if(i
==labelStart
) {
1057 if(!(0x61<=c
&& c
<=0x7a)) {
1058 // First character in the label is not an L.
1062 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1063 // Intermediate character in the label is a B, S or WS.
1071 // UTF-8 version, called for source ASCII prefix.
1072 // Can contain uppercase A-Z.
1073 // s[length-1] must be the trailing dot.
1075 isASCIIOkBiDi(const char *s
, int32_t length
) {
1076 int32_t labelStart
=0;
1077 for(int32_t i
=0; i
<length
; ++i
) {
1079 if(c
==0x2e) { // dot
1082 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a) && !(0x30<=c
&& c
<=0x39)) {
1083 // Last character in the label is not an L or EN.
1088 } else if(i
==labelStart
) {
1089 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a)) {
1090 // First character in the label is not an L.
1094 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1095 // Intermediate character in the label is a B, S or WS.
1104 UTS46::isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const {
1105 // [IDNA2008-Tables]
1106 // 200C..200D ; CONTEXTJ # ZERO WIDTH NON-JOINER..ZERO WIDTH JOINER
1107 for(int32_t i
=0; i
<labelLength
; ++i
) {
1108 if(label
[i
]==0x200c) {
1109 // Appendix A.1. ZERO WIDTH NON-JOINER
1112 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1113 // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
1114 // (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
1120 U16_PREV_UNSAFE(label
, j
, c
);
1121 if(u_getCombiningClass(c
)==9) {
1124 // check precontext (Joining_Type:{L,D})(Joining_Type:T)*
1126 UJoiningType type
=(UJoiningType
)u_getIntPropertyValue(c
, UCHAR_JOINING_TYPE
);
1127 if(type
==U_JT_TRANSPARENT
) {
1131 U16_PREV_UNSAFE(label
, j
, c
);
1132 } else if(type
==U_JT_LEFT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1133 break; // precontext fulfilled
1138 // check postcontext (Joining_Type:T)*(Joining_Type:{R,D})
1140 if(j
==labelLength
) {
1143 U16_NEXT_UNSAFE(label
, j
, c
);
1144 UJoiningType type
=(UJoiningType
)u_getIntPropertyValue(c
, UCHAR_JOINING_TYPE
);
1145 if(type
==U_JT_TRANSPARENT
) {
1146 // just skip this character
1147 } else if(type
==U_JT_RIGHT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1148 break; // postcontext fulfilled
1153 } else if(label
[i
]==0x200d) {
1154 // Appendix A.2. ZERO WIDTH JOINER (U+200D)
1157 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1163 U16_PREV_UNSAFE(label
, j
, c
);
1164 if(u_getCombiningClass(c
)!=9) {
1174 // C API ------------------------------------------------------------------- ***
1178 U_DRAFT UIDNA
* U_EXPORT2
1179 uidna_openUTS46(uint32_t options
, UErrorCode
*pErrorCode
) {
1180 return reinterpret_cast<UIDNA
*>(IDNA::createUTS46Instance(options
, *pErrorCode
));
1183 U_DRAFT
void U_EXPORT2
1184 uidna_close(UIDNA
*idna
) {
1185 delete reinterpret_cast<IDNA
*>(idna
);
1189 checkArgs(const void *label
, int32_t length
,
1190 void *dest
, int32_t capacity
,
1191 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1192 if(U_FAILURE(*pErrorCode
)) {
1195 // sizeof(UIDNAInfo)=16 in the first API version.
1196 if(pInfo
==NULL
|| pInfo
->size
<16) {
1197 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1200 if( (label
==NULL
? length
!=0 : length
<-1) ||
1201 (dest
==NULL
? capacity
!=0 : capacity
<0) ||
1202 (dest
==label
&& label
!=NULL
)
1204 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1207 // Set all *pInfo bytes to 0 except for the size field itself.
1208 uprv_memset(&pInfo
->size
+1, 0, pInfo
->size
-sizeof(pInfo
->size
));
1213 idnaInfoToStruct(IDNAInfo
&info
, UIDNAInfo
*pInfo
) {
1214 pInfo
->isTransitionalDifferent
=info
.isTransitionalDifferent();
1215 pInfo
->errors
=info
.getErrors();
1218 U_DRAFT
int32_t U_EXPORT2
1219 uidna_labelToASCII(const UIDNA
*idna
,
1220 const UChar
*label
, int32_t length
,
1221 UChar
*dest
, int32_t capacity
,
1222 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1223 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1226 UnicodeString
src((UBool
)(length
<0), label
, length
);
1227 UnicodeString
destString(dest
, 0, capacity
);
1229 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII(src
, destString
, info
, *pErrorCode
);
1230 idnaInfoToStruct(info
, pInfo
);
1231 return destString
.extract(dest
, capacity
, *pErrorCode
);
1234 U_DRAFT
int32_t U_EXPORT2
1235 uidna_labelToUnicode(const UIDNA
*idna
,
1236 const UChar
*label
, int32_t length
,
1237 UChar
*dest
, int32_t capacity
,
1238 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1239 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1242 UnicodeString
src((UBool
)(length
<0), label
, length
);
1243 UnicodeString
destString(dest
, 0, capacity
);
1245 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicode(src
, destString
, info
, *pErrorCode
);
1246 idnaInfoToStruct(info
, pInfo
);
1247 return destString
.extract(dest
, capacity
, *pErrorCode
);
1250 U_DRAFT
int32_t U_EXPORT2
1251 uidna_nameToASCII(const UIDNA
*idna
,
1252 const UChar
*name
, int32_t length
,
1253 UChar
*dest
, int32_t capacity
,
1254 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1255 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1258 UnicodeString
src((UBool
)(length
<0), name
, length
);
1259 UnicodeString
destString(dest
, 0, capacity
);
1261 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII(src
, destString
, info
, *pErrorCode
);
1262 idnaInfoToStruct(info
, pInfo
);
1263 return destString
.extract(dest
, capacity
, *pErrorCode
);
1266 U_DRAFT
int32_t U_EXPORT2
1267 uidna_nameToUnicode(const UIDNA
*idna
,
1268 const UChar
*name
, int32_t length
,
1269 UChar
*dest
, int32_t capacity
,
1270 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1271 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1274 UnicodeString
src((UBool
)(length
<0), name
, length
);
1275 UnicodeString
destString(dest
, 0, capacity
);
1277 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicode(src
, destString
, info
, *pErrorCode
);
1278 idnaInfoToStruct(info
, pInfo
);
1279 return destString
.extract(dest
, capacity
, *pErrorCode
);
1282 U_DRAFT
int32_t U_EXPORT2
1283 uidna_labelToASCII_UTF8(const UIDNA
*idna
,
1284 const char *label
, int32_t length
,
1285 char *dest
, int32_t capacity
,
1286 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1287 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1290 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1291 CheckedArrayByteSink
sink(dest
, capacity
);
1293 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1294 idnaInfoToStruct(info
, pInfo
);
1295 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1298 U_DRAFT
int32_t U_EXPORT2
1299 uidna_labelToUnicodeUTF8(const UIDNA
*idna
,
1300 const char *label
, int32_t length
,
1301 char *dest
, int32_t capacity
,
1302 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1303 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1306 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1307 CheckedArrayByteSink
sink(dest
, capacity
);
1309 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1310 idnaInfoToStruct(info
, pInfo
);
1311 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1314 U_DRAFT
int32_t U_EXPORT2
1315 uidna_nameToASCII_UTF8(const UIDNA
*idna
,
1316 const char *name
, int32_t length
,
1317 char *dest
, int32_t capacity
,
1318 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1319 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1322 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1323 CheckedArrayByteSink
sink(dest
, capacity
);
1325 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1326 idnaInfoToStruct(info
, pInfo
);
1327 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1330 U_DRAFT
int32_t U_EXPORT2
1331 uidna_nameToUnicodeUTF8(const UIDNA
*idna
,
1332 const char *name
, int32_t length
,
1333 char *dest
, int32_t capacity
,
1334 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1335 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1338 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1339 CheckedArrayByteSink
sink(dest
, capacity
);
1341 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1342 idnaInfoToStruct(info
, pInfo
);
1343 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1346 #endif // UCONFIG_NO_IDNA