2 *******************************************************************************
3 * Copyright (C) 2010-2015, 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
, UErrorCode
&errorCode
) 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 if(U_FAILURE(errorCode
)) {
593 int32_t length
=dest
.length();
594 UChar
*s
=dest
.getBuffer(dest
[mappingStart
]==0xdf ? length
+1 : length
);
596 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
599 int32_t capacity
=dest
.getCapacity();
600 UBool didMapDevChars
=FALSE
;
601 int32_t readIndex
=mappingStart
, writeIndex
=mappingStart
;
603 UChar c
=s
[readIndex
++];
606 // Map sharp s to ss.
608 s
[writeIndex
++]=0x73; // Replace sharp s with first s.
609 // Insert second s and account for possible buffer reallocation.
610 if(writeIndex
==readIndex
) {
611 if(length
==capacity
) {
612 dest
.releaseBuffer(length
);
613 s
=dest
.getBuffer(length
+1);
615 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
618 capacity
=dest
.getCapacity();
620 u_memmove(s
+writeIndex
+1, s
+writeIndex
, length
-writeIndex
);
623 s
[writeIndex
++]=0x73;
626 case 0x3c2: // Map final sigma to nonfinal sigma.
628 s
[writeIndex
++]=0x3c3;
630 case 0x200c: // Ignore/remove ZWNJ.
631 case 0x200d: // Ignore/remove ZWJ.
636 // Only really necessary if writeIndex was different from readIndex.
640 } while(writeIndex
<length
);
641 dest
.releaseBuffer(length
);
643 // Mapping deviation characters might have resulted in an un-NFC string.
644 // We could use either the NFC or the UTS #46 normalizer.
645 // By using the UTS #46 normalizer again, we avoid having to load a second .nrm data file.
646 UnicodeString normalized
;
647 uts46Norm2
.normalize(dest
.tempSubString(labelStart
), normalized
, errorCode
);
648 if(U_SUCCESS(errorCode
)) {
649 dest
.replace(labelStart
, 0x7fffffff, normalized
);
651 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
653 return dest
.length();
659 // Some non-ASCII characters are equivalent to sequences with
660 // non-LDH ASCII characters. To find them:
661 // grep disallowed_STD3_valid IdnaMappingTable.txt (or uts46.txt)
663 isNonASCIIDisallowedSTD3Valid(UChar32 c
) {
664 return c
==0x2260 || c
==0x226E || c
==0x226F;
667 // Replace the label in dest with the label string, if the label was modified.
668 // If &label==&dest then the label was modified in-place and labelLength
669 // is the new label length, different from label.length().
670 // If &label!=&dest then labelLength==label.length().
671 // Returns labelLength (= the new label length).
673 replaceLabel(UnicodeString
&dest
, int32_t destLabelStart
, int32_t destLabelLength
,
674 const UnicodeString
&label
, int32_t labelLength
, UErrorCode
&errorCode
) {
675 if(U_FAILURE(errorCode
)) {
679 dest
.replace(destLabelStart
, destLabelLength
, label
);
681 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
689 UTS46::processLabel(UnicodeString
&dest
,
690 int32_t labelStart
, int32_t labelLength
,
692 IDNAInfo
&info
, UErrorCode
&errorCode
) const {
693 if(U_FAILURE(errorCode
)) {
696 UnicodeString fromPunycode
;
697 UnicodeString
*labelString
;
698 const UChar
*label
=dest
.getBuffer()+labelStart
;
699 int32_t destLabelStart
=labelStart
;
700 int32_t destLabelLength
=labelLength
;
702 if(labelLength
>=4 && label
[0]==0x78 && label
[1]==0x6e && label
[2]==0x2d && label
[3]==0x2d) {
703 // Label starts with "xn--", try to un-Punycode it.
705 UChar
*unicodeBuffer
=fromPunycode
.getBuffer(-1); // capacity==-1: most labels should fit
706 if(unicodeBuffer
==NULL
) {
707 // Should never occur if we used capacity==-1 which uses the internal buffer.
708 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
711 UErrorCode punycodeErrorCode
=U_ZERO_ERROR
;
712 int32_t unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
713 unicodeBuffer
, fromPunycode
.getCapacity(),
714 NULL
, &punycodeErrorCode
);
715 if(punycodeErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
716 fromPunycode
.releaseBuffer(0);
717 unicodeBuffer
=fromPunycode
.getBuffer(unicodeLength
);
718 if(unicodeBuffer
==NULL
) {
719 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
722 punycodeErrorCode
=U_ZERO_ERROR
;
723 unicodeLength
=u_strFromPunycode(label
+4, labelLength
-4,
724 unicodeBuffer
, fromPunycode
.getCapacity(),
725 NULL
, &punycodeErrorCode
);
727 fromPunycode
.releaseBuffer(unicodeLength
);
728 if(U_FAILURE(punycodeErrorCode
)) {
729 info
.labelErrors
|=UIDNA_ERROR_PUNYCODE
;
730 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
, errorCode
);
732 // Check for NFC, and for characters that are not
733 // valid or deviation characters according to the normalizer.
734 // If there is something wrong, then the string will change.
735 // Note that the normalizer passes through non-LDH ASCII and deviation characters.
736 // Deviation characters are ok in Punycode even in transitional processing.
737 // In the code further below, if we find non-LDH ASCII and we have UIDNA_USE_STD3_RULES
738 // then we will set UIDNA_ERROR_INVALID_ACE_LABEL there too.
739 UBool isValid
=uts46Norm2
.isNormalized(fromPunycode
, errorCode
);
740 if(U_FAILURE(errorCode
)) {
744 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
745 return markBadACELabel(dest
, labelStart
, labelLength
, toASCII
, info
, errorCode
);
747 labelString
=&fromPunycode
;
748 label
=fromPunycode
.getBuffer();
750 labelLength
=fromPunycode
.length();
757 info
.labelErrors
|=UIDNA_ERROR_EMPTY_LABEL
;
758 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
759 *labelString
, labelLength
, errorCode
);
762 if(labelLength
>=4 && label
[2]==0x2d && label
[3]==0x2d) {
763 // label starts with "??--"
764 info
.labelErrors
|=UIDNA_ERROR_HYPHEN_3_4
;
767 // label starts with "-"
768 info
.labelErrors
|=UIDNA_ERROR_LEADING_HYPHEN
;
770 if(label
[labelLength
-1]==0x2d) {
771 // label ends with "-"
772 info
.labelErrors
|=UIDNA_ERROR_TRAILING_HYPHEN
;
774 // If the label was not a Punycode label, then it was the result of
775 // mapping, normalization and label segmentation.
776 // If the label was in Punycode, then we mapped it again above
777 // and checked its validity.
778 // Now we handle the STD3 restriction to LDH characters (if set)
779 // and we look for U+FFFD which indicates disallowed characters
780 // in a non-Punycode label or U+FFFD itself in a Punycode label.
781 // We also check for dots which can come from the input to a single-label function.
782 // Ok to cast away const because we own the UnicodeString.
783 UChar
*s
=(UChar
*)label
;
784 const UChar
*limit
=label
+labelLength
;
786 // If we enforce STD3 rules, then ASCII characters other than LDH and dot are disallowed.
787 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
792 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
794 } else if(disallowNonLDHDot
&& asciiData
[c
]<0) {
795 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
800 if(disallowNonLDHDot
&& isNonASCIIDisallowedSTD3Valid(c
)) {
801 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
803 } else if(c
==0xfffd) {
804 info
.labelErrors
|=UIDNA_ERROR_DISALLOWED
;
809 // Check for a leading combining mark after other validity checks
810 // so that we don't report UIDNA_ERROR_DISALLOWED for the U+FFFD from here.
813 // "Unsafe" is ok because unpaired surrogates were mapped to U+FFFD.
814 U16_NEXT_UNSAFE(label
, cpLength
, c
);
815 if((U_GET_GC_MASK(c
)&U_GC_M_MASK
)!=0) {
816 info
.labelErrors
|=UIDNA_ERROR_LEADING_COMBINING_MARK
;
817 labelString
->replace(labelStart
, cpLength
, (UChar
)0xfffd);
818 label
=labelString
->getBuffer()+labelStart
;
819 labelLength
+=1-cpLength
;
820 if(labelString
==&dest
) {
821 destLabelLength
=labelLength
;
824 if((info
.labelErrors
&severeErrors
)==0) {
825 // Do contextual checks only if we do not have U+FFFD from a severe error
826 // because U+FFFD can make these checks fail.
827 if((options
&UIDNA_CHECK_BIDI
)!=0 && (!info
.isBiDi
|| info
.isOkBiDi
)) {
828 checkLabelBiDi(label
, labelLength
, info
);
830 if( (options
&UIDNA_CHECK_CONTEXTJ
)!=0 && (oredChars
&0x200c)==0x200c &&
831 !isLabelOkContextJ(label
, labelLength
)
833 info
.labelErrors
|=UIDNA_ERROR_CONTEXTJ
;
835 if((options
&UIDNA_CHECK_CONTEXTO
)!=0 && oredChars
>=0xb7) {
836 checkLabelContextO(label
, labelLength
, info
);
840 // Leave a Punycode label unchanged if it has no severe errors.
841 if(destLabelLength
>63) {
842 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
844 return destLabelLength
;
845 } else if(oredChars
>=0x80) {
846 // Contains non-ASCII characters.
847 UnicodeString punycode
;
848 UChar
*buffer
=punycode
.getBuffer(63); // 63==maximum DNS label length
850 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
851 return destLabelLength
;
853 buffer
[0]=0x78; // Write "xn--".
857 int32_t punycodeLength
=u_strToPunycode(label
, labelLength
,
858 buffer
+4, punycode
.getCapacity()-4,
860 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
861 errorCode
=U_ZERO_ERROR
;
862 punycode
.releaseBuffer(4);
863 buffer
=punycode
.getBuffer(4+punycodeLength
);
865 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
866 return destLabelLength
;
868 punycodeLength
=u_strToPunycode(label
, labelLength
,
869 buffer
+4, punycode
.getCapacity()-4,
873 punycode
.releaseBuffer(punycodeLength
);
874 if(U_FAILURE(errorCode
)) {
875 return destLabelLength
;
877 if(punycodeLength
>63) {
878 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
880 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
881 punycode
, punycodeLength
, errorCode
);
885 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
890 // If a Punycode label has severe errors,
891 // then leave it but make sure it does not look valid.
893 info
.labelErrors
|=UIDNA_ERROR_INVALID_ACE_LABEL
;
894 return markBadACELabel(dest
, destLabelStart
, destLabelLength
, toASCII
, info
, errorCode
);
897 return replaceLabel(dest
, destLabelStart
, destLabelLength
,
898 *labelString
, labelLength
, errorCode
);
901 // Make sure an ACE label does not look valid.
902 // Append U+FFFD if the label has only LDH characters.
903 // If UIDNA_USE_STD3_RULES, also replace disallowed ASCII characters with U+FFFD.
905 UTS46::markBadACELabel(UnicodeString
&dest
,
906 int32_t labelStart
, int32_t labelLength
,
907 UBool toASCII
, IDNAInfo
&info
, UErrorCode
&errorCode
) const {
908 if(U_FAILURE(errorCode
)) {
911 UBool disallowNonLDHDot
=(options
&UIDNA_USE_STD3_RULES
)!=0;
914 const UChar
*label
=dest
.getBuffer()+labelStart
;
915 // Ok to cast away const because we own the UnicodeString.
916 UChar
*s
=(UChar
*)label
+4; // After the initial "xn--".
917 const UChar
*limit
=label
+labelLength
;
922 info
.labelErrors
|=UIDNA_ERROR_LABEL_HAS_DOT
;
924 isASCII
=onlyLDH
=FALSE
;
925 } else if(asciiData
[c
]<0) {
927 if(disallowNonLDHDot
) {
933 isASCII
=onlyLDH
=FALSE
;
937 dest
.insert(labelStart
+labelLength
, (UChar
)0xfffd);
939 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
944 if(toASCII
&& isASCII
&& labelLength
>63) {
945 info
.labelErrors
|=UIDNA_ERROR_LABEL_TOO_LONG
;
951 const uint32_t L_MASK
=U_MASK(U_LEFT_TO_RIGHT
);
952 const uint32_t R_AL_MASK
=U_MASK(U_RIGHT_TO_LEFT
)|U_MASK(U_RIGHT_TO_LEFT_ARABIC
);
953 const uint32_t L_R_AL_MASK
=L_MASK
|R_AL_MASK
;
955 const uint32_t R_AL_AN_MASK
=R_AL_MASK
|U_MASK(U_ARABIC_NUMBER
);
957 const uint32_t EN_AN_MASK
=U_MASK(U_EUROPEAN_NUMBER
)|U_MASK(U_ARABIC_NUMBER
);
958 const uint32_t R_AL_EN_AN_MASK
=R_AL_MASK
|EN_AN_MASK
;
959 const uint32_t L_EN_MASK
=L_MASK
|U_MASK(U_EUROPEAN_NUMBER
);
961 const uint32_t ES_CS_ET_ON_BN_NSM_MASK
=
962 U_MASK(U_EUROPEAN_NUMBER_SEPARATOR
)|
963 U_MASK(U_COMMON_NUMBER_SEPARATOR
)|
964 U_MASK(U_EUROPEAN_NUMBER_TERMINATOR
)|
965 U_MASK(U_OTHER_NEUTRAL
)|
966 U_MASK(U_BOUNDARY_NEUTRAL
)|
967 U_MASK(U_DIR_NON_SPACING_MARK
);
968 const uint32_t L_EN_ES_CS_ET_ON_BN_NSM_MASK
=L_EN_MASK
|ES_CS_ET_ON_BN_NSM_MASK
;
969 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
;
971 // We scan the whole label and check both for whether it contains RTL characters
972 // and whether it passes the BiDi Rule.
973 // In a BiDi domain name, all labels must pass the BiDi Rule, but we might find
974 // that a domain name is a BiDi domain name (has an RTL label) only after
975 // processing several earlier labels.
977 UTS46::checkLabelBiDi(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
978 // IDNA2008 BiDi rule
979 // Get the directionality of the first character.
982 U16_NEXT_UNSAFE(label
, i
, c
);
983 uint32_t firstMask
=U_MASK(u_charDirection(c
));
984 // 1. The first character must be a character with BIDI property L, R
985 // or AL. If it has the R or AL property, it is an RTL label; if it
986 // has the L property, it is an LTR label.
987 if((firstMask
&~L_R_AL_MASK
)!=0) {
990 // Get the directionality of the last non-NSM character.
997 U16_PREV_UNSAFE(label
, labelLength
, c
);
998 UCharDirection dir
=u_charDirection(c
);
999 if(dir
!=U_DIR_NON_SPACING_MARK
) {
1000 lastMask
=U_MASK(dir
);
1004 // 3. In an RTL label, the end of the label must be a character with
1005 // BIDI property R, AL, EN or AN, followed by zero or more
1006 // characters with BIDI property NSM.
1007 // 6. In an LTR label, the end of the label must be a character with
1008 // BIDI property L or EN, followed by zero or more characters with
1009 // BIDI property NSM.
1010 if( (firstMask
&L_MASK
)!=0 ?
1011 (lastMask
&~L_EN_MASK
)!=0 :
1012 (lastMask
&~R_AL_EN_AN_MASK
)!=0
1014 info
.isOkBiDi
=FALSE
;
1016 // Get the directionalities of the intervening characters.
1018 while(i
<labelLength
) {
1019 U16_NEXT_UNSAFE(label
, i
, c
);
1020 mask
|=U_MASK(u_charDirection(c
));
1022 if(firstMask
&L_MASK
) {
1023 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1024 // ES, CS, ET, ON, BN and NSM are allowed.
1025 if((mask
&~L_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1026 info
.isOkBiDi
=FALSE
;
1029 // 2. In an RTL label, only characters with the BIDI properties R, AL,
1030 // AN, EN, ES, CS, ET, ON, BN and NSM are allowed.
1031 if((mask
&~R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK
)!=0) {
1032 info
.isOkBiDi
=FALSE
;
1034 // 4. In an RTL label, if an EN is present, no AN may be present, and
1036 if((mask
&EN_AN_MASK
)==EN_AN_MASK
) {
1037 info
.isOkBiDi
=FALSE
;
1040 // An RTL label is a label that contains at least one character of type
1041 // R, AL or AN. [...]
1042 // A "BIDI domain name" is a domain name that contains at least one RTL
1044 // The following rule, consisting of six conditions, applies to labels
1045 // in BIDI domain names.
1046 if(((firstMask
|mask
|lastMask
)&R_AL_AN_MASK
)!=0) {
1051 // Special code for the ASCII prefix of a BiDi domain name.
1052 // The ASCII prefix is all-LTR.
1054 // IDNA2008 BiDi rule, parts relevant to ASCII labels:
1055 // 1. The first character must be a character with BIDI property L [...]
1056 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1057 // ES, CS, ET, ON, BN and NSM are allowed.
1058 // 6. In an LTR label, the end of the label must be a character with
1059 // BIDI property L or EN [...]
1061 // UTF-16 version, called for mapped ASCII prefix.
1062 // Cannot contain uppercase A-Z.
1063 // s[length-1] must be the trailing dot.
1065 isASCIIOkBiDi(const UChar
*s
, int32_t length
) {
1066 int32_t labelStart
=0;
1067 for(int32_t i
=0; i
<length
; ++i
) {
1069 if(c
==0x2e) { // dot
1072 if(!(0x61<=c
&& c
<=0x7a) && !(0x30<=c
&& c
<=0x39)) {
1073 // Last character in the label is not an L or EN.
1078 } else if(i
==labelStart
) {
1079 if(!(0x61<=c
&& c
<=0x7a)) {
1080 // First character in the label is not an L.
1084 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1085 // Intermediate character in the label is a B, S or WS.
1093 // UTF-8 version, called for source ASCII prefix.
1094 // Can contain uppercase A-Z.
1095 // s[length-1] must be the trailing dot.
1097 isASCIIOkBiDi(const char *s
, int32_t length
) {
1098 int32_t labelStart
=0;
1099 for(int32_t i
=0; i
<length
; ++i
) {
1101 if(c
==0x2e) { // dot
1104 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a) && !(0x30<=c
&& c
<=0x39)) {
1105 // Last character in the label is not an L or EN.
1110 } else if(i
==labelStart
) {
1111 if(!(0x61<=c
&& c
<=0x7a) && !(0x41<=c
&& c
<=0x5a)) {
1112 // First character in the label is not an L.
1116 if(c
<=0x20 && (c
>=0x1c || (9<=c
&& c
<=0xd))) {
1117 // Intermediate character in the label is a B, S or WS.
1126 UTS46::isLabelOkContextJ(const UChar
*label
, int32_t labelLength
) const {
1127 const UBiDiProps
*bdp
=ubidi_getSingleton();
1128 // [IDNA2008-Tables]
1129 // 200C..200D ; CONTEXTJ # ZERO WIDTH NON-JOINER..ZERO WIDTH JOINER
1130 for(int32_t i
=0; i
<labelLength
; ++i
) {
1131 if(label
[i
]==0x200c) {
1132 // Appendix A.1. ZERO WIDTH NON-JOINER
1135 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1136 // If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
1137 // (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
1143 U16_PREV_UNSAFE(label
, j
, c
);
1144 if(uts46Norm2
.getCombiningClass(c
)==9) {
1147 // check precontext (Joining_Type:{L,D})(Joining_Type:T)*
1149 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1150 if(type
==U_JT_TRANSPARENT
) {
1154 U16_PREV_UNSAFE(label
, j
, c
);
1155 } else if(type
==U_JT_LEFT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1156 break; // precontext fulfilled
1161 // check postcontext (Joining_Type:T)*(Joining_Type:{R,D})
1163 if(j
==labelLength
) {
1166 U16_NEXT_UNSAFE(label
, j
, c
);
1167 UJoiningType type
=ubidi_getJoiningType(bdp
, c
);
1168 if(type
==U_JT_TRANSPARENT
) {
1169 // just skip this character
1170 } else if(type
==U_JT_RIGHT_JOINING
|| type
==U_JT_DUAL_JOINING
) {
1171 break; // postcontext fulfilled
1176 } else if(label
[i
]==0x200d) {
1177 // Appendix A.2. ZERO WIDTH JOINER (U+200D)
1180 // If Canonical_Combining_Class(Before(cp)) .eq. Virama Then True;
1186 U16_PREV_UNSAFE(label
, j
, c
);
1187 if(uts46Norm2
.getCombiningClass(c
)!=9) {
1196 UTS46::checkLabelContextO(const UChar
*label
, int32_t labelLength
, IDNAInfo
&info
) const {
1197 int32_t labelEnd
=labelLength
-1; // inclusive
1198 int32_t arabicDigits
=0; // -1 for 066x, +1 for 06Fx
1199 for(int32_t i
=0; i
<=labelEnd
; ++i
) {
1203 } else if(c
<=0x6f9) {
1205 // Appendix A.3. MIDDLE DOT (U+00B7)
1208 // If Before(cp) .eq. U+006C And
1209 // After(cp) .eq. U+006C Then True;
1210 if(!(0<i
&& label
[i
-1]==0x6c &&
1211 i
<labelEnd
&& label
[i
+1]==0x6c)) {
1212 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1214 } else if(c
==0x375) {
1215 // Appendix A.4. GREEK LOWER NUMERAL SIGN (KERAIA) (U+0375)
1218 // If Script(After(cp)) .eq. Greek Then True;
1219 UScriptCode script
=USCRIPT_INVALID_CODE
;
1221 UErrorCode errorCode
=U_ZERO_ERROR
;
1223 U16_NEXT(label
, j
, labelLength
, c
);
1224 script
=uscript_getScript(c
, &errorCode
);
1226 if(script
!=USCRIPT_GREEK
) {
1227 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1229 } else if(c
==0x5f3 || c
==0x5f4) {
1230 // Appendix A.5. HEBREW PUNCTUATION GERESH (U+05F3)
1233 // If Script(Before(cp)) .eq. Hebrew Then True;
1235 // Appendix A.6. HEBREW PUNCTUATION GERSHAYIM (U+05F4)
1238 // If Script(Before(cp)) .eq. Hebrew Then True;
1239 UScriptCode script
=USCRIPT_INVALID_CODE
;
1241 UErrorCode errorCode
=U_ZERO_ERROR
;
1243 U16_PREV(label
, 0, j
, c
);
1244 script
=uscript_getScript(c
, &errorCode
);
1246 if(script
!=USCRIPT_HEBREW
) {
1247 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1249 } else if(0x660<=c
/* && c<=0x6f9 */) {
1250 // Appendix A.8. ARABIC-INDIC DIGITS (0660..0669)
1253 // For All Characters:
1254 // If cp .in. 06F0..06F9 Then False;
1257 // Appendix A.9. EXTENDED ARABIC-INDIC DIGITS (06F0..06F9)
1260 // For All Characters:
1261 // If cp .in. 0660..0669 Then False;
1264 if(arabicDigits
>0) {
1265 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1268 } else if(0x6f0<=c
) {
1269 if(arabicDigits
<0) {
1270 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_DIGITS
;
1275 } else if(c
==0x30fb) {
1276 // Appendix A.7. KATAKANA MIDDLE DOT (U+30FB)
1279 // For All Characters:
1280 // If Script(cp) .in. {Hiragana, Katakana, Han} Then True;
1282 UErrorCode errorCode
=U_ZERO_ERROR
;
1285 info
.labelErrors
|=UIDNA_ERROR_CONTEXTO_PUNCTUATION
;
1288 U16_NEXT(label
, j
, labelLength
, c
);
1289 UScriptCode script
=uscript_getScript(c
, &errorCode
);
1290 if(script
==USCRIPT_HIRAGANA
|| script
==USCRIPT_KATAKANA
|| script
==USCRIPT_HAN
) {
1300 // C API ------------------------------------------------------------------- ***
1304 U_CAPI UIDNA
* U_EXPORT2
1305 uidna_openUTS46(uint32_t options
, UErrorCode
*pErrorCode
) {
1306 return reinterpret_cast<UIDNA
*>(IDNA::createUTS46Instance(options
, *pErrorCode
));
1309 U_CAPI
void U_EXPORT2
1310 uidna_close(UIDNA
*idna
) {
1311 delete reinterpret_cast<IDNA
*>(idna
);
1315 checkArgs(const void *label
, int32_t length
,
1316 void *dest
, int32_t capacity
,
1317 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1318 if(U_FAILURE(*pErrorCode
)) {
1321 // sizeof(UIDNAInfo)=16 in the first API version.
1322 if(pInfo
==NULL
|| pInfo
->size
<16) {
1323 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1326 if( (label
==NULL
? length
!=0 : length
<-1) ||
1327 (dest
==NULL
? capacity
!=0 : capacity
<0) ||
1328 (dest
==label
&& label
!=NULL
)
1330 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
1333 // Set all *pInfo bytes to 0 except for the size field itself.
1334 uprv_memset(&pInfo
->size
+1, 0, pInfo
->size
-sizeof(pInfo
->size
));
1339 idnaInfoToStruct(IDNAInfo
&info
, UIDNAInfo
*pInfo
) {
1340 pInfo
->isTransitionalDifferent
=info
.isTransitionalDifferent();
1341 pInfo
->errors
=info
.getErrors();
1344 U_CAPI
int32_t U_EXPORT2
1345 uidna_labelToASCII(const UIDNA
*idna
,
1346 const UChar
*label
, int32_t length
,
1347 UChar
*dest
, int32_t capacity
,
1348 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1349 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1352 UnicodeString
src((UBool
)(length
<0), label
, length
);
1353 UnicodeString
destString(dest
, 0, capacity
);
1355 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII(src
, destString
, info
, *pErrorCode
);
1356 idnaInfoToStruct(info
, pInfo
);
1357 return destString
.extract(dest
, capacity
, *pErrorCode
);
1360 U_CAPI
int32_t U_EXPORT2
1361 uidna_labelToUnicode(const UIDNA
*idna
,
1362 const UChar
*label
, int32_t length
,
1363 UChar
*dest
, int32_t capacity
,
1364 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1365 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1368 UnicodeString
src((UBool
)(length
<0), label
, length
);
1369 UnicodeString
destString(dest
, 0, capacity
);
1371 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicode(src
, destString
, info
, *pErrorCode
);
1372 idnaInfoToStruct(info
, pInfo
);
1373 return destString
.extract(dest
, capacity
, *pErrorCode
);
1376 U_CAPI
int32_t U_EXPORT2
1377 uidna_nameToASCII(const UIDNA
*idna
,
1378 const UChar
*name
, int32_t length
,
1379 UChar
*dest
, int32_t capacity
,
1380 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1381 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1384 UnicodeString
src((UBool
)(length
<0), name
, length
);
1385 UnicodeString
destString(dest
, 0, capacity
);
1387 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII(src
, destString
, info
, *pErrorCode
);
1388 idnaInfoToStruct(info
, pInfo
);
1389 return destString
.extract(dest
, capacity
, *pErrorCode
);
1392 U_CAPI
int32_t U_EXPORT2
1393 uidna_nameToUnicode(const UIDNA
*idna
,
1394 const UChar
*name
, int32_t length
,
1395 UChar
*dest
, int32_t capacity
,
1396 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1397 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1400 UnicodeString
src((UBool
)(length
<0), name
, length
);
1401 UnicodeString
destString(dest
, 0, capacity
);
1403 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicode(src
, destString
, info
, *pErrorCode
);
1404 idnaInfoToStruct(info
, pInfo
);
1405 return destString
.extract(dest
, capacity
, *pErrorCode
);
1408 U_CAPI
int32_t U_EXPORT2
1409 uidna_labelToASCII_UTF8(const UIDNA
*idna
,
1410 const char *label
, int32_t length
,
1411 char *dest
, int32_t capacity
,
1412 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1413 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1416 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1417 CheckedArrayByteSink
sink(dest
, capacity
);
1419 reinterpret_cast<const IDNA
*>(idna
)->labelToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1420 idnaInfoToStruct(info
, pInfo
);
1421 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1424 U_CAPI
int32_t U_EXPORT2
1425 uidna_labelToUnicodeUTF8(const UIDNA
*idna
,
1426 const char *label
, int32_t length
,
1427 char *dest
, int32_t capacity
,
1428 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1429 if(!checkArgs(label
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1432 StringPiece
src(label
, length
<0 ? uprv_strlen(label
) : length
);
1433 CheckedArrayByteSink
sink(dest
, capacity
);
1435 reinterpret_cast<const IDNA
*>(idna
)->labelToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1436 idnaInfoToStruct(info
, pInfo
);
1437 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1440 U_CAPI
int32_t U_EXPORT2
1441 uidna_nameToASCII_UTF8(const UIDNA
*idna
,
1442 const char *name
, int32_t length
,
1443 char *dest
, int32_t capacity
,
1444 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1445 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1448 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1449 CheckedArrayByteSink
sink(dest
, capacity
);
1451 reinterpret_cast<const IDNA
*>(idna
)->nameToASCII_UTF8(src
, sink
, info
, *pErrorCode
);
1452 idnaInfoToStruct(info
, pInfo
);
1453 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1456 U_CAPI
int32_t U_EXPORT2
1457 uidna_nameToUnicodeUTF8(const UIDNA
*idna
,
1458 const char *name
, int32_t length
,
1459 char *dest
, int32_t capacity
,
1460 UIDNAInfo
*pInfo
, UErrorCode
*pErrorCode
) {
1461 if(!checkArgs(name
, length
, dest
, capacity
, pInfo
, pErrorCode
)) {
1464 StringPiece
src(name
, length
<0 ? uprv_strlen(name
) : length
);
1465 CheckedArrayByteSink
sink(dest
, capacity
);
1467 reinterpret_cast<const IDNA
*>(idna
)->nameToUnicodeUTF8(src
, sink
, info
, *pErrorCode
);
1468 idnaInfoToStruct(info
, pInfo
);
1469 return u_terminateChars(dest
, capacity
, sink
.NumberOfBytesAppended(), pErrorCode
);
1472 #endif // UCONFIG_NO_IDNA