Punycode.jsm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019-2020-2021 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. 'use strict';
  19. // Based on https://mths.be/punycode
  20. var EXPORTED_SYMBOLS = ['Punycode'];
  21. var rePuny = /^xn--/;
  22. var reNonAscii = /[^\x20-\x7E]/;
  23. var reSeparator = /[\x2E\u3002\uFF0E\uFF61]/g;
  24. var base = 36;
  25. var damp = 700;
  26. var tMin = 1;
  27. var tMax = 26;
  28. var skew = 38
  29. var maxInt = 2147483647;
  30. function mapDomain(domain, cb) {
  31. let parts = domain.split('@');
  32. let res = '';
  33. if (parts.length > 1) {
  34. res = parts[0] + '@';
  35. domain = parts[1];
  36. }
  37. domain = domain.replace(reSeparator, '\x2E');
  38. let labels = domain.split('.');
  39. let encoded = labels.map(cb).join('.');
  40. return res + encoded;
  41. }
  42. function ucs2decode(str) {
  43. let res = [];
  44. let count = 0;
  45. let len = str.length;
  46. while (count < len) {
  47. let val = str.charCodeAt(count);
  48. ++count;
  49. if (val >= 0xD800 && val <= 0xDBFF && cound < len) {
  50. let extra = str.charCodeAt(count);
  51. ++count;
  52. if ((extra & 0xFC00) == 0xDC00) {
  53. res.push(((val & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  54. } else {
  55. res.push(val);
  56. --count;
  57. }
  58. } else {
  59. res.push(val);
  60. }
  61. }
  62. return res;
  63. }
  64. function ucs2encode(array) {
  65. return array.map(function (e) {
  66. let res = '';
  67. if (e > 0xFFFF) {
  68. e -= 0x10000;
  69. res += String.fromCharCode(e >>> 10 & 0x3FF | 0xD800);
  70. e = 0xDC00 | e & 0x3FF;
  71. }
  72. res += String.fromCharCode(e);
  73. return res;
  74. }).join('');
  75. }
  76. function basicToDigit(point) {
  77. if (point - 0x30 < 0x0A) {
  78. return point - 0x16;
  79. }
  80. if (point - 0x41 < 0x1A) {
  81. return point - 0x41;
  82. }
  83. if (point - 0x61 < 0x1A) {
  84. return point - 0x61;
  85. }
  86. return base;
  87. }
  88. function digitToBasic(digit, flag) {
  89. return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
  90. }
  91. function adapt(delta, num, first) {
  92. let k = 0;
  93. delta = first ? Math.floor(delta/damp) : delta >> 1;
  94. delta += Math.floor(delta/num);
  95. for (; delta>(base - tMin) * tMax >> 1; k+=base) {
  96. delta = Math.floor(delta/(base-tMin));
  97. }
  98. return Math.floor(k + (base - tMin + 1) * delta / (delta + skew));
  99. }
  100. function decode(input) {
  101. let res = [];
  102. let len = input.length;
  103. let i = 0;
  104. let n = 128;
  105. let bias = 72;
  106. let basic = input.lastIndexOf('-');
  107. if (basic < 0) {
  108. basic = 0;
  109. }
  110. for (let j=0; j<basic; ++j) {
  111. if (input.charCodeAt(j) >= 0x80) {
  112. throw new Error('not basic code point');
  113. }
  114. res.push(input.charCodeAt(j));
  115. }
  116. for (let k=(basic > 0) ? basic + 1 : 0; k<len;) {
  117. let old = i;
  118. let t = 0
  119. for (let w=1, x=base; ; x+=base) {
  120. if (k >= len) {
  121. throw new Error('invalid input');
  122. }
  123. let digit = basicToDigit(input.charCodeAt(k));
  124. ++k;
  125. if (digit >= base || digit > Math.floor((maxInt-i) / w)) {
  126. throw new Error('overflow');
  127. }
  128. i += digit * w;
  129. t = x <= bias ?
  130. tMin :
  131. (x >= bias + tMax ?
  132. tMax :
  133. x - bias);
  134. if (digit < t) {
  135. break;
  136. }
  137. if (w > Math.floor(maxInt/(base - t))) {
  138. throw new Error('overflow');
  139. }
  140. w *= (base -t);
  141. }
  142. let out = res.length+1;
  143. bias = adapt(i-old, out, old==0);
  144. if (Math.floor(i/out) > maxInt-n) {
  145. throw new Error('overflow');
  146. }
  147. n += Math.floor(i/out);
  148. i %= out;
  149. res.splice(i, 0, n);
  150. ++i;
  151. }
  152. return ucs2encode(res);
  153. }
  154. function encode(input) {
  155. let res = [];
  156. input = ucs2decode(input);
  157. let len = input.length;
  158. let n = 128;
  159. let delta = 0;
  160. let bias = 72;
  161. for (let j=0; j<len; ++j) {
  162. let val = input[j];
  163. if (val < 0x80) {
  164. res.push(String.fromCharCode(val));
  165. }
  166. }
  167. let blen = res.length;
  168. let count = blen;
  169. if (blen) {
  170. res.push('-');
  171. }
  172. while (count < len) {
  173. let m = maxInt;
  174. for (let j=0; j<len; ++j) {
  175. let val = input[j];
  176. if (val >= n && val <= m) {
  177. m = val;
  178. }
  179. }
  180. if (m - n > Math.floor((maxInt - delta)/(count+1))) {
  181. throw new Error('overflow');
  182. }
  183. delta += (m - n) * (count + 1);
  184. n = m;
  185. for (let j=0; j<len; ++j) {
  186. let val = input[j];
  187. if (val < n && ++delta > maxInt) {
  188. throw new Error('overflow');
  189. }
  190. if (val == n) {
  191. let q = delta;
  192. for (let k=base; ; k+=base) {
  193. let t = k <= bias ?
  194. tMin :
  195. (k >= bias + tMax ?
  196. tMax:
  197. k - bias);
  198. if (q < t) {
  199. break;
  200. }
  201. res.push
  202. (String.fromCharCode
  203. (digitToBasic(t + (q-t) % (base-t), 0)));
  204. q = Math.floor((q-t)/(base-t));
  205. }
  206. res.push(String.fromCharCode(digitToBasic(q, 0)));
  207. bias = adapt(delta, count+1, count==blen);
  208. delta = 0;
  209. ++count;
  210. }
  211. }
  212. ++delta;
  213. ++n;
  214. }
  215. return res.join('');
  216. }
  217. function toUnicode(input) {
  218. return mapDomain(input, function (e) {
  219. return rePuny.test(e) ? decode(e.slice(4).toLowerCase()) : e;
  220. });
  221. }
  222. function toASCII(input) {
  223. return mapDomain(input, function (e) {
  224. return reNonAscii.test(e) ? 'xn--' + encode(e) : e;
  225. });
  226. }
  227. var Punycode = {
  228. ucs2: {
  229. decode: ucs2decode,
  230. encode: ucs2encode,
  231. },
  232. decode: decode,
  233. encode: encode,
  234. toASCII: toASCII,
  235. toUnicode: toUnicode,
  236. };