Guest User

Untitled

a guest
Jan 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. //Javascript Punycode converter derived from example in RFC3492.
  2. //This implementation is created by some@domain.name and released into public domain
  3. var punycode = new function Punycode() {
  4. // This object converts to and from puny-code used in IDN
  5. //
  6. // punycode.ToASCII ( domain )
  7. //
  8. // Returns a puny coded representation of "domain".
  9. // It only converts the part of the domain name that
  10. // has non ASCII characters. I.e. it dosent matter if
  11. // you call it with a domain that already is in ASCII.
  12. //
  13. // punycode.ToUnicode (domain)
  14. //
  15. // Converts a puny-coded domain name to unicode.
  16. // It only converts the puny-coded parts of the domain name.
  17. // I.e. it dosent matter if you call it on a string
  18. // that already has been converted to unicode.
  19. //
  20. //
  21. this.utf16 = {
  22. // The utf16-class is necessary to convert from javascripts internal character representation to unicode and back.
  23. decode:function(input){
  24. var output = [], i=0, len=input.length,value,extra;
  25. while (i < len) {
  26. value = input.charCodeAt(i++);
  27. if ((value & 0xF800) === 0xD800) {
  28. extra = input.charCodeAt(i++);
  29. if ( ((value & 0xFC00) !== 0xD800) || ((extra & 0xFC00) !== 0xDC00) ) {
  30. throw new RangeError("UTF-16(decode): Illegal UTF-16 sequence");
  31. }
  32. value = ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000;
  33. }
  34. output.push(value);
  35. }
  36. return output;
  37. },
  38. encode:function(input){
  39. var output = [], i=0, len=input.length,value;
  40. while (i < len) {
  41. value = input[i++];
  42. if ( (value & 0xF800) === 0xD800 ) {
  43. throw new RangeError("UTF-16(encode): Illegal UTF-16 value");
  44. }
  45. if (value > 0xFFFF) {
  46. value -= 0x10000;
  47. output.push(String.fromCharCode(((value >>>10) & 0x3FF) | 0xD800));
  48. value = 0xDC00 | (value & 0x3FF);
  49. }
  50. output.push(String.fromCharCode(value));
  51. }
  52. return output.join("");
  53. }
  54. }
  55.  
  56. //Default parameters
  57. var initial_n = 0x80;
  58. var initial_bias = 72;
  59. var delimiter = "x2D";
  60. var base = 36;
  61. var damp = 700;
  62. var tmin=1;
  63. var tmax=26;
  64. var skew=38;
  65. var maxint = 0x7FFFFFFF;
  66.  
  67. // decode_digit(cp) returns the numeric value of a basic code
  68. // point (for use in representing integers) in the range 0 to
  69. // base-1, or base if cp is does not represent a value.
  70.  
  71. function decode_digit(cp) {
  72. return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 : cp - 97 < 26 ? cp - 97 : base;
  73. }
  74.  
  75. // encode_digit(d,flag) returns the basic code point whose value
  76. // (when used for representing integers) is d, which needs to be in
  77. // the range 0 to base-1. The lowercase form is used unless flag is
  78. // nonzero, in which case the uppercase form is used. The behavior
  79. // is undefined if flag is nonzero and digit d has no uppercase form.
  80.  
  81. function encode_digit(d, flag) {
  82. return d + 22 + 75 * (d < 26) - ((flag != 0) << 5);
  83. // 0..25 map to ASCII a..z or A..Z
  84. // 26..35 map to ASCII 0..9
  85. }
  86. //** Bias adaptation function **
  87. function adapt(delta, numpoints, firsttime ) {
  88. var k;
  89. delta = firsttime ? Math.floor(delta / damp) : (delta >> 1);
  90. delta += Math.floor(delta / numpoints);
  91.  
  92. for (k = 0; delta > (((base - tmin) * tmax) >> 1); k += base) {
  93. delta = Math.floor(delta / ( base - tmin ));
  94. }
  95. return Math.floor(k + (base - tmin + 1) * delta / (delta + skew));
  96. }
  97.  
  98. // encode_basic(bcp,flag) forces a basic code point to lowercase if flag is zero,
  99. // uppercase if flag is nonzero, and returns the resulting code point.
  100. // The code point is unchanged if it is caseless.
  101. // The behavior is undefined if bcp is not a basic code point.
  102.  
  103. function encode_basic(bcp, flag) {
  104. bcp -= (bcp - 97 < 26) << 5;
  105. return bcp + ((!flag && (bcp - 65 < 26)) << 5);
  106. }
  107.  
  108. // Main decode
  109. this.decode=function(input,preserveCase) {
  110. // Dont use utf16
  111. var output=[];
  112. var case_flags=[];
  113. var input_length = input.length;
  114.  
  115. var n, out, i, bias, basic, j, ic, oldi, w, k, digit, t, len;
  116.  
  117. // Initialize the state:
  118.  
  119. n = initial_n;
  120. i = 0;
  121. bias = initial_bias;
  122.  
  123. // Handle the basic code points: Let basic be the number of input code
  124. // points before the last delimiter, or 0 if there is none, then
  125. // copy the first basic code points to the output.
  126.  
  127. basic = input.lastIndexOf(delimiter);
  128. if (basic < 0) basic = 0;
  129.  
  130. for (j = 0; j < basic; ++j) {
  131. if(preserveCase) case_flags[output.length] = ( input.charCodeAt(j) -65 < 26);
  132. if ( input.charCodeAt(j) >= 0x80) {
  133. throw new RangeError("Illegal input >= 0x80");
  134. }
  135. output.push( input.charCodeAt(j) );
  136. }
  137.  
  138. // Main decoding loop: Start just after the last delimiter if any
  139. // basic code points were copied; start at the beginning otherwise.
  140.  
  141. for (ic = basic > 0 ? basic + 1 : 0; ic < input_length; ) {
  142.  
  143. // ic is the index of the next character to be consumed,
  144.  
  145. // Decode a generalized variable-length integer into delta,
  146. // which gets added to i. The overflow checking is easier
  147. // if we increase i as we go, then subtract off its starting
  148. // value at the end to obtain delta.
  149. for (oldi = i, w = 1, k = base; ; k += base) {
  150. if (ic >= input_length) {
  151. throw RangeError ("punycode_bad_input(1)");
  152. }
  153. digit = decode_digit(input.charCodeAt(ic++));
  154.  
  155. if (digit >= base) {
  156. throw RangeError("punycode_bad_input(2)");
  157. }
  158. if (digit > Math.floor((maxint - i) / w)) {
  159. throw RangeError ("punycode_overflow(1)");
  160. }
  161. i += digit * w;
  162. t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
  163. if (digit < t) { break; }
  164. if (w > Math.floor(maxint / (base - t))) {
  165. throw RangeError("punycode_overflow(2)");
  166. }
  167. w *= (base - t);
  168. }
  169.  
  170. out = output.length + 1;
  171. bias = adapt(i - oldi, out, oldi === 0);
  172.  
  173. // i was supposed to wrap around from out to 0,
  174. // incrementing n each time, so we'll fix that now:
  175. if ( Math.floor(i / out) > maxint - n) {
  176. throw RangeError("punycode_overflow(3)");
  177. }
  178. n += Math.floor( i / out ) ;
  179. i %= out;
  180.  
  181. // Insert n at position i of the output:
  182. // Case of last character determines uppercase flag:
  183. if (preserveCase) { case_flags.splice(i, 0, input.charCodeAt(ic -1) -65 < 26);}
  184.  
  185. output.splice(i, 0, n);
  186. i++;
  187. }
  188. if (preserveCase) {
  189. for (i = 0, len = output.length; i < len; i++) {
  190. if (case_flags[i]) {
  191. output[i] = (String.fromCharCode(output[i]).toUpperCase()).charCodeAt(0);
  192. }
  193. }
  194. }
  195. return this.utf16.encode(output);
  196. };
  197.  
  198. //** Main encode function **
  199.  
  200. this.encode = function (input,preserveCase) {
  201. //** Bias adaptation function **
  202.  
  203. var n, delta, h, b, bias, j, m, q, k, t, ijv, case_flags;
  204.  
  205. if (preserveCase) {
  206. // Preserve case, step1 of 2: Get a list of the unaltered string
  207. case_flags = this.utf16.decode(input);
  208. }
  209. // Converts the input in UTF-16 to Unicode
  210. input = this.utf16.decode(input.toLowerCase());
  211.  
  212. var input_length = input.length; // Cache the length
  213.  
  214. if (preserveCase) {
  215. // Preserve case, step2 of 2: Modify the list to true/false
  216. for (j=0; j < input_length; j++) {
  217. case_flags[j] = input[j] != case_flags[j];
  218. }
  219. }
  220.  
  221. var output=[];
  222.  
  223.  
  224. // Initialize the state:
  225. n = initial_n;
  226. delta = 0;
  227. bias = initial_bias;
  228.  
  229. // Handle the basic code points:
  230. for (j = 0; j < input_length; ++j) {
  231. if ( input[j] < 0x80) {
  232. output.push(
  233. String.fromCharCode(
  234. case_flags ? encode_basic(input[j], case_flags[j]) : input[j]
  235. )
  236. );
  237. }
  238. }
  239.  
  240. h = b = output.length;
  241.  
  242. // h is the number of code points that have been handled, b is the
  243. // number of basic code points
  244.  
  245. if (b > 0) output.push(delimiter);
  246.  
  247. // Main encoding loop:
  248. //
  249. while (h < input_length) {
  250. // All non-basic code points < n have been
  251. // handled already. Find the next larger one:
  252.  
  253. for (m = maxint, j = 0; j < input_length; ++j) {
  254. ijv = input[j];
  255. if (ijv >= n && ijv < m) m = ijv;
  256. }
  257.  
  258. // Increase delta enough to advance the decoder's
  259. // <n,i> state to <m,0>, but guard against overflow:
  260.  
  261. if (m - n > Math.floor((maxint - delta) / (h + 1))) {
  262. throw RangeError("punycode_overflow (1)");
  263. }
  264. delta += (m - n) * (h + 1);
  265. n = m;
  266.  
  267. for (j = 0; j < input_length; ++j) {
  268. ijv = input[j];
  269.  
  270. if (ijv < n ) {
  271. if (++delta > maxint) return Error("punycode_overflow(2)");
  272. }
  273.  
  274. if (ijv == n) {
  275. // Represent delta as a generalized variable-length integer:
  276. for (q = delta, k = base; ; k += base) {
  277. t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias;
  278. if (q < t) break;
  279. output.push( String.fromCharCode(encode_digit(t + (q - t) % (base - t), 0)) );
  280. q = Math.floor( (q - t) / (base - t) );
  281. }
  282. output.push( String.fromCharCode(encode_digit(q, preserveCase && case_flags[j] ? 1:0 )));
  283. bias = adapt(delta, h + 1, h == b);
  284. delta = 0;
  285. ++h;
  286. }
  287. }
  288.  
  289. ++delta, ++n;
  290. }
  291. return output.join("");
  292. }
  293.  
  294. this.ToASCII = function ( domain ) {
  295. var domain_array = domain.split(".");
  296. var out = [];
  297. for (var i=0; i < domain_array.length; ++i) {
  298. var s = domain_array[i];
  299. out.push(
  300. s.match(/[^A-Za-z0-9-]/) ?
  301. "xn--" + punycode.encode(s) :
  302. s
  303. );
  304. }
  305. return out.join(".");
  306. }
  307. this.ToUnicode = function ( domain ) {
  308. var domain_array = domain.split(".");
  309. var out = [];
  310. for (var i=0; i < domain_array.length; ++i) {
  311. var s = domain_array[i];
  312. out.push(
  313. s.match(/^xn--/) ?
  314. punycode.decode(s.slice(4)) :
  315. s
  316. );
  317. }
  318. return out.join(".");
  319. }
  320. }();
Add Comment
Please, Sign In to add comment