You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.base64.js 4.5 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. * jquery.base64.js 0.0.3 - https://github.com/yckart/jquery.base64.js
  3. * Makes Base64 en & -decoding simpler as it is.
  4. *
  5. * Based upon: https://gist.github.com/Yaffle/1284012
  6. *
  7. * Copyright (c) 2012 Yannick Albert (http://yckart.com)
  8. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
  9. * 2013/02/10
  10. **/
  11. ;(function($) {
  12. var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  13. a256 = '',
  14. r64 = [256],
  15. r256 = [256],
  16. i = 0;
  17. var UTF8 = {
  18. /**
  19. * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
  20. * (BMP / basic multilingual plane only)
  21. *
  22. * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
  23. *
  24. * @param {String} strUni Unicode string to be encoded as UTF-8
  25. * @returns {String} encoded string
  26. */
  27. encode: function(strUni) {
  28. // use regular expressions & String.replace callback function for better efficiency
  29. // than procedural approaches
  30. var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
  31. function(c) {
  32. var cc = c.charCodeAt(0);
  33. return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
  34. })
  35. .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
  36. function(c) {
  37. var cc = c.charCodeAt(0);
  38. return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
  39. });
  40. return strUtf;
  41. },
  42. /**
  43. * Decode utf-8 encoded string back into multi-byte Unicode characters
  44. *
  45. * @param {String} strUtf UTF-8 string to be decoded back to Unicode
  46. * @returns {String} decoded string
  47. */
  48. decode: function(strUtf) {
  49. // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
  50. var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
  51. function(c) { // (note parentheses for precence)
  52. var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
  53. return String.fromCharCode(cc);
  54. })
  55. .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
  56. function(c) { // (note parentheses for precence)
  57. var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
  58. return String.fromCharCode(cc);
  59. });
  60. return strUni;
  61. }
  62. };
  63. while(i < 256) {
  64. var c = String.fromCharCode(i);
  65. a256 += c;
  66. r256[i] = i;
  67. r64[i] = b64.indexOf(c);
  68. ++i;
  69. }
  70. function code(s, discard, alpha, beta, w1, w2) {
  71. s = String(s);
  72. var buffer = 0,
  73. i = 0,
  74. length = s.length,
  75. result = '',
  76. bitsInBuffer = 0;
  77. while(i < length) {
  78. var c = s.charCodeAt(i);
  79. c = c < 256 ? alpha[c] : -1;
  80. buffer = (buffer << w1) + c;
  81. bitsInBuffer += w1;
  82. while(bitsInBuffer >= w2) {
  83. bitsInBuffer -= w2;
  84. var tmp = buffer >> bitsInBuffer;
  85. result += beta.charAt(tmp);
  86. buffer ^= tmp << bitsInBuffer;
  87. }
  88. ++i;
  89. }
  90. if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
  91. return result;
  92. }
  93. var Plugin = $.base64 = function(dir, input, encode) {
  94. return input ? Plugin[dir](input, encode) : dir ? null : this;
  95. };
  96. Plugin.btoa = Plugin.encode = function(plain, utf8encode) {
  97. plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
  98. plain = code(plain, false, r256, b64, 8, 6);
  99. return plain + '===='.slice((plain.length % 4) || 4);
  100. };
  101. Plugin.atob = Plugin.decode = function(coded, utf8decode) {
  102. coded = coded.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  103. coded = String(coded).split('=');
  104. var i = coded.length;
  105. do {--i;
  106. coded[i] = code(coded[i], true, r64, a256, 6, 8);
  107. } while (i > 0);
  108. coded = coded.join('');
  109. return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
  110. };
  111. }(jQuery));