jquery.base64.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*!
  2. * jquery.base64.js 0.1 - 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
  31. .replace(
  32. /[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
  33. function (c) {
  34. var cc = c.charCodeAt(0);
  35. return String.fromCharCode(0xc0 | (cc >> 6), 0x80 | (cc & 0x3f));
  36. }
  37. )
  38. .replace(
  39. /[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
  40. function (c) {
  41. var cc = c.charCodeAt(0);
  42. return String.fromCharCode(
  43. 0xe0 | (cc >> 12),
  44. 0x80 | ((cc >> 6) & 0x3f),
  45. 0x80 | (cc & 0x3f)
  46. );
  47. }
  48. );
  49. return strUtf;
  50. },
  51. /**
  52. * Decode utf-8 encoded string back into multi-byte Unicode characters
  53. *
  54. * @param {String} strUtf UTF-8 string to be decoded back to Unicode
  55. * @returns {String} decoded string
  56. */
  57. decode: function (strUtf) {
  58. // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
  59. var strUni = strUtf
  60. .replace(
  61. /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
  62. function (c) {
  63. // (note parentheses for precence)
  64. var cc =
  65. ((c.charCodeAt(0) & 0x0f) << 12) |
  66. ((c.charCodeAt(1) & 0x3f) << 6) |
  67. (c.charCodeAt(2) & 0x3f);
  68. return String.fromCharCode(cc);
  69. }
  70. )
  71. .replace(
  72. /[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
  73. function (c) {
  74. // (note parentheses for precence)
  75. var cc = ((c.charCodeAt(0) & 0x1f) << 6) | (c.charCodeAt(1) & 0x3f);
  76. return String.fromCharCode(cc);
  77. }
  78. );
  79. return strUni;
  80. },
  81. };
  82. while (i < 256) {
  83. var c = String.fromCharCode(i);
  84. a256 += c;
  85. r256[i] = i;
  86. r64[i] = b64.indexOf(c);
  87. ++i;
  88. }
  89. function code(s, discard, alpha, beta, w1, w2) {
  90. s = String(s);
  91. var buffer = 0,
  92. i = 0,
  93. length = s.length,
  94. result = "",
  95. bitsInBuffer = 0;
  96. while (i < length) {
  97. var c = s.charCodeAt(i);
  98. c = c < 256 ? alpha[c] : -1;
  99. buffer = (buffer << w1) + c;
  100. bitsInBuffer += w1;
  101. while (bitsInBuffer >= w2) {
  102. bitsInBuffer -= w2;
  103. var tmp = buffer >> bitsInBuffer;
  104. result += beta.charAt(tmp);
  105. buffer ^= tmp << bitsInBuffer;
  106. }
  107. ++i;
  108. }
  109. if (!discard && bitsInBuffer > 0)
  110. result += beta.charAt(buffer << (w2 - bitsInBuffer));
  111. return result;
  112. }
  113. var Plugin = ($.base64 = function (dir, input, encode) {
  114. return input ? Plugin[dir](input, encode) : dir ? null : this;
  115. });
  116. Plugin.btoa = Plugin.encode = function (plain, utf8encode) {
  117. plain =
  118. Plugin.raw === false || Plugin.utf8encode || utf8encode
  119. ? UTF8.encode(plain)
  120. : plain;
  121. plain = code(plain, false, r256, b64, 8, 6);
  122. return plain + "====".slice(plain.length % 4 || 4);
  123. };
  124. Plugin.atob = Plugin.decode = function (coded, utf8decode) {
  125. coded = String(coded).split("=");
  126. var i = coded.length;
  127. do {
  128. --i;
  129. coded[i] = code(coded[i], true, r64, a256, 6, 8);
  130. } while (i > 0);
  131. coded = coded.join("");
  132. return Plugin.raw === false || Plugin.utf8decode || utf8decode
  133. ? UTF8.decode(coded)
  134. : coded;
  135. };
  136. })(jQuery);