-
Notifications
You must be signed in to change notification settings - Fork 8
/
lzjs.min.js
7 lines (7 loc) · 9.52 KB
/
lzjs.min.js
1
2
3
4
5
6
7
/*!
* lzjs v1.3.1 - compressing strings using an original algorithm based on the LZ algorithm
* Copyright (c) 2014 polygonplanet <polygon.planet.aqua@gmail.com>
* https://github.com/polygonplanet/lzjs
* @license MIT
*/
!function(t,e,n){"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports?module.exports=n():exports[t]=n():"function"==typeof define&&define.amd?define(n):e[t]=n()}("lzjs",this,function(){var g=String.fromCharCode,p=Object.prototype.hasOwnProperty,n="undefined"!=typeof Uint8Array&&"undefined"!=typeof Uint16Array,o=!1,l=!1;try{"a"===g.apply(null,[97])&&(o=!0)}catch(t){}if(n)try{"a"===g.apply(null,new Uint8Array([97]))&&(l=!0)}catch(t){}var c=65533,u=null,f=!1,r=(-1!=="abc\u307b\u3052".lastIndexOf("\u307b\u3052",1)&&(f=!0),function(){for(var t="",e={8:1,10:1,11:1,12:1,13:1,92:1},n=0;n<127;n++)p.call(e,n)||(t+=g(n));return t}()),d=r.length,t=Math.max(d,62)-Math.min(d,62),A=d-1,m=1024,_=c,y=_-d,s=c,b=s+2*m,C=40,v=1640,x=d+1,w=20+t,k=d+5,B=d-t-19;function h(t){this._init(t)}function e(t){this._init(t)}function D(t){this._init(t)}function S(t){this._init(t)}function E(){for(var t,e,n="abcdefghijklmnopqrstuvwxyz",r="",s=n.length,a=0;a<s;a++)for(e=n.charAt(a),t=s-1;15<t&&r.length<m;t--)r+=" "+e+" "+n.charAt(t);for(;r.length<m;)r=" "+r;return r=r.slice(0,m)}function U(t,e){if(null==e?e=t.length:t=function(t,e){if(t.length!==e){if(t.subarray)return t.subarray(0,e);t.length=e}return t}(t,e),o&&l&&e<c){if(u)return g.apply(null,t);if(null===u)try{var n=g.apply(null,t);return c<e&&(u=!0),n}catch(t){u=!1}}for(var r,s=t,a="",i=s.length,h=0;h<i;){if(r=s.subarray?s.subarray(h,h+c):s.slice(h,h+c),h+=c,!u){if(null===u)try{a+=g.apply(null,r),r.length>c&&(u=!0);continue}catch(t){u=!1}return function(t){for(var e="",n=t.length,r=0;r<n;r++)e+=g(t[r]);return e}(s)}a+=g.apply(null,r)}return a}function M(t,e){if(!n)return new Array(e);switch(t){case 8:return new Uint8Array(e);case 16:return new Uint16Array(e)}}function N(t){for(var e,n,r=[],s=0,a=t.length;s<a;s++)55296<=(e=t.charCodeAt(s))&&e<=56319&&s+1<a&&56320<=(n=t.charCodeAt(s+1))&&n<=57343&&(e=1024*(e-55296)+n-56320+65536,s++),e<128?r[r.length]=e:e<2048?(r[r.length]=192|e>>6&31,r[r.length]=128|63&e):e<65536?(r[r.length]=224|e>>12&15,r[r.length]=128|e>>6&63,r[r.length]=128|63&e):e<2097152&&(r[r.length]=240|e>>18&15,r[r.length]=128|e>>12&63,r[r.length]=128|e>>6&63,r[r.length]=128|63&e);return U(r)}function a(t){for(var e,n,r,s=[],a=0,i=t.length;a<i;)0<=(e=(n=t.charCodeAt(a++))>>4)&&e<=7?r=n:12==e||13==e?r=(31&n)<<6|63&t.charCodeAt(a++):14==e?r=(15&n)<<12|(63&t.charCodeAt(a++))<<6|63&t.charCodeAt(a++):15==e&&(r=(7&n)<<18|(63&t.charCodeAt(a++))<<12|(63&t.charCodeAt(a++))<<6|63&t.charCodeAt(a++)),r<=65535?s[s.length]=r:(s[s.length]=55296+((r-=65536)>>10),s[s.length]=r%1024+56320);return U(s)}function O(t){for(var e,n,r=0,s=0,a=t.length;s<a;s++)55296==(64512&(e=t.charCodeAt(s)))&&s+1<a&&56320==(64512&(n=t.charCodeAt(s+1)))&&(e=65536+(e-55296<<10)+(n-56320),s++),e<128?r++:r+=e<2048?2:e<65536?3:4;return r}h.prototype={_init:function(t){t=t||{},this._data=null,this._table=null,this._result=null,this._onDataCallback=t.onData,this._onEndCallback=t.onEnd,this._maxBytes=t.maxBytes},_createTable:function(){for(var t=M(8,d),e=0;e<d;e++)t[e]=r.charCodeAt(e);return t},_onData:function(t,e){t=U(t,e);this._onDataCallback?this._onDataCallback(t):this._result+=t},_onEnd:function(){this._onEndCallback&&this._onEndCallback(),this._data=this._table=null},_search:function(){var t=2,e=this._data,n=this._offset,r=A;if((r=this._dataLen-n<r?this._dataLen-n:r)<t)return!1;var s,a,i,h,o,l=n-304,c=e.substring(l,n+r),u=n+t-3-l;do{if(2===t){if(a=e.charAt(n)+e.charAt(n+1),-1===(i=c.indexOf(a))||u<i)break}else 3===t?a+=e.charAt(n+2):a=e.substr(n,t);if(-1===(h=f?e.substring(l,n+t-1).lastIndexOf(a):c.lastIndexOf(a,u)))break;s=l+(o=h);do{if(e.charCodeAt(n+t)!==e.charCodeAt(s+t))break}while(++t<r);if(i===h){t++;break}}while(++t<r);return 2!==t&&(this._index=304-o,this._length=t-1,!0)},compress:function(t){if(null==t||0===t.length)return"";for(var e,n,r,s,a=this._createTable(),i=E(),h=M(8,_),o=0,l=0,c=(this._result="",this._offset=i.length,this._data=i+t,this._dataLen=this._data.length,t=null,-1),u=-1;this._offset<this._dataLen;){if(this._search()?(n=this._index<A?(e=this._index,0):(e=this._index%A,(this._index-e)/A),2===this._length?(h[o++]=a[n+54],h[o++]=a[e],l+=2):(h[o++]=a[n+49],h[o++]=a[e],h[o++]=a[this._length],l+=3),this._offset+=this._length,~u&&(u=-1)):(s=this._data.charCodeAt(this._offset++))<132?u===(c=s<C?(e=s,n=0,x):(n=(s-(e=s%C))/C)+x)?(h[o++]=a[e],l++):(h[o++]=a[c-w],h[o++]=a[e],l+=2,u=c):(c=s<v?(e=s,n=0,k):(n=(s-(e=s%v))/v)+k,s=e<C?(r=e,0):(e-(r=e%C))/C,u===c?(h[o++]=a[r],h[o++]=a[s],l+=2):(h[o++]=a[48],h[o++]=a[c-d],h[o++]=a[r],h[o++]=a[s],l+=4,u=c)),l>this._maxBytes)return!1;y<=o&&(this._onData(h,o),o=0)}return 0<o&&this._onData(h,o),this._onEnd(),i=this._result,(this._result=null)===i?"":i}},e.prototype={_init:function(t){t=t||{},this._result=null,this._onDataCallback=t.onData,this._onEndCallback=t.onEnd},_createTable:function(){for(var t={},e=0;e<d;e++)t[r.charAt(e)]=e;return t},_onData:function(t){var e;this._onDataCallback&&(t?(e=this._result,this._result=[]):(e=this._result.slice(m,m+(t=s-m)),this._result=this._result.slice(0,m).concat(this._result.slice(m+t))),0<e.length)&&this._onDataCallback(U(e))},_onEnd:function(){this._onEndCallback&&this._onEndCallback()},decompress:function(t){if(null==t||0===t.length)return"";this._result=function(t){for(var e=[],n=t&&t.length,r=0;r<n;r++)e[r]=t.charCodeAt(r);return e}(E());for(var e,n,r,s,a,i,h,o,l,c,u=this._createTable(),f=!1,d=null,_=t.length,g=0;g<_;g++)if(void 0!==(r=u[t.charAt(g)])){if(r<B)a=f?u[t.charAt(++g)]*C+r+v*d:d*C+r,this._result[this._result.length]=a;else if(r<47)d=r-B,f=!1;else if(48===r)d=(s=u[t.charAt(++g)])-5,f=!0;else if(r<59){if(s=u[t.charAt(++g)],h=r<54?(i=(r-49)*A+s,u[t.charAt(++g)]):(i=(r-54)*A+s,2),(o=this._result.slice(-i)).length>h&&(o.length=h),l=o.length,0<o.length)for(c=0;c<h;)for(n=0;n<l&&(this._result[this._result.length]=o[n],!(++c>=h));n++);d=null}this._result.length>=b&&this._onData()}return this._result=this._result.slice(m),this._onData(!0),this._onEnd(),e=U(this._result),this._result=null,e}},D.prototype={_init:function(t){this._codeStart=(t=t||{}).codeStart||255,this._codeMax=t.codeMax||65535,this._maxBytes=t.maxBytes},compress:function(t){if(null==t||0===t.length)return"";var e,n,r,s,a="",i=0,h=0,o=t.length,l="",c=this._codeStart+1,u=this._codeMax,f=2,d=[],_=0;for(0<o&&(l=e=t.charAt(h++));h<o;)if(((s=(n=l+(e=t.charAt(h++))).length)<32&&_&1<<s||32<=s&&void 0!==d[s])&&p.call(d[s],n))l+=e;else{if(1===l.length?(a+=l,i++):(a+=d[l.length][l],i+=f),i>this._maxBytes)return!1;c<=u&&(r=1<<(s=(n=l+e).length),(s<32&&!(_&r)||32<=s&&void 0===d[s])&&(d[s]={},_|=r),d[s][n]=g(c++),2048===c)&&(f=3),l=e}return 1===l.length?(a+=l,i++):(a+=d[l.length][l],i+=f),!(i>this._maxBytes)&&a},decompress:function(t){if(null==t||0===t.length)return"";var e,n,r,s,a="",i={},h=this._codeStart+1,o=this._codeStart,l=0,c=t.length;for(0<c&&(e=t.charCodeAt(l++),a+=n=g(e),r=n);l<c;)a+=s=(e=t.charCodeAt(l++))<=o?g(e):p.call(i,e)?i[e]:r+n,n=s.charAt(0),i[h++]=r+n,r=s;return a}},S.prototype={_init:function(t){this._encoding=(t=t||{}).encoding||"utf-8"},compress:function(t){var e,n,r,s,a,i;return null==t||0===t.length?"":(r=.9*(n=O(t=(e="")+t))|0,a={maxBytes:n},n===(s=t.length)?(i="W",a.codeStart=127,!(a.codeMax=2047)===(e=new D(a).compress(t))&&!(i="S")===(e=new h(a).compress(t))&&(i="N",e=t)):s<n&&r<s?!(i="U")===(e=new D(a).compress(N(t)))&&!(i="S")===(e=new h(a).compress(t))&&(i="N",e=t):!(i="S")===(e=new h(a).compress(t))&&(!(i="U")===(e=new D(a).compress(N(t)))||O(e)>n)&&(i="N",e=t),i+e)},decompress:function(t){if(null==t||0===t.length)return"";switch((t=""+t).charAt(0)){case"S":return this._decompressByS(t.substring(1));case"W":return this._decompressByW(t.substring(1));case"U":return this._decompressByU(t.substring(1));case"N":return this._decompressByN(t.substring(1));default:return t}},_decompressByS:function(t){return(new e).decompress(t)},_decompressByW:function(t){return new D({codeStart:127,codeMax:2047}).decompress(t)},_decompressByU:function(t){return a((new D).decompress(t))},_decompressByN:function(t){return t}};var T="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",L=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1];return{compress:function(t,e){return new S(e).compress(t)},decompress:function(t,e){return new S(e).decompress(t)},compressToBase64:function(t,e){for(var n,r,s,a=N(new S(e).compress(t)),i=a.length,h=0,o="";h<i;){if(n=255&a.charCodeAt(h++),h===i){o+=T.charAt(n>>2)+T.charAt((3&n)<<4)+"==";break}if(r=a.charCodeAt(h++),h===i){o+=T.charAt(n>>2)+T.charAt((3&n)<<4|(240&r)>>4)+T.charAt((15&r)<<2)+"=";break}s=a.charCodeAt(h++),o+=T.charAt(n>>2)+T.charAt((3&n)<<4|(240&r)>>4)+T.charAt((15&r)<<2|(192&s)>>6)+T.charAt(63&s)}return o},decompressFromBase64:function(t,e){return new S(e).decompress(a(function(t){for(var e,n,r,s,a=t.length,i=0,h="";i<a;){for(;e=L[255&t.charCodeAt(i++)],i<a&&-1===e;);if(-1===e)break;for(;n=L[255&t.charCodeAt(i++)],i<a&&-1===n;);if(-1===n)break;h+=g(e<<2|(48&n)>>4);do{if(61===(r=255&t.charCodeAt(i++)))return h}while(r=L[r],i<a&&-1===r);if(-1===r)break;h+=g((15&n)<<4|(60&r)>>2);do{if(61===(s=255&t.charCodeAt(i++)))return h}while(s=L[s],i<a&&-1===s);if(-1===s)break;h+=g((3&r)<<6|s)}return h}(t)))}}});