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.
 
 
 
 

8 lines
2.3 KiB

/**
* Created by SLICE_30_K on 2017/5/22.
*
* 支持一般Base64的编码和解码
* 支持符合RFC_4648标准中"URL and Filename Safe Alphabet"的URL安全Base64编解码
* 支持中文字符的编解码(Unicode编码)
*/
(function(n,m){"object"===typeof exports?module.exports=exports=m():"function"===typeof define&&define.amd?define(m):window.BASE64=m()})(this,function(){var n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),m="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".split(""),q=function(b){for(var h=[];0<b;){var c=b%2;b=Math.floor(b/2);h.push(c)}h.reverse();return h},p=function(b){for(var h=0,c=0,a=b.length-1;0<=a;--a)1==b[a]&&(h+=Math.pow(2,c)),++c;return h},l=function(b,h){for(var c=8-(b+1)+6*(b-1)-h.length;0<=--c;)h.unshift(0);for(var c=[],a=b;0<=--a;)c.push(1);c.push(0);for(var a=0,e=8-(b+1);a<e;++a)c.push(h[a]);for(e=0;e<b-1;++e){c.push(1);c.push(0);for(var f=6;0<=--f;)c.push(h[a++])}return c},r=function(b,h){for(var c=[],a,e=[],f=0,g=b.length;f<g;++f){a=b.charCodeAt(f);var d=q(a);if(128>a){for(a=8-d.length;0<=--a;)d.unshift(0);e=e.concat(d)}else 128<=a&&2047>=a?e=e.concat(l(2,d)):2048<=a&&65535>=a?e=e.concat(l(3,d)):65536<=a&&2097151>=a?e=e.concat(l(4,d)):2097152<=a&&67108863>=a?e=e.concat(l(5,d)):4E6<=a&&2147483647>=a&&(e=e.concat(l(6,d)))}a=e;e=h?m:n;g=f=0;for(d=a.length;g<d;g+=6){var k=g+6-d;2==k?f=2:4==k&&(f=4);for(k=f;0<=--k;)a.push(0);c.push(p(a.slice(g,g+6)))}a="";g=0;for(d=c.length;g<d;++g)a+=e[c[g]];g=0;for(d=f/2;g<d;++g)a+="\x3d";return a},t=function(b,h){var c=b.length,a=0,e=h?m:n;"\x3d"==b.charAt(c-1)&&("\x3d"==b.charAt(c-2)?(a=4,b=b.substring(0,c-2)):(a=2,b=b.substring(0,c-1)));for(var c=[],f=0,g=b.length;f<g;++f)for(var d=b.charAt(f),k=0,l=e.length;k<l;++k)if(d==e[k]){d=q(k);k=d.length;if(0<6-k)for(k=6-k;0<k;--k)d.unshift(0);c=c.concat(d);break}0<a&&(c=c.slice(0,c.length-a));a=c;e=[];c="";f=0;for(g=a.length;f<g;)if(0==a[f])d=p(a.slice(f,f+8)),c+=String.fromCharCode(d),f+=8;else{for(d=0;f<g;){if(1==a[f])++d;else break;++f}e=e.concat(a.slice(f+1,f+8-d));for(f+=8-d;1<d;)e=e.concat(a.slice(f+2,f+8)),f+=8,--d;d=p(e);c+=String.fromCharCode(d);e=[]}return c};return{encode:function(b){return r(b,!1)},decode:function(b){return t(b,!1)},urlsafe_encode:function(b){return r(b,!0)},urlsafe_decode:function(b){return t(b,!0)}}});