uts46.cpp 56 KB

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