/*! * jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js * makes base64 en & -decoding simpler as it is. * * based upon: https://gist.github.com/yaffle/1284012 * * copyright (c) 2012 yannick albert (http://yckart.com) * licensed under the mit license (http://www.opensource.org/licenses/mit-license.php). * 2013/02/10 **/ ;(function($) { var b64 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/", a256 = '', r64 = [256], r256 = [256], i = 0; var utf8 = { /** * encode multi-byte unicode string into utf-8 multiple single-byte characters * (bmp / basic multilingual plane only) * * chars in range u+0080 - u+07ff are encoded in 2 chars, u+0800 - u+ffff in 3 chars * * @param {string} struni unicode string to be encoded as utf-8 * @returns {string} encoded string */ encode: function(struni) { // use regular expressions & string.replace callback function for better efficiency // than procedural approaches var strutf = struni.replace(/[\u0080-\u07ff]/g, // u+0080 - u+07ff => 2 bytes 110yyyyy, 10zzzzzz function(c) { var cc = c.charcodeat(0); return string.fromcharcode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); }) .replace(/[\u0800-\uffff]/g, // u+0800 - u+ffff => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz function(c) { var cc = c.charcodeat(0); return string.fromcharcode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3f, 0x80 | cc & 0x3f); }); return strutf; }, /** * decode utf-8 encoded string back into multi-byte unicode characters * * @param {string} strutf utf-8 string to be decoded back to unicode * @returns {string} decoded string */ decode: function(strutf) { // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char! var struni = strutf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars function(c) { // (note parentheses for precence) var cc = ((c.charcodeat(0) & 0x0f) << 12) | ((c.charcodeat(1) & 0x3f) << 6) | (c.charcodeat(2) & 0x3f); return string.fromcharcode(cc); }) .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars function(c) { // (note parentheses for precence) var cc = (c.charcodeat(0) & 0x1f) << 6 | c.charcodeat(1) & 0x3f; return string.fromcharcode(cc); }); return struni; } }; while(i < 256) { var c = string.fromcharcode(i); a256 += c; r256[i] = i; r64[i] = b64.indexof(c); ++i; } function code(s, discard, alpha, beta, w1, w2) { s = string(s); var buffer = 0, i = 0, length = s.length, result = '', bitsinbuffer = 0; while(i < length) { var c = s.charcodeat(i); c = c < 256 ? alpha[c] : -1; buffer = (buffer << w1) + c; bitsinbuffer += w1; while(bitsinbuffer >= w2) { bitsinbuffer -= w2; var tmp = buffer >> bitsinbuffer; result += beta.charat(tmp); buffer ^= tmp << bitsinbuffer; } ++i; } if(!discard && bitsinbuffer > 0) result += beta.charat(buffer << (w2 - bitsinbuffer)); return result; } var plugin = $.base64 = function(dir, input, encode) { return input ? plugin[dir](input, encode) : dir ? null : this; }; plugin.btoa = plugin.encode = function(plain, utf8encode) { plain = plugin.raw === false || plugin.utf8encode || utf8encode ? utf8.encode(plain) : plain; plain = code(plain, false, r256, b64, 8, 6); return plain + '===='.slice((plain.length % 4) || 4); }; plugin.atob = plugin.decode = function(coded, utf8decode) { coded = string(coded).split('='); var i = coded.length; do {--i; coded[i] = code(coded[i], true, r64, a256, 6, 8); } while (i > 0); coded = coded.join(''); return plugin.raw === false || plugin.utf8decode || utf8decode ? utf8.decode(coded) : coded; }; }(jquery));