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