uts46.cpp 56 KB

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