xref: /aosp_15_r20/external/cronet/third_party/icu/source/common/uts46.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *   Copyright (C) 2010-2015, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 *   file name:  uts46.cpp
9 *   encoding:   UTF-8
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2010mar09
14 *   created by: Markus W. Scherer
15 */
16 
17 #include "unicode/utypes.h"
18 
19 #if !UCONFIG_NO_IDNA
20 
21 #include "unicode/idna.h"
22 #include "unicode/normalizer2.h"
23 #include "unicode/uscript.h"
24 #include "unicode/ustring.h"
25 #include "unicode/utf16.h"
26 #include "cmemory.h"
27 #include "cstring.h"
28 #include "punycode.h"
29 #include "ubidi_props.h"
30 #include "ustr_imp.h"
31 
32 // Note about tests for UIDNA_ERROR_DOMAIN_NAME_TOO_LONG:
33 //
34 // The domain name length limit is 255 octets in an internal DNS representation
35 // where the last ("root") label is the empty label
36 // represented by length byte 0 alone.
37 // In a conventional string, this translates to 253 characters, or 254
38 // if there is a trailing dot for the root label.
39 
40 U_NAMESPACE_BEGIN
41 
42 // Severe errors which usually result in a U+FFFD replacement character in the result string.
43 const uint32_t severeErrors=
44     UIDNA_ERROR_LEADING_COMBINING_MARK|
45     UIDNA_ERROR_DISALLOWED|
46     UIDNA_ERROR_PUNYCODE|
47     UIDNA_ERROR_LABEL_HAS_DOT|
48     UIDNA_ERROR_INVALID_ACE_LABEL;
49 
50 static inline UBool
isASCIIString(const UnicodeString & dest)51 isASCIIString(const UnicodeString &dest) {
52     const char16_t *s=dest.getBuffer();
53     const char16_t *limit=s+dest.length();
54     while(s<limit) {
55         if(*s++>0x7f) {
56             return false;
57         }
58     }
59     return true;
60 }
61 
62 static UBool
63 isASCIIOkBiDi(const char16_t *s, int32_t length);
64 
65 static UBool
66 isASCIIOkBiDi(const char *s, int32_t length);
67 
68 // IDNA class default implementations -------------------------------------- ***
69 
~IDNA()70 IDNA::~IDNA() {}
71 
72 void
labelToASCII_UTF8(StringPiece label,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const73 IDNA::labelToASCII_UTF8(StringPiece label, ByteSink &dest,
74                         IDNAInfo &info, UErrorCode &errorCode) const {
75     if(U_SUCCESS(errorCode)) {
76         UnicodeString destString;
77         labelToASCII(UnicodeString::fromUTF8(label), destString,
78                      info, errorCode).toUTF8(dest);
79     }
80 }
81 
82 void
labelToUnicodeUTF8(StringPiece label,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const83 IDNA::labelToUnicodeUTF8(StringPiece label, ByteSink &dest,
84                          IDNAInfo &info, UErrorCode &errorCode) const {
85     if(U_SUCCESS(errorCode)) {
86         UnicodeString destString;
87         labelToUnicode(UnicodeString::fromUTF8(label), destString,
88                        info, errorCode).toUTF8(dest);
89     }
90 }
91 
92 void
nameToASCII_UTF8(StringPiece name,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const93 IDNA::nameToASCII_UTF8(StringPiece name, ByteSink &dest,
94                        IDNAInfo &info, UErrorCode &errorCode) const {
95     if(U_SUCCESS(errorCode)) {
96         UnicodeString destString;
97         nameToASCII(UnicodeString::fromUTF8(name), destString,
98                     info, errorCode).toUTF8(dest);
99     }
100 }
101 
102 void
nameToUnicodeUTF8(StringPiece name,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const103 IDNA::nameToUnicodeUTF8(StringPiece name, ByteSink &dest,
104                         IDNAInfo &info, UErrorCode &errorCode) const {
105     if(U_SUCCESS(errorCode)) {
106         UnicodeString destString;
107         nameToUnicode(UnicodeString::fromUTF8(name), destString,
108                       info, errorCode).toUTF8(dest);
109     }
110 }
111 
112 // UTS46 class declaration ------------------------------------------------- ***
113 
114 class UTS46 : public IDNA {
115 public:
116     UTS46(uint32_t options, UErrorCode &errorCode);
117     virtual ~UTS46();
118 
119     virtual UnicodeString &
120     labelToASCII(const UnicodeString &label, UnicodeString &dest,
121                  IDNAInfo &info, UErrorCode &errorCode) const override;
122 
123     virtual UnicodeString &
124     labelToUnicode(const UnicodeString &label, UnicodeString &dest,
125                    IDNAInfo &info, UErrorCode &errorCode) const override;
126 
127     virtual UnicodeString &
128     nameToASCII(const UnicodeString &name, UnicodeString &dest,
129                 IDNAInfo &info, UErrorCode &errorCode) const override;
130 
131     virtual UnicodeString &
132     nameToUnicode(const UnicodeString &name, UnicodeString &dest,
133                   IDNAInfo &info, UErrorCode &errorCode) const override;
134 
135     virtual void
136     labelToASCII_UTF8(StringPiece label, ByteSink &dest,
137                       IDNAInfo &info, UErrorCode &errorCode) const override;
138 
139     virtual void
140     labelToUnicodeUTF8(StringPiece label, ByteSink &dest,
141                        IDNAInfo &info, UErrorCode &errorCode) const override;
142 
143     virtual void
144     nameToASCII_UTF8(StringPiece name, ByteSink &dest,
145                      IDNAInfo &info, UErrorCode &errorCode) const override;
146 
147     virtual void
148     nameToUnicodeUTF8(StringPiece name, ByteSink &dest,
149                       IDNAInfo &info, UErrorCode &errorCode) const override;
150 
151 private:
152     UnicodeString &
153     process(const UnicodeString &src,
154             UBool isLabel, UBool toASCII,
155             UnicodeString &dest,
156             IDNAInfo &info, UErrorCode &errorCode) const;
157 
158     void
159     processUTF8(StringPiece src,
160                 UBool isLabel, UBool toASCII,
161                 ByteSink &dest,
162                 IDNAInfo &info, UErrorCode &errorCode) const;
163 
164     UnicodeString &
165     processUnicode(const UnicodeString &src,
166                    int32_t labelStart, int32_t mappingStart,
167                    UBool isLabel, UBool toASCII,
168                    UnicodeString &dest,
169                    IDNAInfo &info, UErrorCode &errorCode) const;
170 
171     // returns the new dest.length()
172     int32_t
173     mapDevChars(UnicodeString &dest, int32_t labelStart, int32_t mappingStart,
174                 UErrorCode &errorCode) const;
175 
176     // returns the new label length
177     int32_t
178     processLabel(UnicodeString &dest,
179                  int32_t labelStart, int32_t labelLength,
180                  UBool toASCII,
181                  IDNAInfo &info, UErrorCode &errorCode) const;
182     int32_t
183     markBadACELabel(UnicodeString &dest,
184                     int32_t labelStart, int32_t labelLength,
185                     UBool toASCII, IDNAInfo &info, UErrorCode &errorCode) const;
186 
187     void
188     checkLabelBiDi(const char16_t *label, int32_t labelLength, IDNAInfo &info) const;
189 
190     UBool
191     isLabelOkContextJ(const char16_t *label, int32_t labelLength) const;
192 
193     void
194     checkLabelContextO(const char16_t *label, int32_t labelLength, IDNAInfo &info) const;
195 
196     const Normalizer2 &uts46Norm2;  // uts46.nrm
197     uint32_t options;
198 };
199 
200 IDNA *
createUTS46Instance(uint32_t options,UErrorCode & errorCode)201 IDNA::createUTS46Instance(uint32_t options, UErrorCode &errorCode) {
202     if(U_SUCCESS(errorCode)) {
203         IDNA *idna=new UTS46(options, errorCode);
204         if(idna==nullptr) {
205             errorCode=U_MEMORY_ALLOCATION_ERROR;
206         } else if(U_FAILURE(errorCode)) {
207             delete idna;
208             idna=nullptr;
209         }
210         return idna;
211     } else {
212         return nullptr;
213     }
214 }
215 
216 // UTS46 implementation ---------------------------------------------------- ***
217 
UTS46(uint32_t opt,UErrorCode & errorCode)218 UTS46::UTS46(uint32_t opt, UErrorCode &errorCode)
219         : uts46Norm2(*Normalizer2::getInstance(nullptr, "uts46", UNORM2_COMPOSE, errorCode)),
220           options(opt) {}
221 
~UTS46()222 UTS46::~UTS46() {}
223 
224 UnicodeString &
labelToASCII(const UnicodeString & label,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const225 UTS46::labelToASCII(const UnicodeString &label, UnicodeString &dest,
226                     IDNAInfo &info, UErrorCode &errorCode) const {
227     return process(label, true, true, dest, info, errorCode);
228 }
229 
230 UnicodeString &
labelToUnicode(const UnicodeString & label,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const231 UTS46::labelToUnicode(const UnicodeString &label, UnicodeString &dest,
232                       IDNAInfo &info, UErrorCode &errorCode) const {
233     return process(label, true, false, dest, info, errorCode);
234 }
235 
236 UnicodeString &
nameToASCII(const UnicodeString & name,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const237 UTS46::nameToASCII(const UnicodeString &name, UnicodeString &dest,
238                    IDNAInfo &info, UErrorCode &errorCode) const {
239     process(name, false, true, dest, info, errorCode);
240     if( dest.length()>=254 && (info.errors&UIDNA_ERROR_DOMAIN_NAME_TOO_LONG)==0 &&
241         isASCIIString(dest) &&
242         (dest.length()>254 || dest[253]!=0x2e)
243     ) {
244         info.errors|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
245     }
246     return dest;
247 }
248 
249 UnicodeString &
nameToUnicode(const UnicodeString & name,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const250 UTS46::nameToUnicode(const UnicodeString &name, UnicodeString &dest,
251                      IDNAInfo &info, UErrorCode &errorCode) const {
252     return process(name, false, false, dest, info, errorCode);
253 }
254 
255 void
labelToASCII_UTF8(StringPiece label,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const256 UTS46::labelToASCII_UTF8(StringPiece label, ByteSink &dest,
257                          IDNAInfo &info, UErrorCode &errorCode) const {
258     processUTF8(label, true, true, dest, info, errorCode);
259 }
260 
261 void
labelToUnicodeUTF8(StringPiece label,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const262 UTS46::labelToUnicodeUTF8(StringPiece label, ByteSink &dest,
263                           IDNAInfo &info, UErrorCode &errorCode) const {
264     processUTF8(label, true, false, dest, info, errorCode);
265 }
266 
267 void
nameToASCII_UTF8(StringPiece name,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const268 UTS46::nameToASCII_UTF8(StringPiece name, ByteSink &dest,
269                         IDNAInfo &info, UErrorCode &errorCode) const {
270     processUTF8(name, false, true, dest, info, errorCode);
271 }
272 
273 void
nameToUnicodeUTF8(StringPiece name,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const274 UTS46::nameToUnicodeUTF8(StringPiece name, ByteSink &dest,
275                          IDNAInfo &info, UErrorCode &errorCode) const {
276     processUTF8(name, false, false, dest, info, errorCode);
277 }
278 
279 // UTS #46 data for ASCII characters.
280 // The normalizer (using uts46.nrm) maps uppercase ASCII letters to lowercase
281 // and passes through all other ASCII characters.
282 // If UIDNA_USE_STD3_RULES is set, then non-LDH characters are disallowed
283 // using this data.
284 // The ASCII fastpath also uses this data.
285 // Values: -1=disallowed  0==valid  1==mapped (lowercase)
286 static const int8_t asciiData[128]={
287     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
288     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
289     // 002D..002E; valid  #  HYPHEN-MINUS..FULL STOP
290     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  0,  0, -1,
291     // 0030..0039; valid  #  DIGIT ZERO..DIGIT NINE
292      0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -1,
293     // 0041..005A; mapped  #  LATIN CAPITAL LETTER A..LATIN CAPITAL LETTER Z
294     -1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
295      1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, -1, -1, -1, -1, -1,
296     // 0061..007A; valid  #  LATIN SMALL LETTER A..LATIN SMALL LETTER Z
297     -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
298      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1
299 };
300 
301 UnicodeString &
process(const UnicodeString & src,UBool isLabel,UBool toASCII,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const302 UTS46::process(const UnicodeString &src,
303                UBool isLabel, UBool toASCII,
304                UnicodeString &dest,
305                IDNAInfo &info, UErrorCode &errorCode) const {
306     // uts46Norm2.normalize() would do all of this error checking and setup,
307     // but with the ASCII fastpath we do not always call it, and do not
308     // call it first.
309     if(U_FAILURE(errorCode)) {
310         dest.setToBogus();
311         return dest;
312     }
313     const char16_t *srcArray=src.getBuffer();
314     if(&dest==&src || srcArray==nullptr) {
315         errorCode=U_ILLEGAL_ARGUMENT_ERROR;
316         dest.setToBogus();
317         return dest;
318     }
319     // Arguments are fine, reset output values.
320     dest.remove();
321     info.reset();
322     int32_t srcLength=src.length();
323     if(srcLength==0) {
324         info.errors|=UIDNA_ERROR_EMPTY_LABEL;
325         return dest;
326     }
327     char16_t *destArray=dest.getBuffer(srcLength);
328     if(destArray==nullptr) {
329         errorCode=U_MEMORY_ALLOCATION_ERROR;
330         return dest;
331     }
332     // ASCII fastpath
333     UBool disallowNonLDHDot=(options&UIDNA_USE_STD3_RULES)!=0;
334     int32_t labelStart=0;
335     int32_t i;
336     for(i=0;; ++i) {
337         if(i==srcLength) {
338             if(toASCII) {
339                 if((i-labelStart)>63) {
340                     info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
341                 }
342                 // There is a trailing dot if labelStart==i.
343                 if(!isLabel && i>=254 && (i>254 || labelStart<i)) {
344                     info.errors|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
345                 }
346             }
347             info.errors|=info.labelErrors;
348             dest.releaseBuffer(i);
349             return dest;
350         }
351         char16_t c=srcArray[i];
352         if(c>0x7f) {
353             break;
354         }
355         int cData=asciiData[c];
356         if(cData>0) {
357             destArray[i]=c+0x20;  // Lowercase an uppercase ASCII letter.
358         } else if(cData<0 && disallowNonLDHDot) {
359             break;  // Replacing with U+FFFD can be complicated for toASCII.
360         } else {
361             destArray[i]=c;
362             if(c==0x2d) {  // hyphen
363                 if(i==(labelStart+3) && srcArray[i-1]==0x2d) {
364                     // "??--..." is Punycode or forbidden.
365                     ++i;  // '-' was copied to dest already
366                     break;
367                 }
368                 if(i==labelStart) {
369                     // label starts with "-"
370                     info.labelErrors|=UIDNA_ERROR_LEADING_HYPHEN;
371                 }
372                 if((i+1)==srcLength || srcArray[i+1]==0x2e) {
373                     // label ends with "-"
374                     info.labelErrors|=UIDNA_ERROR_TRAILING_HYPHEN;
375                 }
376             } else if(c==0x2e) {  // dot
377                 if(isLabel) {
378                     // Replacing with U+FFFD can be complicated for toASCII.
379                     ++i;  // '.' was copied to dest already
380                     break;
381                 }
382                 if(i==labelStart) {
383                     info.labelErrors|=UIDNA_ERROR_EMPTY_LABEL;
384                 }
385                 if(toASCII && (i-labelStart)>63) {
386                     info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
387                 }
388                 info.errors|=info.labelErrors;
389                 info.labelErrors=0;
390                 labelStart=i+1;
391             }
392         }
393     }
394     info.errors|=info.labelErrors;
395     dest.releaseBuffer(i);
396     processUnicode(src, labelStart, i, isLabel, toASCII, dest, info, errorCode);
397     if( info.isBiDi && U_SUCCESS(errorCode) && (info.errors&severeErrors)==0 &&
398         (!info.isOkBiDi || (labelStart>0 && !isASCIIOkBiDi(dest.getBuffer(), labelStart)))
399     ) {
400         info.errors|=UIDNA_ERROR_BIDI;
401     }
402     return dest;
403 }
404 
405 void
processUTF8(StringPiece src,UBool isLabel,UBool toASCII,ByteSink & dest,IDNAInfo & info,UErrorCode & errorCode) const406 UTS46::processUTF8(StringPiece src,
407                    UBool isLabel, UBool toASCII,
408                    ByteSink &dest,
409                    IDNAInfo &info, UErrorCode &errorCode) const {
410     if(U_FAILURE(errorCode)) {
411         return;
412     }
413     const char *srcArray=src.data();
414     int32_t srcLength=src.length();
415     if(srcArray==nullptr && srcLength!=0) {
416         errorCode=U_ILLEGAL_ARGUMENT_ERROR;
417         return;
418     }
419     // Arguments are fine, reset output values.
420     info.reset();
421     if(srcLength==0) {
422         info.errors|=UIDNA_ERROR_EMPTY_LABEL;
423         dest.Flush();
424         return;
425     }
426     UnicodeString destString;
427     int32_t labelStart=0;
428     if(srcLength<=256) {  // length of stackArray[]
429         // ASCII fastpath
430         char stackArray[256];
431         int32_t destCapacity;
432         char *destArray=dest.GetAppendBuffer(srcLength, srcLength+20,
433                                              stackArray, UPRV_LENGTHOF(stackArray), &destCapacity);
434         UBool disallowNonLDHDot=(options&UIDNA_USE_STD3_RULES)!=0;
435         int32_t i;
436         for(i=0;; ++i) {
437             if(i==srcLength) {
438                 if(toASCII) {
439                     if((i-labelStart)>63) {
440                         info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
441                     }
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;
445                     }
446                 }
447                 info.errors|=info.labelErrors;
448                 dest.Append(destArray, i);
449                 dest.Flush();
450                 return;
451             }
452             char c=srcArray[i];
453             if((int8_t)c<0) {  // (uint8_t)c>0x7f
454                 break;
455             }
456             int cData=asciiData[(int)c];  // Cast: gcc warns about indexing with a char.
457             if(cData>0) {
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.
461             } else {
462                 destArray[i]=c;
463                 if(c==0x2d) {  // hyphen
464                     if(i==(labelStart+3) && srcArray[i-1]==0x2d) {
465                         // "??--..." is Punycode or forbidden.
466                         break;
467                     }
468                     if(i==labelStart) {
469                         // label starts with "-"
470                         info.labelErrors|=UIDNA_ERROR_LEADING_HYPHEN;
471                     }
472                     if((i+1)==srcLength || srcArray[i+1]==0x2e) {
473                         // label ends with "-"
474                         info.labelErrors|=UIDNA_ERROR_TRAILING_HYPHEN;
475                     }
476                 } else if(c==0x2e) {  // dot
477                     if(isLabel) {
478                         break;  // Replacing with U+FFFD can be complicated for toASCII.
479                     }
480                     if(i==labelStart) {
481                         info.labelErrors|=UIDNA_ERROR_EMPTY_LABEL;
482                     }
483                     if(toASCII && (i-labelStart)>63) {
484                         info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
485                     }
486                     info.errors|=info.labelErrors;
487                     info.labelErrors=0;
488                     labelStart=i+1;
489                 }
490             }
491         }
492         info.errors|=info.labelErrors;
493         // Convert the processed ASCII prefix of the current label to UTF-16.
494         int32_t mappingStart=i-labelStart;
495         destString=UnicodeString::fromUTF8(StringPiece(destArray+labelStart, mappingStart));
496         // Output the previous ASCII labels and process the rest of src in UTF-16.
497         dest.Append(destArray, labelStart);
498         processUnicode(UnicodeString::fromUTF8(StringPiece(src, labelStart)), 0, mappingStart,
499                        isLabel, toASCII,
500                        destString, info, errorCode);
501     } else {
502         // src is too long for the ASCII fastpath implementation.
503         processUnicode(UnicodeString::fromUTF8(src), 0, 0,
504                        isLabel, toASCII,
505                        destString, info, errorCode);
506     }
507     destString.toUTF8(dest);  // calls dest.Flush()
508     if(toASCII && !isLabel) {
509         // length==labelStart==254 means that there is a trailing dot (ok) and
510         // destString is empty (do not index at 253-labelStart).
511         int32_t length=labelStart+destString.length();
512         if( length>=254 && isASCIIString(destString) &&
513             (length>254 ||
514              (labelStart<254 && destString[253-labelStart]!=0x2e))
515         ) {
516             info.errors|=UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
517         }
518     }
519     if( info.isBiDi && U_SUCCESS(errorCode) && (info.errors&severeErrors)==0 &&
520         (!info.isOkBiDi || (labelStart>0 && !isASCIIOkBiDi(srcArray, labelStart)))
521     ) {
522         info.errors|=UIDNA_ERROR_BIDI;
523     }
524 }
525 
526 UnicodeString &
processUnicode(const UnicodeString & src,int32_t labelStart,int32_t mappingStart,UBool isLabel,UBool toASCII,UnicodeString & dest,IDNAInfo & info,UErrorCode & errorCode) const527 UTS46::processUnicode(const UnicodeString &src,
528                       int32_t labelStart, int32_t mappingStart,
529                       UBool isLabel, UBool toASCII,
530                       UnicodeString &dest,
531                       IDNAInfo &info, UErrorCode &errorCode) const {
532     if(mappingStart==0) {
533         uts46Norm2.normalize(src, dest, errorCode);
534     } else {
535         uts46Norm2.normalizeSecondAndAppend(dest, src.tempSubString(mappingStart), errorCode);
536     }
537     if(U_FAILURE(errorCode)) {
538         return dest;
539     }
540     UBool doMapDevChars=
541         toASCII ? (options&UIDNA_NONTRANSITIONAL_TO_ASCII)==0 :
542                   (options&UIDNA_NONTRANSITIONAL_TO_UNICODE)==0;
543     const char16_t *destArray=dest.getBuffer();
544     int32_t destLength=dest.length();
545     int32_t labelLimit=labelStart;
546     while(labelLimit<destLength) {
547         char16_t c=destArray[labelLimit];
548         if(c==0x2e && !isLabel) {
549             int32_t labelLength=labelLimit-labelStart;
550             int32_t newLength=processLabel(dest, labelStart, labelLength,
551                                             toASCII, info, errorCode);
552             info.errors|=info.labelErrors;
553             info.labelErrors=0;
554             if(U_FAILURE(errorCode)) {
555                 return dest;
556             }
557             destArray=dest.getBuffer();
558             destLength+=newLength-labelLength;
559             labelLimit=labelStart+=newLength+1;
560             continue;
561         } else if(c<0xdf) {
562             // pass
563         } else if(c<=0x200d && (c==0xdf || c==0x3c2 || c>=0x200c)) {
564             info.isTransDiff=true;
565             if(doMapDevChars) {
566                 destLength=mapDevChars(dest, labelStart, labelLimit, errorCode);
567                 if(U_FAILURE(errorCode)) {
568                     return dest;
569                 }
570                 destArray=dest.getBuffer();
571                 // All deviation characters have been mapped, no need to check for them again.
572                 doMapDevChars=false;
573                 // Do not increment labelLimit in case c was removed.
574                 continue;
575             }
576         } else if(U16_IS_SURROGATE(c)) {
577             if(U16_IS_SURROGATE_LEAD(c) ?
578                     (labelLimit+1)==destLength || !U16_IS_TRAIL(destArray[labelLimit+1]) :
579                     labelLimit==labelStart || !U16_IS_LEAD(destArray[labelLimit-1])) {
580                 // Map an unpaired surrogate to U+FFFD before normalization so that when
581                 // that removes characters we do not turn two unpaired ones into a pair.
582                 info.labelErrors|=UIDNA_ERROR_DISALLOWED;
583                 dest.setCharAt(labelLimit, 0xfffd);
584                 destArray=dest.getBuffer();
585             }
586         }
587         ++labelLimit;
588     }
589     // Permit an empty label at the end (0<labelStart==labelLimit==destLength is ok)
590     // but not an empty label elsewhere nor a completely empty domain name.
591     // processLabel() sets UIDNA_ERROR_EMPTY_LABEL when labelLength==0.
592     if(0==labelStart || labelStart<labelLimit) {
593         processLabel(dest, labelStart, labelLimit-labelStart,
594                       toASCII, info, errorCode);
595         info.errors|=info.labelErrors;
596     }
597     return dest;
598 }
599 
600 int32_t
mapDevChars(UnicodeString & dest,int32_t labelStart,int32_t mappingStart,UErrorCode & errorCode) const601 UTS46::mapDevChars(UnicodeString &dest, int32_t labelStart, int32_t mappingStart,
602                    UErrorCode &errorCode) const {
603     if(U_FAILURE(errorCode)) {
604         return 0;
605     }
606     int32_t length=dest.length();
607     char16_t *s=dest.getBuffer(dest[mappingStart]==0xdf ? length+1 : length);
608     if(s==nullptr) {
609         errorCode=U_MEMORY_ALLOCATION_ERROR;
610         return length;
611     }
612     int32_t capacity=dest.getCapacity();
613     UBool didMapDevChars=false;
614     int32_t readIndex=mappingStart, writeIndex=mappingStart;
615     do {
616         char16_t c=s[readIndex++];
617         switch(c) {
618         case 0xdf:
619             // Map sharp s to ss.
620             didMapDevChars=true;
621             s[writeIndex++]=0x73;  // Replace sharp s with first s.
622             // Insert second s and account for possible buffer reallocation.
623             if(writeIndex==readIndex) {
624                 if(length==capacity) {
625                     dest.releaseBuffer(length);
626                     s=dest.getBuffer(length+1);
627                     if(s==nullptr) {
628                         errorCode=U_MEMORY_ALLOCATION_ERROR;
629                         return length;
630                     }
631                     capacity=dest.getCapacity();
632                 }
633                 u_memmove(s+writeIndex+1, s+writeIndex, length-writeIndex);
634                 ++readIndex;
635             }
636             s[writeIndex++]=0x73;
637             ++length;
638             break;
639         case 0x3c2:  // Map final sigma to nonfinal sigma.
640             didMapDevChars=true;
641             s[writeIndex++]=0x3c3;
642             break;
643         case 0x200c:  // Ignore/remove ZWNJ.
644         case 0x200d:  // Ignore/remove ZWJ.
645             didMapDevChars=true;
646             --length;
647             break;
648         default:
649             // Only really necessary if writeIndex was different from readIndex.
650             s[writeIndex++]=c;
651             break;
652         }
653     } while(writeIndex<length);
654     dest.releaseBuffer(length);
655     if(didMapDevChars) {
656         // Mapping deviation characters might have resulted in an un-NFC string.
657         // We could use either the NFC or the UTS #46 normalizer.
658         // By using the UTS #46 normalizer again, we avoid having to load a second .nrm data file.
659         UnicodeString normalized;
660         uts46Norm2.normalize(dest.tempSubString(labelStart), normalized, errorCode);
661         if(U_SUCCESS(errorCode)) {
662             dest.replace(labelStart, 0x7fffffff, normalized);
663             if(dest.isBogus()) {
664                 errorCode=U_MEMORY_ALLOCATION_ERROR;
665             }
666             return dest.length();
667         }
668     }
669     return length;
670 }
671 
672 // Replace the label in dest with the label string, if the label was modified.
673 // If &label==&dest then the label was modified in-place and labelLength
674 // is the new label length, different from label.length().
675 // If &label!=&dest then labelLength==label.length().
676 // Returns labelLength (= the new label length).
677 static int32_t
replaceLabel(UnicodeString & dest,int32_t destLabelStart,int32_t destLabelLength,const UnicodeString & label,int32_t labelLength,UErrorCode & errorCode)678 replaceLabel(UnicodeString &dest, int32_t destLabelStart, int32_t destLabelLength,
679              const UnicodeString &label, int32_t labelLength, UErrorCode &errorCode) {
680     if(U_FAILURE(errorCode)) {
681         return 0;
682     }
683     if(&label!=&dest) {
684         dest.replace(destLabelStart, destLabelLength, label);
685         if(dest.isBogus()) {
686             errorCode=U_MEMORY_ALLOCATION_ERROR;
687             return 0;
688         }
689     }
690     return labelLength;
691 }
692 
693 int32_t
processLabel(UnicodeString & dest,int32_t labelStart,int32_t labelLength,UBool toASCII,IDNAInfo & info,UErrorCode & errorCode) const694 UTS46::processLabel(UnicodeString &dest,
695                     int32_t labelStart, int32_t labelLength,
696                     UBool toASCII,
697                     IDNAInfo &info, UErrorCode &errorCode) const {
698     if(U_FAILURE(errorCode)) {
699         return 0;
700     }
701     UnicodeString fromPunycode;
702     UnicodeString *labelString;
703     const char16_t *label=dest.getBuffer()+labelStart;
704     int32_t destLabelStart=labelStart;
705     int32_t destLabelLength=labelLength;
706     UBool wasPunycode;
707     if(labelLength>=4 && label[0]==0x78 && label[1]==0x6e && label[2]==0x2d && label[3]==0x2d) {
708         // Label starts with "xn--", try to un-Punycode it.
709         // In IDNA2008, labels like "xn--" (decodes to an empty string) and
710         // "xn--ASCII-" (decodes to just "ASCII") fail the round-trip validation from
711         // comparing the ToUnicode input with the back-to-ToASCII output.
712         // They are alternate encodings of the respective ASCII labels.
713         // Ignore "xn---" here: It will fail Punycode.decode() which logically comes before
714         // the round-trip verification.
715         if(labelLength==4 || (labelLength>5 && label[labelLength-1]==u'-')) {
716             info.labelErrors|=UIDNA_ERROR_INVALID_ACE_LABEL;
717             return markBadACELabel(dest, labelStart, labelLength, toASCII, info, errorCode);
718         }
719         wasPunycode=true;
720         char16_t *unicodeBuffer=fromPunycode.getBuffer(-1);  // capacity==-1: most labels should fit
721         if(unicodeBuffer==nullptr) {
722             // Should never occur if we used capacity==-1 which uses the internal buffer.
723             errorCode=U_MEMORY_ALLOCATION_ERROR;
724             return labelLength;
725         }
726         UErrorCode punycodeErrorCode=U_ZERO_ERROR;
727         int32_t unicodeLength=u_strFromPunycode(label+4, labelLength-4,
728                                                 unicodeBuffer, fromPunycode.getCapacity(),
729                                                 nullptr, &punycodeErrorCode);
730         if(punycodeErrorCode==U_BUFFER_OVERFLOW_ERROR) {
731             fromPunycode.releaseBuffer(0);
732             unicodeBuffer=fromPunycode.getBuffer(unicodeLength);
733             if(unicodeBuffer==nullptr) {
734                 errorCode=U_MEMORY_ALLOCATION_ERROR;
735                 return labelLength;
736             }
737             punycodeErrorCode=U_ZERO_ERROR;
738             unicodeLength=u_strFromPunycode(label+4, labelLength-4,
739                                             unicodeBuffer, fromPunycode.getCapacity(),
740                                             nullptr, &punycodeErrorCode);
741         }
742         fromPunycode.releaseBuffer(unicodeLength);
743         if(U_FAILURE(punycodeErrorCode)) {
744             info.labelErrors|=UIDNA_ERROR_PUNYCODE;
745             return markBadACELabel(dest, labelStart, labelLength, toASCII, info, errorCode);
746         }
747         // Check for NFC, and for characters that are not
748         // valid or deviation characters according to the normalizer.
749         // If there is something wrong, then the string will change.
750         // Note that the normalizer passes through non-LDH ASCII and deviation characters.
751         // Deviation characters are ok in Punycode even in transitional processing.
752         // In the code further below, if we find non-LDH ASCII and we have UIDNA_USE_STD3_RULES
753         // then we will set UIDNA_ERROR_INVALID_ACE_LABEL there too.
754         UBool isValid=uts46Norm2.isNormalized(fromPunycode, errorCode);
755         if(U_FAILURE(errorCode)) {
756             return labelLength;
757         }
758         if(!isValid) {
759             info.labelErrors|=UIDNA_ERROR_INVALID_ACE_LABEL;
760             return markBadACELabel(dest, labelStart, labelLength, toASCII, info, errorCode);
761         }
762         labelString=&fromPunycode;
763         label=fromPunycode.getBuffer();
764         labelStart=0;
765         labelLength=fromPunycode.length();
766     } else {
767         wasPunycode=false;
768         labelString=&dest;
769     }
770     // Validity check
771     if(labelLength==0) {
772         info.labelErrors|=UIDNA_ERROR_EMPTY_LABEL;
773         return replaceLabel(dest, destLabelStart, destLabelLength,
774                             *labelString, labelLength, errorCode);
775     }
776     // labelLength>0
777     if(labelLength>=4 && label[2]==0x2d && label[3]==0x2d) {
778         // label starts with "??--"
779         info.labelErrors|=UIDNA_ERROR_HYPHEN_3_4;
780     }
781     if(label[0]==0x2d) {
782         // label starts with "-"
783         info.labelErrors|=UIDNA_ERROR_LEADING_HYPHEN;
784     }
785     if(label[labelLength-1]==0x2d) {
786         // label ends with "-"
787         info.labelErrors|=UIDNA_ERROR_TRAILING_HYPHEN;
788     }
789     // If the label was not a Punycode label, then it was the result of
790     // mapping, normalization and label segmentation.
791     // If the label was in Punycode, then we mapped it again above
792     // and checked its validity.
793     // Now we handle the STD3 restriction to LDH characters (if set)
794     // and we look for U+FFFD which indicates disallowed characters
795     // in a non-Punycode label or U+FFFD itself in a Punycode label.
796     // We also check for dots which can come from the input to a single-label function.
797     // Ok to cast away const because we own the UnicodeString.
798     char16_t *s=(char16_t *)label;
799     const char16_t *limit=label+labelLength;
800     char16_t oredChars=0;
801     // If we enforce STD3 rules, then ASCII characters other than LDH and dot are disallowed.
802     UBool disallowNonLDHDot=(options&UIDNA_USE_STD3_RULES)!=0;
803     do {
804         char16_t c=*s;
805         if(c<=0x7f) {
806             if(c==0x2e) {
807                 info.labelErrors|=UIDNA_ERROR_LABEL_HAS_DOT;
808                 *s=0xfffd;
809             } else if(disallowNonLDHDot && asciiData[c]<0) {
810                 info.labelErrors|=UIDNA_ERROR_DISALLOWED;
811                 *s=0xfffd;
812             }
813         } else {
814             oredChars|=c;
815             if(c==0xfffd) {
816                 info.labelErrors|=UIDNA_ERROR_DISALLOWED;
817             }
818         }
819         ++s;
820     } while(s<limit);
821     // Check for a leading combining mark after other validity checks
822     // so that we don't report UIDNA_ERROR_DISALLOWED for the U+FFFD from here.
823     UChar32 c;
824     int32_t cpLength=0;
825     // "Unsafe" is ok because unpaired surrogates were mapped to U+FFFD.
826     U16_NEXT_UNSAFE(label, cpLength, c);
827     if((U_GET_GC_MASK(c)&U_GC_M_MASK)!=0) {
828         info.labelErrors|=UIDNA_ERROR_LEADING_COMBINING_MARK;
829         labelString->replace(labelStart, cpLength, (char16_t)0xfffd);
830         label=labelString->getBuffer()+labelStart;
831         labelLength+=1-cpLength;
832         if(labelString==&dest) {
833             destLabelLength=labelLength;
834         }
835     }
836     if((info.labelErrors&severeErrors)==0) {
837         // Do contextual checks only if we do not have U+FFFD from a severe error
838         // because U+FFFD can make these checks fail.
839         if((options&UIDNA_CHECK_BIDI)!=0 && (!info.isBiDi || info.isOkBiDi)) {
840             checkLabelBiDi(label, labelLength, info);
841         }
842         if( (options&UIDNA_CHECK_CONTEXTJ)!=0 && (oredChars&0x200c)==0x200c &&
843             !isLabelOkContextJ(label, labelLength)
844         ) {
845             info.labelErrors|=UIDNA_ERROR_CONTEXTJ;
846         }
847         if((options&UIDNA_CHECK_CONTEXTO)!=0 && oredChars>=0xb7) {
848             checkLabelContextO(label, labelLength, info);
849         }
850         if(toASCII) {
851             if(wasPunycode) {
852                 // Leave a Punycode label unchanged if it has no severe errors.
853                 if(destLabelLength>63) {
854                     info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
855                 }
856                 return destLabelLength;
857             } else if(oredChars>=0x80) {
858                 // Contains non-ASCII characters.
859                 UnicodeString punycode;
860                 char16_t *buffer=punycode.getBuffer(63);  // 63==maximum DNS label length
861                 if(buffer==nullptr) {
862                     errorCode=U_MEMORY_ALLOCATION_ERROR;
863                     return destLabelLength;
864                 }
865                 buffer[0]=0x78;  // Write "xn--".
866                 buffer[1]=0x6e;
867                 buffer[2]=0x2d;
868                 buffer[3]=0x2d;
869                 int32_t punycodeLength=u_strToPunycode(label, labelLength,
870                                                       buffer+4, punycode.getCapacity()-4,
871                                                       nullptr, &errorCode);
872                 if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
873                     errorCode=U_ZERO_ERROR;
874                     punycode.releaseBuffer(4);
875                     buffer=punycode.getBuffer(4+punycodeLength);
876                     if(buffer==nullptr) {
877                         errorCode=U_MEMORY_ALLOCATION_ERROR;
878                         return destLabelLength;
879                     }
880                     punycodeLength=u_strToPunycode(label, labelLength,
881                                                   buffer+4, punycode.getCapacity()-4,
882                                                   nullptr, &errorCode);
883                 }
884                 punycodeLength+=4;
885                 punycode.releaseBuffer(punycodeLength);
886                 if(U_FAILURE(errorCode)) {
887                     return destLabelLength;
888                 }
889                 if(punycodeLength>63) {
890                     info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
891                 }
892                 return replaceLabel(dest, destLabelStart, destLabelLength,
893                                     punycode, punycodeLength, errorCode);
894             } else {
895                 // all-ASCII label
896                 if(labelLength>63) {
897                     info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
898                 }
899             }
900         }
901     } else {
902         // If a Punycode label has severe errors,
903         // then leave it but make sure it does not look valid.
904         if(wasPunycode) {
905             info.labelErrors|=UIDNA_ERROR_INVALID_ACE_LABEL;
906             return markBadACELabel(dest, destLabelStart, destLabelLength, toASCII, info, errorCode);
907         }
908     }
909     return replaceLabel(dest, destLabelStart, destLabelLength,
910                         *labelString, labelLength, errorCode);
911 }
912 
913 // Make sure an ACE label does not look valid.
914 // Append U+FFFD if the label has only LDH characters.
915 // If UIDNA_USE_STD3_RULES, also replace disallowed ASCII characters with U+FFFD.
916 int32_t
markBadACELabel(UnicodeString & dest,int32_t labelStart,int32_t labelLength,UBool toASCII,IDNAInfo & info,UErrorCode & errorCode) const917 UTS46::markBadACELabel(UnicodeString &dest,
918                        int32_t labelStart, int32_t labelLength,
919                        UBool toASCII, IDNAInfo &info, UErrorCode &errorCode) const {
920     if(U_FAILURE(errorCode)) {
921         return 0;
922     }
923     UBool disallowNonLDHDot=(options&UIDNA_USE_STD3_RULES)!=0;
924     UBool isASCII=true;
925     UBool onlyLDH=true;
926     const char16_t *label=dest.getBuffer()+labelStart;
927     const char16_t *limit=label+labelLength;
928     // Start after the initial "xn--".
929     // Ok to cast away const because we own the UnicodeString.
930     for(char16_t *s=const_cast<char16_t *>(label+4); s<limit; ++s) {
931         char16_t c=*s;
932         if(c<=0x7f) {
933             if(c==0x2e) {
934                 info.labelErrors|=UIDNA_ERROR_LABEL_HAS_DOT;
935                 *s=0xfffd;
936                 isASCII=onlyLDH=false;
937             } else if(asciiData[c]<0) {
938                 onlyLDH=false;
939                 if(disallowNonLDHDot) {
940                     *s=0xfffd;
941                     isASCII=false;
942                 }
943             }
944         } else {
945             isASCII=onlyLDH=false;
946         }
947     }
948     if(onlyLDH) {
949         dest.insert(labelStart+labelLength, (char16_t)0xfffd);
950         if(dest.isBogus()) {
951             errorCode=U_MEMORY_ALLOCATION_ERROR;
952             return 0;
953         }
954         ++labelLength;
955     } else {
956         if(toASCII && isASCII && labelLength>63) {
957             info.labelErrors|=UIDNA_ERROR_LABEL_TOO_LONG;
958         }
959     }
960     return labelLength;
961 }
962 
963 const uint32_t L_MASK=U_MASK(U_LEFT_TO_RIGHT);
964 const uint32_t R_AL_MASK=U_MASK(U_RIGHT_TO_LEFT)|U_MASK(U_RIGHT_TO_LEFT_ARABIC);
965 const uint32_t L_R_AL_MASK=L_MASK|R_AL_MASK;
966 
967 const uint32_t R_AL_AN_MASK=R_AL_MASK|U_MASK(U_ARABIC_NUMBER);
968 
969 const uint32_t EN_AN_MASK=U_MASK(U_EUROPEAN_NUMBER)|U_MASK(U_ARABIC_NUMBER);
970 const uint32_t R_AL_EN_AN_MASK=R_AL_MASK|EN_AN_MASK;
971 const uint32_t L_EN_MASK=L_MASK|U_MASK(U_EUROPEAN_NUMBER);
972 
973 const uint32_t ES_CS_ET_ON_BN_NSM_MASK=
974     U_MASK(U_EUROPEAN_NUMBER_SEPARATOR)|
975     U_MASK(U_COMMON_NUMBER_SEPARATOR)|
976     U_MASK(U_EUROPEAN_NUMBER_TERMINATOR)|
977     U_MASK(U_OTHER_NEUTRAL)|
978     U_MASK(U_BOUNDARY_NEUTRAL)|
979     U_MASK(U_DIR_NON_SPACING_MARK);
980 const uint32_t L_EN_ES_CS_ET_ON_BN_NSM_MASK=L_EN_MASK|ES_CS_ET_ON_BN_NSM_MASK;
981 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;
982 
983 // We scan the whole label and check both for whether it contains RTL characters
984 // and whether it passes the BiDi Rule.
985 // In a BiDi domain name, all labels must pass the BiDi Rule, but we might find
986 // that a domain name is a BiDi domain name (has an RTL label) only after
987 // processing several earlier labels.
988 void
checkLabelBiDi(const char16_t * label,int32_t labelLength,IDNAInfo & info) const989 UTS46::checkLabelBiDi(const char16_t *label, int32_t labelLength, IDNAInfo &info) const {
990     // IDNA2008 BiDi rule
991     // Get the directionality of the first character.
992     UChar32 c;
993     int32_t i=0;
994     U16_NEXT_UNSAFE(label, i, c);
995     uint32_t firstMask=U_MASK(u_charDirection(c));
996     // 1. The first character must be a character with BIDI property L, R
997     // or AL.  If it has the R or AL property, it is an RTL label; if it
998     // has the L property, it is an LTR label.
999     if((firstMask&~L_R_AL_MASK)!=0) {
1000         info.isOkBiDi=false;
1001     }
1002     // Get the directionality of the last non-NSM character.
1003     uint32_t lastMask;
1004     for(;;) {
1005         if(i>=labelLength) {
1006             lastMask=firstMask;
1007             break;
1008         }
1009         U16_PREV_UNSAFE(label, labelLength, c);
1010         UCharDirection dir=u_charDirection(c);
1011         if(dir!=U_DIR_NON_SPACING_MARK) {
1012             lastMask=U_MASK(dir);
1013             break;
1014         }
1015     }
1016     // 3. In an RTL label, the end of the label must be a character with
1017     // BIDI property R, AL, EN or AN, followed by zero or more
1018     // characters with BIDI property NSM.
1019     // 6. In an LTR label, the end of the label must be a character with
1020     // BIDI property L or EN, followed by zero or more characters with
1021     // BIDI property NSM.
1022     if( (firstMask&L_MASK)!=0 ?
1023             (lastMask&~L_EN_MASK)!=0 :
1024             (lastMask&~R_AL_EN_AN_MASK)!=0
1025     ) {
1026         info.isOkBiDi=false;
1027     }
1028     // Add the directionalities of the intervening characters.
1029     uint32_t mask=firstMask|lastMask;
1030     while(i<labelLength) {
1031         U16_NEXT_UNSAFE(label, i, c);
1032         mask|=U_MASK(u_charDirection(c));
1033     }
1034     if(firstMask&L_MASK) {
1035         // 5. In an LTR label, only characters with the BIDI properties L, EN,
1036         // ES, CS, ET, ON, BN and NSM are allowed.
1037         if((mask&~L_EN_ES_CS_ET_ON_BN_NSM_MASK)!=0) {
1038             info.isOkBiDi=false;
1039         }
1040     } else {
1041         // 2. In an RTL label, only characters with the BIDI properties R, AL,
1042         // AN, EN, ES, CS, ET, ON, BN and NSM are allowed.
1043         if((mask&~R_AL_AN_EN_ES_CS_ET_ON_BN_NSM_MASK)!=0) {
1044             info.isOkBiDi=false;
1045         }
1046         // 4. In an RTL label, if an EN is present, no AN may be present, and
1047         // vice versa.
1048         if((mask&EN_AN_MASK)==EN_AN_MASK) {
1049             info.isOkBiDi=false;
1050         }
1051     }
1052     // An RTL label is a label that contains at least one character of type
1053     // R, AL or AN. [...]
1054     // A "BIDI domain name" is a domain name that contains at least one RTL
1055     // label. [...]
1056     // The following rule, consisting of six conditions, applies to labels
1057     // in BIDI domain names.
1058     if((mask&R_AL_AN_MASK)!=0) {
1059         info.isBiDi=true;
1060     }
1061 }
1062 
1063 // Special code for the ASCII prefix of a BiDi domain name.
1064 // The ASCII prefix is all-LTR.
1065 
1066 // IDNA2008 BiDi rule, parts relevant to ASCII labels:
1067 // 1. The first character must be a character with BIDI property L [...]
1068 // 5. In an LTR label, only characters with the BIDI properties L, EN,
1069 // ES, CS, ET, ON, BN and NSM are allowed.
1070 // 6. In an LTR label, the end of the label must be a character with
1071 // BIDI property L or EN [...]
1072 
1073 // UTF-16 version, called for mapped ASCII prefix.
1074 // Cannot contain uppercase A-Z.
1075 // s[length-1] must be the trailing dot.
1076 static UBool
isASCIIOkBiDi(const char16_t * s,int32_t length)1077 isASCIIOkBiDi(const char16_t *s, int32_t length) {
1078     int32_t labelStart=0;
1079     for(int32_t i=0; i<length; ++i) {
1080         char16_t c=s[i];
1081         if(c==0x2e) {  // dot
1082             if(i>labelStart) {
1083                 c=s[i-1];
1084                 if(!(0x61<=c && c<=0x7a) && !(0x30<=c && c<=0x39)) {
1085                     // Last character in the label is not an L or EN.
1086                     return false;
1087                 }
1088             }
1089             labelStart=i+1;
1090         } else if(i==labelStart) {
1091             if(!(0x61<=c && c<=0x7a)) {
1092                 // First character in the label is not an L.
1093                 return false;
1094             }
1095         } else {
1096             if(c<=0x20 && (c>=0x1c || (9<=c && c<=0xd))) {
1097                 // Intermediate character in the label is a B, S or WS.
1098                 return false;
1099             }
1100         }
1101     }
1102     return true;
1103 }
1104 
1105 // UTF-8 version, called for source ASCII prefix.
1106 // Can contain uppercase A-Z.
1107 // s[length-1] must be the trailing dot.
1108 static UBool
isASCIIOkBiDi(const char * s,int32_t length)1109 isASCIIOkBiDi(const char *s, int32_t length) {
1110     int32_t labelStart=0;
1111     for(int32_t i=0; i<length; ++i) {
1112         char c=s[i];
1113         if(c==0x2e) {  // dot
1114             if(i>labelStart) {
1115                 c=s[i-1];
1116                 if(!(0x61<=c && c<=0x7a) && !(0x41<=c && c<=0x5a) && !(0x30<=c && c<=0x39)) {
1117                     // Last character in the label is not an L or EN.
1118                     return false;
1119                 }
1120             }
1121             labelStart=i+1;
1122         } else if(i==labelStart) {
1123             if(!(0x61<=c && c<=0x7a) && !(0x41<=c && c<=0x5a)) {
1124                 // First character in the label is not an L.
1125                 return false;
1126             }
1127         } else {
1128             if(c<=0x20 && (c>=0x1c || (9<=c && c<=0xd))) {
1129                 // Intermediate character in the label is a B, S or WS.
1130                 return false;
1131             }
1132         }
1133     }
1134     return true;
1135 }
1136 
1137 UBool
isLabelOkContextJ(const char16_t * label,int32_t labelLength) const1138 UTS46::isLabelOkContextJ(const char16_t *label, int32_t labelLength) const {
1139     // [IDNA2008-Tables]
1140     // 200C..200D  ; CONTEXTJ    # ZERO WIDTH NON-JOINER..ZERO WIDTH JOINER
1141     for(int32_t i=0; i<labelLength; ++i) {
1142         if(label[i]==0x200c) {
1143             // Appendix A.1. ZERO WIDTH NON-JOINER
1144             // Rule Set:
1145             //  False;
1146             //  If Canonical_Combining_Class(Before(cp)) .eq.  Virama Then True;
1147             //  If RegExpMatch((Joining_Type:{L,D})(Joining_Type:T)*\u200C
1148             //     (Joining_Type:T)*(Joining_Type:{R,D})) Then True;
1149             if(i==0) {
1150                 return false;
1151             }
1152             UChar32 c;
1153             int32_t j=i;
1154             U16_PREV_UNSAFE(label, j, c);
1155             if(uts46Norm2.getCombiningClass(c)==9) {
1156                 continue;
1157             }
1158             // check precontext (Joining_Type:{L,D})(Joining_Type:T)*
1159             for(;;) {
1160                 UJoiningType type=ubidi_getJoiningType(c);
1161                 if(type==U_JT_TRANSPARENT) {
1162                     if(j==0) {
1163                         return false;
1164                     }
1165                     U16_PREV_UNSAFE(label, j, c);
1166                 } else if(type==U_JT_LEFT_JOINING || type==U_JT_DUAL_JOINING) {
1167                     break;  // precontext fulfilled
1168                 } else {
1169                     return false;
1170                 }
1171             }
1172             // check postcontext (Joining_Type:T)*(Joining_Type:{R,D})
1173             for(j=i+1;;) {
1174                 if(j==labelLength) {
1175                     return false;
1176                 }
1177                 U16_NEXT_UNSAFE(label, j, c);
1178                 UJoiningType type=ubidi_getJoiningType(c);
1179                 if(type==U_JT_TRANSPARENT) {
1180                     // just skip this character
1181                 } else if(type==U_JT_RIGHT_JOINING || type==U_JT_DUAL_JOINING) {
1182                     break;  // postcontext fulfilled
1183                 } else {
1184                     return false;
1185                 }
1186             }
1187         } else if(label[i]==0x200d) {
1188             // Appendix A.2. ZERO WIDTH JOINER (U+200D)
1189             // Rule Set:
1190             //  False;
1191             //  If Canonical_Combining_Class(Before(cp)) .eq.  Virama Then True;
1192             if(i==0) {
1193                 return false;
1194             }
1195             UChar32 c;
1196             int32_t j=i;
1197             U16_PREV_UNSAFE(label, j, c);
1198             if(uts46Norm2.getCombiningClass(c)!=9) {
1199                 return false;
1200             }
1201         }
1202     }
1203     return true;
1204 }
1205 
1206 void
checkLabelContextO(const char16_t * label,int32_t labelLength,IDNAInfo & info) const1207 UTS46::checkLabelContextO(const char16_t *label, int32_t labelLength, IDNAInfo &info) const {
1208     int32_t labelEnd=labelLength-1;  // inclusive
1209     int32_t arabicDigits=0;  // -1 for 066x, +1 for 06Fx
1210     for(int32_t i=0; i<=labelEnd; ++i) {
1211         UChar32 c=label[i];
1212         if(c<0xb7) {
1213             // ASCII fastpath
1214         } else if(c<=0x6f9) {
1215             if(c==0xb7) {
1216                 // Appendix A.3. MIDDLE DOT (U+00B7)
1217                 // Rule Set:
1218                 //  False;
1219                 //  If Before(cp) .eq.  U+006C And
1220                 //     After(cp) .eq.  U+006C Then True;
1221                 if(!(0<i && label[i-1]==0x6c &&
1222                      i<labelEnd && label[i+1]==0x6c)) {
1223                     info.labelErrors|=UIDNA_ERROR_CONTEXTO_PUNCTUATION;
1224                 }
1225             } else if(c==0x375) {
1226                 // Appendix A.4. GREEK LOWER NUMERAL SIGN (KERAIA) (U+0375)
1227                 // Rule Set:
1228                 //  False;
1229                 //  If Script(After(cp)) .eq.  Greek Then True;
1230                 UScriptCode script=USCRIPT_INVALID_CODE;
1231                 if(i<labelEnd) {
1232                     UErrorCode errorCode=U_ZERO_ERROR;
1233                     int32_t j=i+1;
1234                     U16_NEXT(label, j, labelLength, c);
1235                     script=uscript_getScript(c, &errorCode);
1236                 }
1237                 if(script!=USCRIPT_GREEK) {
1238                     info.labelErrors|=UIDNA_ERROR_CONTEXTO_PUNCTUATION;
1239                 }
1240             } else if(c==0x5f3 || c==0x5f4) {
1241                 // Appendix A.5. HEBREW PUNCTUATION GERESH (U+05F3)
1242                 // Rule Set:
1243                 //  False;
1244                 //  If Script(Before(cp)) .eq.  Hebrew Then True;
1245                 //
1246                 // Appendix A.6. HEBREW PUNCTUATION GERSHAYIM (U+05F4)
1247                 // Rule Set:
1248                 //  False;
1249                 //  If Script(Before(cp)) .eq.  Hebrew Then True;
1250                 UScriptCode script=USCRIPT_INVALID_CODE;
1251                 if(0<i) {
1252                     UErrorCode errorCode=U_ZERO_ERROR;
1253                     int32_t j=i;
1254                     U16_PREV(label, 0, j, c);
1255                     script=uscript_getScript(c, &errorCode);
1256                 }
1257                 if(script!=USCRIPT_HEBREW) {
1258                     info.labelErrors|=UIDNA_ERROR_CONTEXTO_PUNCTUATION;
1259                 }
1260             } else if(0x660<=c /* && c<=0x6f9 */) {
1261                 // Appendix A.8. ARABIC-INDIC DIGITS (0660..0669)
1262                 // Rule Set:
1263                 //  True;
1264                 //  For All Characters:
1265                 //    If cp .in. 06F0..06F9 Then False;
1266                 //  End For;
1267                 //
1268                 // Appendix A.9. EXTENDED ARABIC-INDIC DIGITS (06F0..06F9)
1269                 // Rule Set:
1270                 //  True;
1271                 //  For All Characters:
1272                 //    If cp .in. 0660..0669 Then False;
1273                 //  End For;
1274                 if(c<=0x669) {
1275                     if(arabicDigits>0) {
1276                         info.labelErrors|=UIDNA_ERROR_CONTEXTO_DIGITS;
1277                     }
1278                     arabicDigits=-1;
1279                 } else if(0x6f0<=c) {
1280                     if(arabicDigits<0) {
1281                         info.labelErrors|=UIDNA_ERROR_CONTEXTO_DIGITS;
1282                     }
1283                     arabicDigits=1;
1284                 }
1285             }
1286         } else if(c==0x30fb) {
1287             // Appendix A.7. KATAKANA MIDDLE DOT (U+30FB)
1288             // Rule Set:
1289             //  False;
1290             //  For All Characters:
1291             //    If Script(cp) .in. {Hiragana, Katakana, Han} Then True;
1292             //  End For;
1293             UErrorCode errorCode=U_ZERO_ERROR;
1294             for(int j=0;;) {
1295                 if(j>labelEnd) {
1296                     info.labelErrors|=UIDNA_ERROR_CONTEXTO_PUNCTUATION;
1297                     break;
1298                 }
1299                 U16_NEXT(label, j, labelLength, c);
1300                 UScriptCode script=uscript_getScript(c, &errorCode);
1301                 if(script==USCRIPT_HIRAGANA || script==USCRIPT_KATAKANA || script==USCRIPT_HAN) {
1302                     break;
1303                 }
1304             }
1305         }
1306     }
1307 }
1308 
1309 U_NAMESPACE_END
1310 
1311 // C API ------------------------------------------------------------------- ***
1312 
1313 U_NAMESPACE_USE
1314 
1315 U_CAPI UIDNA * U_EXPORT2
uidna_openUTS46(uint32_t options,UErrorCode * pErrorCode)1316 uidna_openUTS46(uint32_t options, UErrorCode *pErrorCode) {
1317     return reinterpret_cast<UIDNA *>(IDNA::createUTS46Instance(options, *pErrorCode));
1318 }
1319 
1320 U_CAPI void U_EXPORT2
uidna_close(UIDNA * idna)1321 uidna_close(UIDNA *idna) {
1322     delete reinterpret_cast<IDNA *>(idna);
1323 }
1324 
1325 static UBool
checkArgs(const void * label,int32_t length,void * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1326 checkArgs(const void *label, int32_t length,
1327           void *dest, int32_t capacity,
1328           UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1329     if(U_FAILURE(*pErrorCode)) {
1330         return false;
1331     }
1332     // sizeof(UIDNAInfo)=16 in the first API version.
1333     if(pInfo==nullptr || pInfo->size<16) {
1334         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1335         return false;
1336     }
1337     if( (label==nullptr ? length!=0 : length<-1) ||
1338         (dest==nullptr ? capacity!=0 : capacity<0) ||
1339         (dest==label && label!=nullptr)
1340     ) {
1341         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
1342         return false;
1343     }
1344     // Set all *pInfo bytes to 0 except for the size field itself.
1345     uprv_memset(&pInfo->size+1, 0, pInfo->size-sizeof(pInfo->size));
1346     return true;
1347 }
1348 
1349 static void
idnaInfoToStruct(IDNAInfo & info,UIDNAInfo * pInfo)1350 idnaInfoToStruct(IDNAInfo &info, UIDNAInfo *pInfo) {
1351     pInfo->isTransitionalDifferent=info.isTransitionalDifferent();
1352     pInfo->errors=info.getErrors();
1353 }
1354 
1355 U_CAPI int32_t U_EXPORT2
uidna_labelToASCII(const UIDNA * idna,const char16_t * label,int32_t length,char16_t * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1356 uidna_labelToASCII(const UIDNA *idna,
1357                    const char16_t *label, int32_t length,
1358                    char16_t *dest, int32_t capacity,
1359                    UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1360     if(!checkArgs(label, length, dest, capacity, pInfo, pErrorCode)) {
1361         return 0;
1362     }
1363     UnicodeString src((UBool)(length<0), label, length);
1364     UnicodeString destString(dest, 0, capacity);
1365     IDNAInfo info;
1366     reinterpret_cast<const IDNA *>(idna)->labelToASCII(src, destString, info, *pErrorCode);
1367     idnaInfoToStruct(info, pInfo);
1368     return destString.extract(dest, capacity, *pErrorCode);
1369 }
1370 
1371 U_CAPI int32_t U_EXPORT2
uidna_labelToUnicode(const UIDNA * idna,const char16_t * label,int32_t length,char16_t * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1372 uidna_labelToUnicode(const UIDNA *idna,
1373                      const char16_t *label, int32_t length,
1374                      char16_t *dest, int32_t capacity,
1375                      UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1376     if(!checkArgs(label, length, dest, capacity, pInfo, pErrorCode)) {
1377         return 0;
1378     }
1379     UnicodeString src((UBool)(length<0), label, length);
1380     UnicodeString destString(dest, 0, capacity);
1381     IDNAInfo info;
1382     reinterpret_cast<const IDNA *>(idna)->labelToUnicode(src, destString, info, *pErrorCode);
1383     idnaInfoToStruct(info, pInfo);
1384     return destString.extract(dest, capacity, *pErrorCode);
1385 }
1386 
1387 U_CAPI int32_t U_EXPORT2
uidna_nameToASCII(const UIDNA * idna,const char16_t * name,int32_t length,char16_t * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1388 uidna_nameToASCII(const UIDNA *idna,
1389                   const char16_t *name, int32_t length,
1390                   char16_t *dest, int32_t capacity,
1391                   UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1392     if(!checkArgs(name, length, dest, capacity, pInfo, pErrorCode)) {
1393         return 0;
1394     }
1395     UnicodeString src((UBool)(length<0), name, length);
1396     UnicodeString destString(dest, 0, capacity);
1397     IDNAInfo info;
1398     reinterpret_cast<const IDNA *>(idna)->nameToASCII(src, destString, info, *pErrorCode);
1399     idnaInfoToStruct(info, pInfo);
1400     return destString.extract(dest, capacity, *pErrorCode);
1401 }
1402 
1403 U_CAPI int32_t U_EXPORT2
uidna_nameToUnicode(const UIDNA * idna,const char16_t * name,int32_t length,char16_t * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1404 uidna_nameToUnicode(const UIDNA *idna,
1405                     const char16_t *name, int32_t length,
1406                     char16_t *dest, int32_t capacity,
1407                     UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1408     if(!checkArgs(name, length, dest, capacity, pInfo, pErrorCode)) {
1409         return 0;
1410     }
1411     UnicodeString src((UBool)(length<0), name, length);
1412     UnicodeString destString(dest, 0, capacity);
1413     IDNAInfo info;
1414     reinterpret_cast<const IDNA *>(idna)->nameToUnicode(src, destString, info, *pErrorCode);
1415     idnaInfoToStruct(info, pInfo);
1416     return destString.extract(dest, capacity, *pErrorCode);
1417 }
1418 
1419 U_CAPI int32_t U_EXPORT2
uidna_labelToASCII_UTF8(const UIDNA * idna,const char * label,int32_t length,char * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1420 uidna_labelToASCII_UTF8(const UIDNA *idna,
1421                         const char *label, int32_t length,
1422                         char *dest, int32_t capacity,
1423                         UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1424     if(!checkArgs(label, length, dest, capacity, pInfo, pErrorCode)) {
1425         return 0;
1426     }
1427     StringPiece src(label, length<0 ? static_cast<int32_t>(uprv_strlen(label)) : length);
1428     CheckedArrayByteSink sink(dest, capacity);
1429     IDNAInfo info;
1430     reinterpret_cast<const IDNA *>(idna)->labelToASCII_UTF8(src, sink, info, *pErrorCode);
1431     idnaInfoToStruct(info, pInfo);
1432     return u_terminateChars(dest, capacity, sink.NumberOfBytesAppended(), pErrorCode);
1433 }
1434 
1435 U_CAPI int32_t U_EXPORT2
uidna_labelToUnicodeUTF8(const UIDNA * idna,const char * label,int32_t length,char * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1436 uidna_labelToUnicodeUTF8(const UIDNA *idna,
1437                          const char *label, int32_t length,
1438                          char *dest, int32_t capacity,
1439                          UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1440     if(!checkArgs(label, length, dest, capacity, pInfo, pErrorCode)) {
1441         return 0;
1442     }
1443     StringPiece src(label, length<0 ? static_cast<int32_t>(uprv_strlen(label)) : length);
1444     CheckedArrayByteSink sink(dest, capacity);
1445     IDNAInfo info;
1446     reinterpret_cast<const IDNA *>(idna)->labelToUnicodeUTF8(src, sink, info, *pErrorCode);
1447     idnaInfoToStruct(info, pInfo);
1448     return u_terminateChars(dest, capacity, sink.NumberOfBytesAppended(), pErrorCode);
1449 }
1450 
1451 U_CAPI int32_t U_EXPORT2
uidna_nameToASCII_UTF8(const UIDNA * idna,const char * name,int32_t length,char * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1452 uidna_nameToASCII_UTF8(const UIDNA *idna,
1453                        const char *name, int32_t length,
1454                        char *dest, int32_t capacity,
1455                        UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1456     if(!checkArgs(name, length, dest, capacity, pInfo, pErrorCode)) {
1457         return 0;
1458     }
1459     StringPiece src(name, length<0 ? static_cast<int32_t>(uprv_strlen(name)) : length);
1460     CheckedArrayByteSink sink(dest, capacity);
1461     IDNAInfo info;
1462     reinterpret_cast<const IDNA *>(idna)->nameToASCII_UTF8(src, sink, info, *pErrorCode);
1463     idnaInfoToStruct(info, pInfo);
1464     return u_terminateChars(dest, capacity, sink.NumberOfBytesAppended(), pErrorCode);
1465 }
1466 
1467 U_CAPI int32_t U_EXPORT2
uidna_nameToUnicodeUTF8(const UIDNA * idna,const char * name,int32_t length,char * dest,int32_t capacity,UIDNAInfo * pInfo,UErrorCode * pErrorCode)1468 uidna_nameToUnicodeUTF8(const UIDNA *idna,
1469                         const char *name, int32_t length,
1470                         char *dest, int32_t capacity,
1471                         UIDNAInfo *pInfo, UErrorCode *pErrorCode) {
1472     if(!checkArgs(name, length, dest, capacity, pInfo, pErrorCode)) {
1473         return 0;
1474     }
1475     StringPiece src(name, length<0 ? static_cast<int32_t>(uprv_strlen(name)) : length);
1476     CheckedArrayByteSink sink(dest, capacity);
1477     IDNAInfo info;
1478     reinterpret_cast<const IDNA *>(idna)->nameToUnicodeUTF8(src, sink, info, *pErrorCode);
1479     idnaInfoToStruct(info, pInfo);
1480     return u_terminateChars(dest, capacity, sink.NumberOfBytesAppended(), pErrorCode);
1481 }
1482 
1483 #endif  // UCONFIG_NO_IDNA
1484