├── .gitignore ├── lib └── js.jar ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── .classpath ├── .project ├── refence ├── LZString102.htm ├── LZString.htm ├── lz-string-1.3.3-min.js ├── lz-string-1.0.2.js ├── lz-string-1.4.4.js └── lz-string-1.3.3.js ├── README.md ├── LICENSE.md └── src └── rufus ├── lzstring4javarhinotest ├── LZString102InRhino.java ├── LZStringTest.java ├── LZStringInRhino.java └── LZStringInRhino144.java └── lzstring4java └── LZString.java /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /lib/js.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rufushuang/lz-string4java/HEAD/lib/js.jar -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | lz-string4java 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.6 12 | -------------------------------------------------------------------------------- /refence/LZString102.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | LZString_compress 6 | 7 | 8 | 9 |
to compress below
10 |
11 | 12 | 13 |
result:
14 | 15 | 16 | -------------------------------------------------------------------------------- /refence/LZString.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | LZString_compress 6 | 7 | 8 | 9 |
to compress below
10 |
11 | 12 | 13 |
result:
14 | 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | lz-string4java 2 | ========= 3 | Port javascript lz-string in JAVA version. 4 | 5 | Port from original JavaScript version by pieroxy 6 | https://github.com/pieroxy/lz-string 7 | 8 | Done some of language optimization after porting. 9 | 10 | Porting Version CAUTION !! 11 | ========= 12 | Original LZString is not all version compatible. 13 | 14 | From version 1.3.3 to version 1.4.4, there are some incompatible changes in compressToBase64/decompressFromBase64. Same as EncodedURIComponent. Use different will result in failure. 15 | 16 | So just keep appropriate/same version on your web page javascript side and server java side. 17 | 18 | IF YOU USE LZ-String VERSION WHICH BACKWARDS OF 1.4.4 and have problem with Base64/URIComponent, go to the release part and get the 1.3.3 release to have a try. 19 | 20 | ## License 21 | MIT License 22 | 23 | 24 | ## Rhino Testing 25 | See rufus.lzstring4javarhinotest.LZStringTest. Only the testing package need rhino. 26 | 27 | If you're not intrestring in testing, just get the rufus.lzstring4java package only, no rhino or any other third party package is needed. -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 rufushuang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/rufus/lzstring4javarhinotest/LZString102InRhino.java: -------------------------------------------------------------------------------- 1 | /* 2 | * LZString4Java By Rufus Huang 3 | * https://github.com/rufushuang/lz-string4java 4 | * MIT License 5 | * 6 | * Port from original JavaScript version by pieroxy 7 | * https://github.com/pieroxy/lz-string 8 | */ 9 | 10 | package rufus.lzstring4javarhinotest; 11 | 12 | import java.io.IOException; 13 | 14 | import org.mozilla.javascript.Context; 15 | import org.mozilla.javascript.Function; 16 | import org.mozilla.javascript.NativeObject; 17 | import org.mozilla.javascript.Script; 18 | import org.mozilla.javascript.Scriptable; 19 | 20 | 21 | public class LZString102InRhino { 22 | 23 | protected static Script COMPILED_LZ; 24 | protected static Context context; 25 | protected static Scriptable scope; 26 | protected static NativeObject OBJ_LZString; 27 | // protected static Function FUNC_compressToBase64; 28 | // protected static Function FUNC_decompressFromBase64; 29 | // protected static Function FUNC_compressToUTF16; 30 | // protected static Function FUNC_decompressFromUTF16; 31 | protected static Function FUNC_compress; 32 | protected static Function FUNC_decompress; 33 | static { 34 | // should change above JDK6, or just use Nashorn instead of Rhino? 35 | context = Context.enter(); 36 | scope = context.initStandardObjects(); 37 | context.evaluateString(scope, LZStringInRhino.LZ_STRING_JS, "script", 1, null); 38 | OBJ_LZString = (NativeObject) scope.get("LZString", scope); 39 | FUNC_compress = (Function) OBJ_LZString.get("compress", scope); 40 | FUNC_decompress = (Function) OBJ_LZString.get("decompress", scope); 41 | } 42 | 43 | public static String compress(String input) { 44 | return (String) FUNC_compress.call(context, scope, scope, new Object[] { input }); 45 | } 46 | 47 | public static String decompress(String input) { 48 | return (String) FUNC_decompress.call(context, scope, scope, new Object[] { input }); 49 | } 50 | 51 | public static void main(String[] args) throws IOException { 52 | String input; 53 | // input = "hello"; 54 | input = "hello1hello2hello3hello4hello5hello6hello7hello8hello9helloAhelloBhelloChelloDhelloEhelloF"; 55 | 56 | System.out.println(decompress(compress(input))); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/rufus/lzstring4javarhinotest/LZStringTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * LZString4Java By Rufus Huang 3 | * https://github.com/rufushuang/lz-string4java 4 | * MIT License 5 | * 6 | * Port from original JavaScript version by pieroxy 7 | * https://github.com/pieroxy/lz-string 8 | */ 9 | 10 | package rufus.lzstring4javarhinotest; 11 | 12 | import java.io.IOException; 13 | 14 | import rufus.lzstring4java.LZString; 15 | 16 | public class LZStringTest { 17 | 18 | public static void main(String[] args) throws IOException { 19 | String input; 20 | input = "hello1hello2hello3hello4hello5hello6hello7hello8hello9helloAhelloBhelloChelloDhelloEhelloF"; 21 | // StringBuffer sb = LZStringInRhino.getStringFromJSFile(new File("C:\\large.json"), "GBK"); 22 | // input = sb.toString(); 23 | String compressed; 24 | long now = System.currentTimeMillis(); 25 | compressed = LZStringInRhino.compressToBase64(input); 26 | System.out.println("\nLZStringInRhino(133).compressToBase64 time(ms): " + (System.currentTimeMillis() - now) + "\n" + compressed); 27 | // LZStringInRhino.writeStringToFile(new File("C:\\compressed.json"), compressed, "UTF-8"); 28 | now = System.currentTimeMillis(); 29 | compressed = LZStringInRhino144.compressToBase64(input); 30 | System.out.println("\nLZStringInRhino144.compressToBase64 time(ms): " + (System.currentTimeMillis() - now) + "\n" + compressed); 31 | // LZStringInRhino.writeStringToFile(new File("C:\\compressed.json"), compressed, "UTF-8"); 32 | now = System.currentTimeMillis(); 33 | compressed = LZString.compressToBase64(input); 34 | System.out.println("\nLZString.compressToBase64 time(ms): " + (System.currentTimeMillis() - now) + "\n" + compressed); 35 | // LZStringInRhino.writeStringToFile(new File("C:\\compressed2.json"), compressed, "UTF-8"); 36 | 37 | // compressToEncodedURIComponent test 38 | now = System.currentTimeMillis(); 39 | compressed = LZStringInRhino144.compressToEncodedURIComponent(input); 40 | System.out.println("\nLZStringInRhino144.compressToEncodedURIComponent time(ms): " + (System.currentTimeMillis() - now) + "\n" + compressed); 41 | 42 | now = System.currentTimeMillis(); 43 | compressed = LZString.compressToEncodedURIComponent(input); 44 | System.out.println("\nLZString.compressToEncodedURIComponent time(ms): " + (System.currentTimeMillis() - now) + "\n" + compressed); 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /refence/lz-string-1.3.3-min.js: -------------------------------------------------------------------------------- 1 | var LZString={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",_f:String.fromCharCode,compressToBase64:function(e){if(e==null)return"";var t="";var n,r,i,s,o,u,a;var f=0;e=LZString.compress(e);while(f>8;r=e.charCodeAt(f/2)&255;if(f/2+1>8;else i=NaN}else{n=e.charCodeAt((f-1)/2)&255;if((f+1)/2>8;i=e.charCodeAt((f+1)/2)&255}else r=i=NaN}f+=3;s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+LZString._keyStr.charAt(s)+LZString._keyStr.charAt(o)+LZString._keyStr.charAt(u)+LZString._keyStr.charAt(a)}return t},decompressFromBase64:function(e){if(e==null)return"";var t="",n=0,r,i,s,o,u,a,f,l,c=0,h=LZString._f;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(c>4;s=(a&15)<<4|f>>2;o=(f&3)<<6|l;if(n%2==0){r=i<<8;if(f!=64){t+=h(r|s)}if(l!=64){r=o<<8}}else{t=t+h(r|i);if(f!=64){r=s<<8}if(l!=64){t+=h(r|o)}}n+=3}return LZString.decompress(t)},compressToUTF16:function(e){if(e==null)return"";var t="",n,r,i,s=0,o=LZString._f;e=LZString.compress(e);for(n=0;n>1)+32);i=(r&1)<<14;break;case 1:t+=o(i+(r>>2)+32);i=(r&3)<<13;break;case 2:t+=o(i+(r>>3)+32);i=(r&7)<<12;break;case 3:t+=o(i+(r>>4)+32);i=(r&15)<<11;break;case 4:t+=o(i+(r>>5)+32);i=(r&31)<<10;break;case 5:t+=o(i+(r>>6)+32);i=(r&63)<<9;break;case 6:t+=o(i+(r>>7)+32);i=(r&127)<<8;break;case 7:t+=o(i+(r>>8)+32);i=(r&255)<<7;break;case 8:t+=o(i+(r>>9)+32);i=(r&511)<<6;break;case 9:t+=o(i+(r>>10)+32);i=(r&1023)<<5;break;case 10:t+=o(i+(r>>11)+32);i=(r&2047)<<4;break;case 11:t+=o(i+(r>>12)+32);i=(r&4095)<<3;break;case 12:t+=o(i+(r>>13)+32);i=(r&8191)<<2;break;case 13:t+=o(i+(r>>14)+32);i=(r&16383)<<1;break;case 14:t+=o(i+(r>>15)+32,(r&32767)+32);s=0;break}}return t+o(i+32)},decompressFromUTF16:function(e){if(e==null)return"";var t="",n,r,i=0,s=0,o=LZString._f;while(s>14);n=(r&16383)<<2;break;case 2:t+=o(n|r>>13);n=(r&8191)<<3;break;case 3:t+=o(n|r>>12);n=(r&4095)<<4;break;case 4:t+=o(n|r>>11);n=(r&2047)<<5;break;case 5:t+=o(n|r>>10);n=(r&1023)<<6;break;case 6:t+=o(n|r>>9);n=(r&511)<<7;break;case 7:t+=o(n|r>>8);n=(r&255)<<8;break;case 8:t+=o(n|r>>7);n=(r&127)<<9;break;case 9:t+=o(n|r>>6);n=(r&63)<<10;break;case 10:t+=o(n|r>>5);n=(r&31)<<11;break;case 11:t+=o(n|r>>4);n=(r&15)<<12;break;case 12:t+=o(n|r>>3);n=(r&7)<<13;break;case 13:t+=o(n|r>>2);n=(r&3)<<14;break;case 14:t+=o(n|r>>1);n=(r&1)<<15;break;case 15:t+=o(n|r);i=0;break}s++}return LZString.decompress(t)},compress:function(e){if(e==null)return"";var t,n,r={},i={},s="",o="",u="",a=2,f=3,l=2,c="",h=0,p=0,d,v=LZString._f;for(d=0;d>1}}else{n=1;for(t=0;t>1}}a--;if(a==0){a=Math.pow(2,l);l++}delete i[u]}else{n=r[u];for(t=0;t>1}}a--;if(a==0){a=Math.pow(2,l);l++}r[o]=f++;u=String(s)}}if(u!==""){if(Object.prototype.hasOwnProperty.call(i,u)){if(u.charCodeAt(0)<256){for(t=0;t>1}}else{n=1;for(t=0;t>1}}a--;if(a==0){a=Math.pow(2,l);l++}delete i[u]}else{n=r[u];for(t=0;t>1}}a--;if(a==0){a=Math.pow(2,l);l++}}n=2;for(t=0;t>1}while(true){h=h<<1;if(p==15){c+=v(h);break}else p++}return c},decompress:function(e){if(e==null)return"";if(e=="")return null;var t=[],n,r=4,i=4,s=3,o="",u="",a,f,l,c,h,p,d,v=LZString._f,m={string:e,val:e.charCodeAt(0),position:32768,index:1};for(a=0;a<3;a+=1){t[a]=a}l=0;h=Math.pow(2,2);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}switch(n=l){case 0:l=0;h=Math.pow(2,8);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}d=v(l);break;case 1:l=0;h=Math.pow(2,16);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}d=v(l);break;case 2:return""}t[3]=d;f=u=d;while(true){if(m.index>m.string.length){return""}l=0;h=Math.pow(2,s);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}switch(d=l){case 0:l=0;h=Math.pow(2,8);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}t[i++]=v(l);d=i-1;r--;break;case 1:l=0;h=Math.pow(2,16);p=1;while(p!=h){c=m.val&m.position;m.position>>=1;if(m.position==0){m.position=32768;m.val=m.string.charCodeAt(m.index++)}l|=(c>0?1:0)*p;p<<=1}t[i++]=v(l);d=i-1;r--;break;case 2:return u}if(r==0){r=Math.pow(2,s);s++}if(t[d]){o=t[d]}else{if(d===i){o=f+f.charAt(0)}else{return null}}u+=o;t[i++]=f+o.charAt(0);r--;f=o;if(r==0){r=Math.pow(2,s);s++}}}};if(typeof module!=="undefined"&&module!=null){module.exports=LZString} 2 | -------------------------------------------------------------------------------- /src/rufus/lzstring4javarhinotest/LZStringInRhino.java: -------------------------------------------------------------------------------- 1 | /* 2 | * LZString4Java By Rufus Huang 3 | * https://github.com/rufushuang/lz-string4java 4 | * MIT License 5 | * 6 | * Port from original JavaScript version by pieroxy 7 | * https://github.com/pieroxy/lz-string 8 | */ 9 | 10 | package rufus.lzstring4javarhinotest; 11 | 12 | import java.io.BufferedOutputStream; 13 | import java.io.BufferedReader; 14 | import java.io.File; 15 | import java.io.FileInputStream; 16 | import java.io.FileOutputStream; 17 | import java.io.FileReader; 18 | import java.io.IOException; 19 | import java.io.InputStreamReader; 20 | 21 | import org.mozilla.javascript.Context; 22 | import org.mozilla.javascript.Function; 23 | import org.mozilla.javascript.NativeObject; 24 | import org.mozilla.javascript.Script; 25 | import org.mozilla.javascript.Scriptable; 26 | 27 | 28 | public class LZStringInRhino { 29 | 30 | protected static StringBuffer getStringFromJSFile(File file) throws IOException { 31 | // file.length() is in bytes count, char counts can be smaller. 32 | // anyway, ensure the capacity. bigger is ok. 33 | StringBuffer sb = new StringBuffer((int) file.length()); 34 | FileReader fr = null; 35 | try { 36 | fr = new FileReader(file); 37 | char[] buff = new char[65536]; 38 | int len; 39 | while ((len = fr.read(buff)) > 0) { 40 | sb.append(buff, 0, len); 41 | } 42 | } catch (IOException e) { 43 | e.printStackTrace(); 44 | throw e; 45 | } finally { 46 | try { 47 | if (fr != null) 48 | fr.close(); 49 | } catch (IOException e) { 50 | } 51 | } 52 | return sb; 53 | } 54 | 55 | public static StringBuffer getStringFromJSFile(File file, String encoding) throws IOException { 56 | // file.length() is in bytes count, char counts can be smaller. 57 | // anyway, ensure the capacity. bigger is ok. 58 | StringBuffer sb = new StringBuffer((int) file.length()); 59 | BufferedReader fr = null; 60 | try { 61 | fr = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); 62 | char[] buff = new char[65536]; 63 | int len; 64 | while ((len = fr.read(buff)) > 0) { 65 | sb.append(buff, 0, len); 66 | } 67 | } catch (IOException e) { 68 | e.printStackTrace(); 69 | throw e; 70 | } finally { 71 | try { 72 | if (fr != null) 73 | fr.close(); 74 | } catch (IOException e) { 75 | } 76 | } 77 | return sb; 78 | } 79 | 80 | public static void writeStringToFile(File file, String str, String strencoding) throws IOException { 81 | BufferedOutputStream bos = null; 82 | try { 83 | bos = new BufferedOutputStream(new FileOutputStream(file)); 84 | bos.write(str.getBytes(strencoding)); 85 | } catch (IOException e) { 86 | e.printStackTrace(); 87 | throw e; 88 | } finally { 89 | try { 90 | if (bos != null) 91 | bos.close(); 92 | } catch (IOException e) { 93 | } 94 | } 95 | } 96 | 97 | protected static String LZ_STRING_JS_FILE_PATH = System.getProperty("user.dir") + File.separatorChar + "refence" + File.separatorChar + "lz-string-1.3.3-min.js"; 98 | protected static String LZ_STRING_JS; 99 | static { 100 | try { 101 | LZ_STRING_JS = getStringFromJSFile(new File(LZ_STRING_JS_FILE_PATH)).toString(); 102 | } catch (IOException e) { 103 | throw new RuntimeException(e); 104 | } 105 | } 106 | protected static Script COMPILED_LZ; 107 | protected static Context context; 108 | protected static Scriptable scope; 109 | protected static NativeObject OBJ_LZString; 110 | protected static Function FUNC_compressToBase64; 111 | protected static Function FUNC_decompressFromBase64; 112 | protected static Function FUNC_compressToUTF16; 113 | protected static Function FUNC_decompressFromUTF16; 114 | protected static Function FUNC_compress; 115 | protected static Function FUNC_decompress; 116 | static { 117 | // should change above JDK6, or just use Nashorn instead of Rhino? 118 | context = Context.enter(); 119 | scope = context.initStandardObjects(); 120 | context.evaluateString(scope, LZ_STRING_JS, "script", 1, null); 121 | OBJ_LZString = (NativeObject) scope.get("LZString", scope); 122 | FUNC_compressToBase64 = (Function) OBJ_LZString.get("compressToBase64", scope); 123 | FUNC_decompressFromBase64 = (Function) OBJ_LZString.get("decompressFromBase64", scope); 124 | FUNC_compressToUTF16 = (Function) OBJ_LZString.get("compressToUTF16", scope); 125 | FUNC_decompressFromUTF16 = (Function) OBJ_LZString.get("decompressFromUTF16", scope); 126 | FUNC_compress = (Function) OBJ_LZString.get("compress", scope); 127 | FUNC_decompress = (Function) OBJ_LZString.get("decompress", scope); 128 | } 129 | 130 | public static String compressToBase64(String input) { 131 | return (String) FUNC_compressToBase64.call(context, scope, scope, new Object[] { input }); 132 | } 133 | 134 | public static String decompressFromBase64(String input) { 135 | return (String) FUNC_decompressFromBase64.call(context, scope, scope, new Object[] { input }); 136 | } 137 | 138 | public static String compressToUTF16(String input) { 139 | return (String) FUNC_compressToUTF16.call(context, scope, scope, new Object[] { input }); 140 | } 141 | 142 | public static String decompressFromUTF16(String input) { 143 | return (String) FUNC_decompressFromUTF16.call(context, scope, scope, new Object[] { input }); 144 | } 145 | 146 | public static String compress(String input) { 147 | return (String) FUNC_compress.call(context, scope, scope, new Object[] { input }); 148 | } 149 | 150 | public static String decompress(String input) { 151 | return (String) FUNC_decompress.call(context, scope, scope, new Object[] { input }); 152 | } 153 | 154 | public static void main(String[] args) throws IOException { 155 | String input; 156 | // input = "hello"; 157 | input = "hello1hello2hello3hello4hello5hello6hello7hello8hello9helloAhelloBhelloChelloDhelloEhelloF"; 158 | 159 | System.out.println(decompress(compress(input))); 160 | System.out.println(decompressFromBase64(compressToBase64(input))); 161 | System.out.println(decompressFromUTF16(compressToUTF16(input))); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /refence/lz-string-1.0.2.js: -------------------------------------------------------------------------------- 1 | // Copyright © 2013 Pieroxy 2 | // This work is free. You can redistribute it and/or modify it 3 | // under the terms of the WTFPL, Version 2 4 | // For more information see LICENSE.txt or http://www.wtfpl.net/ 5 | // 6 | // LZ-based compression algorithm, version 1.0.2-rc1 7 | var LZString = { 8 | 9 | writeBit : function(value, data) { 10 | data.val = (data.val << 1) | value; 11 | if (data.position == 15) { 12 | data.position = 0; 13 | data.string += String.fromCharCode(data.val); 14 | data.val = 0; 15 | } else { 16 | data.position++; 17 | } 18 | }, 19 | 20 | writeBits : function(numBits, value, data) { 21 | if (typeof(value)=="string") 22 | value = value.charCodeAt(0); 23 | for (var i=0 ; i> 1; 26 | } 27 | }, 28 | 29 | produceW : function (context) { 30 | if (Object.prototype.hasOwnProperty.call(context.dictionaryToCreate,context.w)) { 31 | if (context.w.charCodeAt(0)<256) { 32 | LZString.writeBits(context.numBits, 0, context.data); 33 | LZString.writeBits(8, context.w, context.data); 34 | } else { 35 | LZString.writeBits(context.numBits, 1, context.data); 36 | LZString.writeBits(16, context.w, context.data); 37 | } 38 | LZString.decrementEnlargeIn(context); 39 | delete context.dictionaryToCreate[context.w]; 40 | } else { 41 | LZString.writeBits(context.numBits, context.dictionary[context.w], context.data); 42 | } 43 | LZString.decrementEnlargeIn(context); 44 | }, 45 | 46 | decrementEnlargeIn : function(context) { 47 | context.enlargeIn--; 48 | if (context.enlargeIn == 0) { 49 | context.enlargeIn = Math.pow(2, context.numBits); 50 | context.numBits++; 51 | } 52 | }, 53 | 54 | compress: function (uncompressed) { 55 | var context = { 56 | dictionary: {}, 57 | dictionaryToCreate: {}, 58 | c:"", 59 | wc:"", 60 | w:"", 61 | enlargeIn: 2, // Compensate for the first entry which should not count 62 | dictSize: 3, 63 | numBits: 2, 64 | result: "", 65 | data: {string:"", val:0, position:0} 66 | }, i; 67 | 68 | for (i = 0; i < uncompressed.length; i += 1) { 69 | context.c = uncompressed.charAt(i); 70 | if (!Object.prototype.hasOwnProperty.call(context.dictionary,context.c)) { 71 | context.dictionary[context.c] = context.dictSize++; 72 | context.dictionaryToCreate[context.c] = true; 73 | } 74 | 75 | context.wc = context.w + context.c; 76 | if (Object.prototype.hasOwnProperty.call(context.dictionary,context.wc)) { 77 | context.w = context.wc; 78 | } else { 79 | LZString.produceW(context); 80 | // Add wc to the dictionary. 81 | context.dictionary[context.wc] = context.dictSize++; 82 | context.w = String(context.c); 83 | } 84 | } 85 | 86 | // Output the code for w. 87 | if (context.w !== "") { 88 | LZString.produceW(context); 89 | } 90 | 91 | // Mark the end of the stream 92 | LZString.writeBits(context.numBits, 2, context.data); 93 | 94 | // Flush the last char 95 | while (context.data.val>0) LZString.writeBit(0,context.data) 96 | return context.data.string; 97 | }, 98 | 99 | readBit : function(data) { 100 | var res = data.val & data.position; 101 | data.position >>= 1; 102 | if (data.position == 0) { 103 | data.position = 32768; 104 | data.val = data.string.charCodeAt(data.index++); 105 | } 106 | //data.val = (data.val << 1); 107 | return res>0 ? 1 : 0; 108 | }, 109 | 110 | readBits : function(numBits, data) { 111 | var res = 0; 112 | var maxpower = Math.pow(2,numBits); 113 | var power=1; 114 | while (power!=maxpower) { 115 | res |= LZString.readBit(data) * power; 116 | power <<= 1; 117 | } 118 | return res; 119 | }, 120 | 121 | decompress: function (compressed) { 122 | var dictionary = {}, 123 | next, 124 | enlargeIn = 4, 125 | dictSize = 4, 126 | numBits = 3, 127 | entry = "", 128 | result = "", 129 | i, 130 | w, 131 | c, 132 | errorCount=0, 133 | literal, 134 | data = {string:compressed, val:compressed.charCodeAt(0), position:32768, index:1}; 135 | 136 | for (i = 0; i < 3; i += 1) { 137 | dictionary[i] = i; 138 | } 139 | 140 | next = LZString.readBits(2, data); 141 | switch (next) { 142 | case 0: 143 | c = String.fromCharCode(LZString.readBits(8, data)); 144 | break; 145 | case 1: 146 | c = String.fromCharCode(LZString.readBits(16, data)); 147 | break; 148 | case 2: 149 | return ""; 150 | } 151 | dictionary[3] = c; 152 | w = result = c; 153 | while (true) { 154 | c = LZString.readBits(numBits, data); 155 | 156 | switch (c) { 157 | case 0: 158 | if (errorCount++ > 10000) return "Error"; 159 | c = String.fromCharCode(LZString.readBits(8, data)); 160 | dictionary[dictSize++] = c; 161 | c = dictSize-1; 162 | enlargeIn--; 163 | break; 164 | case 1: 165 | c = String.fromCharCode(LZString.readBits(16, data)); 166 | dictionary[dictSize++] = c; 167 | c = dictSize-1; 168 | enlargeIn--; 169 | break; 170 | case 2: 171 | return result; 172 | } 173 | 174 | if (enlargeIn == 0) { 175 | enlargeIn = Math.pow(2, numBits); 176 | numBits++; 177 | } 178 | 179 | if (dictionary[c]) { 180 | entry = dictionary[c]; 181 | } else { 182 | if (c === dictSize) { 183 | entry = w + w.charAt(0); 184 | } else { 185 | return null; 186 | } 187 | } 188 | result += entry; 189 | 190 | // Add w+entry[0] to the dictionary. 191 | dictionary[dictSize++] = w + entry.charAt(0); 192 | enlargeIn--; 193 | 194 | w = entry; 195 | 196 | if (enlargeIn == 0) { 197 | enlargeIn = Math.pow(2, numBits); 198 | numBits++; 199 | } 200 | 201 | } 202 | return result; 203 | } 204 | }; 205 | -------------------------------------------------------------------------------- /src/rufus/lzstring4javarhinotest/LZStringInRhino144.java: -------------------------------------------------------------------------------- 1 | /* 2 | * LZString4Java By Rufus Huang 3 | * https://github.com/rufushuang/lz-string4java 4 | * MIT License 5 | * 6 | * Port from original JavaScript version by pieroxy 7 | * https://github.com/pieroxy/lz-string 8 | */ 9 | 10 | package rufus.lzstring4javarhinotest; 11 | 12 | import java.io.BufferedOutputStream; 13 | import java.io.BufferedReader; 14 | import java.io.File; 15 | import java.io.FileInputStream; 16 | import java.io.FileOutputStream; 17 | import java.io.FileReader; 18 | import java.io.IOException; 19 | import java.io.InputStreamReader; 20 | 21 | import org.mozilla.javascript.Context; 22 | import org.mozilla.javascript.Function; 23 | import org.mozilla.javascript.NativeObject; 24 | import org.mozilla.javascript.Script; 25 | import org.mozilla.javascript.Scriptable; 26 | 27 | 28 | public class LZStringInRhino144 { 29 | 30 | protected static StringBuffer getStringFromJSFile(File file) throws IOException { 31 | // file.length() is in bytes count, char counts can be smaller. 32 | // anyway, ensure the capacity. bigger is ok. 33 | StringBuffer sb = new StringBuffer((int) file.length()); 34 | FileReader fr = null; 35 | try { 36 | fr = new FileReader(file); 37 | char[] buff = new char[65536]; 38 | int len; 39 | while ((len = fr.read(buff)) > 0) { 40 | sb.append(buff, 0, len); 41 | } 42 | } catch (IOException e) { 43 | e.printStackTrace(); 44 | throw e; 45 | } finally { 46 | try { 47 | if (fr != null) 48 | fr.close(); 49 | } catch (IOException e) { 50 | } 51 | } 52 | return sb; 53 | } 54 | 55 | public static StringBuffer getStringFromJSFile(File file, String encoding) throws IOException { 56 | // file.length() is in bytes count, char counts can be smaller. 57 | // anyway, ensure the capacity. bigger is ok. 58 | StringBuffer sb = new StringBuffer((int) file.length()); 59 | BufferedReader fr = null; 60 | try { 61 | fr = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); 62 | char[] buff = new char[65536]; 63 | int len; 64 | while ((len = fr.read(buff)) > 0) { 65 | sb.append(buff, 0, len); 66 | } 67 | } catch (IOException e) { 68 | e.printStackTrace(); 69 | throw e; 70 | } finally { 71 | try { 72 | if (fr != null) 73 | fr.close(); 74 | } catch (IOException e) { 75 | } 76 | } 77 | return sb; 78 | } 79 | 80 | public static void writeStringToFile(File file, String str, String strencoding) throws IOException { 81 | BufferedOutputStream bos = null; 82 | try { 83 | bos = new BufferedOutputStream(new FileOutputStream(file)); 84 | bos.write(str.getBytes(strencoding)); 85 | } catch (IOException e) { 86 | e.printStackTrace(); 87 | throw e; 88 | } finally { 89 | try { 90 | if (bos != null) 91 | bos.close(); 92 | } catch (IOException e) { 93 | } 94 | } 95 | } 96 | 97 | protected static String LZ_STRING_JS_FILE_PATH = System.getProperty("user.dir") + File.separatorChar + "refence" + File.separatorChar + "lz-string-1.4.4.js"; 98 | protected static String LZ_STRING_JS; 99 | static { 100 | try { 101 | LZ_STRING_JS = getStringFromJSFile(new File(LZ_STRING_JS_FILE_PATH)).toString(); 102 | } catch (IOException e) { 103 | throw new RuntimeException(e); 104 | } 105 | } 106 | protected static Script COMPILED_LZ; 107 | protected static Context context; 108 | protected static Scriptable scope; 109 | protected static NativeObject OBJ_LZString; 110 | protected static Function FUNC_compressToEncodedURIComponent; 111 | protected static Function FUNC_decompressFromEncodedURIComponent; 112 | protected static Function FUNC_compressToBase64; 113 | protected static Function FUNC_decompressFromBase64; 114 | protected static Function FUNC_compressToUTF16; 115 | protected static Function FUNC_decompressFromUTF16; 116 | protected static Function FUNC_compress; 117 | protected static Function FUNC_decompress; 118 | static { 119 | // should change above JDK6, or just use Nashorn instead of Rhino? 120 | context = Context.enter(); 121 | scope = context.initStandardObjects(); 122 | context.evaluateString(scope, LZ_STRING_JS, "script", 1, null); 123 | OBJ_LZString = (NativeObject) scope.get("LZString", scope); 124 | FUNC_compressToEncodedURIComponent = (Function) OBJ_LZString.get("compressToEncodedURIComponent", scope); 125 | FUNC_decompressFromEncodedURIComponent = (Function) OBJ_LZString.get("decompressFromEncodedURIComponent", scope); 126 | FUNC_compressToBase64 = (Function) OBJ_LZString.get("compressToBase64", scope); 127 | FUNC_decompressFromBase64 = (Function) OBJ_LZString.get("decompressFromBase64", scope); 128 | FUNC_compressToUTF16 = (Function) OBJ_LZString.get("compressToUTF16", scope); 129 | FUNC_decompressFromUTF16 = (Function) OBJ_LZString.get("decompressFromUTF16", scope); 130 | FUNC_compress = (Function) OBJ_LZString.get("compress", scope); 131 | FUNC_decompress = (Function) OBJ_LZString.get("decompress", scope); 132 | } 133 | 134 | public static String compressToEncodedURIComponent(String input) { 135 | return (String) FUNC_compressToEncodedURIComponent.call(context, scope, scope, new Object[] { input }); 136 | } 137 | 138 | public static String decompressFromEncodedURIComponent(String input) { 139 | return (String) FUNC_decompressFromEncodedURIComponent.call(context, scope, scope, new Object[] { input }); 140 | } 141 | 142 | public static String compressToBase64(String input) { 143 | return (String) FUNC_compressToBase64.call(context, scope, scope, new Object[] { input }); 144 | } 145 | 146 | public static String decompressFromBase64(String input) { 147 | return (String) FUNC_decompressFromBase64.call(context, scope, scope, new Object[] { input }); 148 | } 149 | 150 | public static String compressToUTF16(String input) { 151 | return (String) FUNC_compressToUTF16.call(context, scope, scope, new Object[] { input }); 152 | } 153 | 154 | public static String decompressFromUTF16(String input) { 155 | return (String) FUNC_decompressFromUTF16.call(context, scope, scope, new Object[] { input }); 156 | } 157 | 158 | public static String compress(String input) { 159 | return (String) FUNC_compress.call(context, scope, scope, new Object[] { input }); 160 | } 161 | 162 | public static String decompress(String input) { 163 | return (String) FUNC_decompress.call(context, scope, scope, new Object[] { input }); 164 | } 165 | 166 | public static void main(String[] args) throws IOException { 167 | String input; 168 | // input = "hello"; 169 | input = "hello1hello2hello3hello4hello5hello6hello7hello8hello9helloAhelloBhelloChelloDhelloEhelloF"; 170 | 171 | System.out.println(decompress(compress(input))); 172 | System.out.println(decompressFromBase64(compressToBase64(input))); 173 | System.out.println(decompressFromUTF16(compressToUTF16(input))); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /refence/lz-string-1.4.4.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Pieroxy 2 | // This work is free. You can redistribute it and/or modify it 3 | // under the terms of the WTFPL, Version 2 4 | // For more information see LICENSE.txt or http://www.wtfpl.net/ 5 | // 6 | // For more information, the home page: 7 | // http://pieroxy.net/blog/pages/lz-string/testing.html 8 | // 9 | // LZ-based compression algorithm, version 1.4.4 10 | var LZString = (function() { 11 | 12 | // private property 13 | var f = String.fromCharCode; 14 | var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 15 | var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"; 16 | var baseReverseDic = {}; 17 | 18 | function getBaseValue(alphabet, character) { 19 | if (!baseReverseDic[alphabet]) { 20 | baseReverseDic[alphabet] = {}; 21 | for (var i=0 ; i>> 8; 66 | buf[i*2+1] = current_value % 256; 67 | } 68 | return buf; 69 | }, 70 | 71 | //decompress from uint8array (UCS-2 big endian format) 72 | decompressFromUint8Array:function (compressed) { 73 | if (compressed===null || compressed===undefined){ 74 | return LZString.decompress(compressed); 75 | } else { 76 | var buf=new Array(compressed.length/2); // 2 bytes per character 77 | for (var i=0, TotalLen=buf.length; i> 1; 159 | } 160 | } else { 161 | value = 1; 162 | for (i=0 ; i> 1; 184 | } 185 | } 186 | context_enlargeIn--; 187 | if (context_enlargeIn == 0) { 188 | context_enlargeIn = Math.pow(2, context_numBits); 189 | context_numBits++; 190 | } 191 | delete context_dictionaryToCreate[context_w]; 192 | } else { 193 | value = context_dictionary[context_w]; 194 | for (i=0 ; i> 1; 204 | } 205 | 206 | 207 | } 208 | context_enlargeIn--; 209 | if (context_enlargeIn == 0) { 210 | context_enlargeIn = Math.pow(2, context_numBits); 211 | context_numBits++; 212 | } 213 | // Add wc to the dictionary. 214 | context_dictionary[context_wc] = context_dictSize++; 215 | context_w = String(context_c); 216 | } 217 | } 218 | 219 | // Output the code for w. 220 | if (context_w !== "") { 221 | if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) { 222 | if (context_w.charCodeAt(0)<256) { 223 | for (i=0 ; i> 1; 244 | } 245 | } else { 246 | value = 1; 247 | for (i=0 ; i> 1; 269 | } 270 | } 271 | context_enlargeIn--; 272 | if (context_enlargeIn == 0) { 273 | context_enlargeIn = Math.pow(2, context_numBits); 274 | context_numBits++; 275 | } 276 | delete context_dictionaryToCreate[context_w]; 277 | } else { 278 | value = context_dictionary[context_w]; 279 | for (i=0 ; i> 1; 289 | } 290 | 291 | 292 | } 293 | context_enlargeIn--; 294 | if (context_enlargeIn == 0) { 295 | context_enlargeIn = Math.pow(2, context_numBits); 296 | context_numBits++; 297 | } 298 | } 299 | 300 | // Mark the end of the stream 301 | value = 2; 302 | for (i=0 ; i> 1; 312 | } 313 | 314 | // Flush the last char 315 | while (true) { 316 | context_data_val = (context_data_val << 1); 317 | if (context_data_position == bitsPerChar-1) { 318 | context_data.push(getCharFromInt(context_data_val)); 319 | break; 320 | } 321 | else context_data_position++; 322 | } 323 | return context_data.join(''); 324 | }, 325 | 326 | decompress: function (compressed) { 327 | if (compressed == null) return ""; 328 | if (compressed == "") return null; 329 | return LZString._decompress(compressed.length, 32768, function(index) { return compressed.charCodeAt(index); }); 330 | }, 331 | 332 | _decompress: function (length, resetValue, getNextValue) { 333 | var dictionary = [], 334 | next, 335 | enlargeIn = 4, 336 | dictSize = 4, 337 | numBits = 3, 338 | entry = "", 339 | result = [], 340 | i, 341 | w, 342 | bits, resb, maxpower, power, 343 | c, 344 | data = {val:getNextValue(0), position:resetValue, index:1}; 345 | 346 | for (i = 0; i < 3; i += 1) { 347 | dictionary[i] = i; 348 | } 349 | 350 | bits = 0; 351 | maxpower = Math.pow(2,2); 352 | power=1; 353 | while (power!=maxpower) { 354 | resb = data.val & data.position; 355 | data.position >>= 1; 356 | if (data.position == 0) { 357 | data.position = resetValue; 358 | data.val = getNextValue(data.index++); 359 | } 360 | bits |= (resb>0 ? 1 : 0) * power; 361 | power <<= 1; 362 | } 363 | 364 | switch (next = bits) { 365 | case 0: 366 | bits = 0; 367 | maxpower = Math.pow(2,8); 368 | power=1; 369 | while (power!=maxpower) { 370 | resb = data.val & data.position; 371 | data.position >>= 1; 372 | if (data.position == 0) { 373 | data.position = resetValue; 374 | data.val = getNextValue(data.index++); 375 | } 376 | bits |= (resb>0 ? 1 : 0) * power; 377 | power <<= 1; 378 | } 379 | c = f(bits); 380 | break; 381 | case 1: 382 | bits = 0; 383 | maxpower = Math.pow(2,16); 384 | power=1; 385 | while (power!=maxpower) { 386 | resb = data.val & data.position; 387 | data.position >>= 1; 388 | if (data.position == 0) { 389 | data.position = resetValue; 390 | data.val = getNextValue(data.index++); 391 | } 392 | bits |= (resb>0 ? 1 : 0) * power; 393 | power <<= 1; 394 | } 395 | c = f(bits); 396 | break; 397 | case 2: 398 | return ""; 399 | } 400 | dictionary[3] = c; 401 | w = c; 402 | result.push(c); 403 | while (true) { 404 | if (data.index > length) { 405 | return ""; 406 | } 407 | 408 | bits = 0; 409 | maxpower = Math.pow(2,numBits); 410 | power=1; 411 | while (power!=maxpower) { 412 | resb = data.val & data.position; 413 | data.position >>= 1; 414 | if (data.position == 0) { 415 | data.position = resetValue; 416 | data.val = getNextValue(data.index++); 417 | } 418 | bits |= (resb>0 ? 1 : 0) * power; 419 | power <<= 1; 420 | } 421 | 422 | switch (c = bits) { 423 | case 0: 424 | bits = 0; 425 | maxpower = Math.pow(2,8); 426 | power=1; 427 | while (power!=maxpower) { 428 | resb = data.val & data.position; 429 | data.position >>= 1; 430 | if (data.position == 0) { 431 | data.position = resetValue; 432 | data.val = getNextValue(data.index++); 433 | } 434 | bits |= (resb>0 ? 1 : 0) * power; 435 | power <<= 1; 436 | } 437 | 438 | dictionary[dictSize++] = f(bits); 439 | c = dictSize-1; 440 | enlargeIn--; 441 | break; 442 | case 1: 443 | bits = 0; 444 | maxpower = Math.pow(2,16); 445 | power=1; 446 | while (power!=maxpower) { 447 | resb = data.val & data.position; 448 | data.position >>= 1; 449 | if (data.position == 0) { 450 | data.position = resetValue; 451 | data.val = getNextValue(data.index++); 452 | } 453 | bits |= (resb>0 ? 1 : 0) * power; 454 | power <<= 1; 455 | } 456 | dictionary[dictSize++] = f(bits); 457 | c = dictSize-1; 458 | enlargeIn--; 459 | break; 460 | case 2: 461 | return result.join(''); 462 | } 463 | 464 | if (enlargeIn == 0) { 465 | enlargeIn = Math.pow(2, numBits); 466 | numBits++; 467 | } 468 | 469 | if (dictionary[c]) { 470 | entry = dictionary[c]; 471 | } else { 472 | if (c === dictSize) { 473 | entry = w + w.charAt(0); 474 | } else { 475 | return null; 476 | } 477 | } 478 | result.push(entry); 479 | 480 | // Add w+entry[0] to the dictionary. 481 | dictionary[dictSize++] = w + entry.charAt(0); 482 | enlargeIn--; 483 | 484 | w = entry; 485 | 486 | if (enlargeIn == 0) { 487 | enlargeIn = Math.pow(2, numBits); 488 | numBits++; 489 | } 490 | 491 | } 492 | } 493 | }; 494 | return LZString; 495 | })(); 496 | 497 | if (typeof define === 'function' && define.amd) { 498 | define(function () { return LZString; }); 499 | } else if( typeof module !== 'undefined' && module != null ) { 500 | module.exports = LZString 501 | } 502 | -------------------------------------------------------------------------------- /src/rufus/lzstring4java/LZString.java: -------------------------------------------------------------------------------- 1 | /* 2 | * LZString4Java By Rufus Huang 3 | * https://github.com/rufushuang/lz-string4java 4 | * MIT License 5 | * 6 | * Port from original JavaScript version by pieroxy 7 | * https://github.com/pieroxy/lz-string 8 | */ 9 | 10 | package rufus.lzstring4java; 11 | 12 | import java.util.*; 13 | 14 | public class LZString { 15 | 16 | private static char[] keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray(); 17 | private static char[] keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$".toCharArray(); 18 | private static Map> baseReverseDic = new HashMap>(); 19 | 20 | private static char getBaseValue(char[] alphabet, Character character) { 21 | Map map = baseReverseDic.get(alphabet); 22 | if (map == null) { 23 | map = new HashMap(); 24 | baseReverseDic.put(alphabet, map); 25 | for (int i = 0; i < alphabet.length; i++) { 26 | map.put(alphabet[i], i); 27 | } 28 | } 29 | return (char) map.get(character).intValue(); 30 | } 31 | 32 | public static String compressToBase64(String input) { 33 | if (input == null) 34 | return ""; 35 | String res = LZString._compress(input, 6, new CompressFunctionWrapper() { 36 | @Override 37 | public char doFunc(int a) { 38 | return keyStrBase64[a]; 39 | } 40 | }); 41 | switch (res.length() % 4) { // To produce valid Base64 42 | default: // When could this happen ? 43 | case 0: 44 | return res; 45 | case 1: 46 | return res + "==="; 47 | case 2: 48 | return res + "=="; 49 | case 3: 50 | return res + "="; 51 | } 52 | } 53 | 54 | public static String decompressFromBase64(final String inputStr) { 55 | if (inputStr == null) 56 | return ""; 57 | if (inputStr.equals("")) 58 | return null; 59 | return LZString._decompress(inputStr.length(), 32, new DecompressFunctionWrapper() { 60 | @Override 61 | public char doFunc(int index) { 62 | return getBaseValue(keyStrBase64, inputStr.charAt(index)); 63 | } 64 | }); 65 | } 66 | 67 | public static String compressToUTF16(String input) { 68 | if (input == null) 69 | return ""; 70 | return LZString._compress(input, 15, new CompressFunctionWrapper() { 71 | @Override 72 | public char doFunc(int a) { 73 | return fc(a + 32); 74 | } 75 | }) + " "; 76 | } 77 | 78 | public static String decompressFromUTF16(final String compressedStr) { 79 | if (compressedStr == null) 80 | return ""; 81 | if (compressedStr.isEmpty()) 82 | return null; 83 | return LZString._decompress(compressedStr.length(), 16384, new DecompressFunctionWrapper() { 84 | @Override 85 | public char doFunc(int index) { 86 | return (char) (compressedStr.charAt(index) - 32); 87 | } 88 | }); 89 | } 90 | 91 | //TODO: java has no Uint8Array type, what can we do? 92 | 93 | public static String compressToEncodedURIComponent(String input) { 94 | if (input == null) 95 | return ""; 96 | return LZString._compress(input, 6, new CompressFunctionWrapper() { 97 | @Override 98 | public char doFunc(int a) { 99 | return keyStrUriSafe[a]; 100 | } 101 | }); 102 | } 103 | 104 | public static String decompressFromEncodedURIComponent(String inputStr) { 105 | if (inputStr == null) return ""; 106 | if (inputStr.isEmpty()) return null; 107 | final String urlEncodedInputStr = inputStr.replace(' ', '+'); 108 | return LZString._decompress(urlEncodedInputStr.length(), 32, new DecompressFunctionWrapper() { 109 | @Override 110 | public char doFunc(int index) { 111 | return getBaseValue(keyStrUriSafe, urlEncodedInputStr.charAt(index)); 112 | } 113 | }); 114 | } 115 | 116 | private static abstract class CompressFunctionWrapper { 117 | public abstract char doFunc(int i); 118 | } 119 | 120 | public static String compress(String uncompressed) { 121 | return LZString._compress(uncompressed, 16, new CompressFunctionWrapper() { 122 | @Override 123 | public char doFunc(int a) { 124 | return fc(a); 125 | } 126 | }); 127 | } 128 | private static String _compress(String uncompressedStr, int bitsPerChar, CompressFunctionWrapper getCharFromInt) { 129 | if (uncompressedStr == null) return ""; 130 | int i, value; 131 | Map context_dictionary = new HashMap(); 132 | Set context_dictionaryToCreate = new HashSet(); 133 | String context_c = ""; 134 | String context_wc = ""; 135 | String context_w = ""; 136 | int context_enlargeIn = 2; // Compensate for the first entry which should not count 137 | int context_dictSize = 3; 138 | int context_numBits = 2; 139 | StringBuilder context_data = new StringBuilder(uncompressedStr.length() / 3); 140 | int context_data_val = 0; 141 | int context_data_position = 0; 142 | int ii; 143 | 144 | for (ii = 0; ii < uncompressedStr.length(); ii += 1) { 145 | context_c = String.valueOf(uncompressedStr.charAt(ii)); 146 | if (!context_dictionary.containsKey(context_c)) { 147 | context_dictionary.put(context_c, context_dictSize++); 148 | context_dictionaryToCreate.add(context_c); 149 | } 150 | 151 | context_wc = context_w + context_c; 152 | if (context_dictionary.containsKey(context_wc)) { 153 | context_w = context_wc; 154 | } else { 155 | if (context_dictionaryToCreate.contains(context_w)) { 156 | if (context_w.charAt(0) < 256) { 157 | for (i = 0; i < context_numBits; i++) { 158 | context_data_val = (context_data_val << 1); 159 | if (context_data_position == bitsPerChar - 1) { 160 | context_data_position = 0; 161 | context_data.append(getCharFromInt.doFunc(context_data_val)); 162 | context_data_val = 0; 163 | } else { 164 | context_data_position++; 165 | } 166 | } 167 | value = context_w.charAt(0); 168 | for (i = 0; i < 8; i++) { 169 | context_data_val = (context_data_val << 1) | (value & 1); 170 | if (context_data_position == bitsPerChar - 1) { 171 | context_data_position = 0; 172 | context_data.append(getCharFromInt.doFunc(context_data_val)); 173 | context_data_val = 0; 174 | } else { 175 | context_data_position++; 176 | } 177 | value = value >> 1; 178 | } 179 | } else { 180 | value = 1; 181 | for (i = 0; i < context_numBits; i++) { 182 | context_data_val = (context_data_val << 1) | value; 183 | if (context_data_position == bitsPerChar - 1) { 184 | context_data_position = 0; 185 | context_data.append(getCharFromInt.doFunc(context_data_val)); 186 | context_data_val = 0; 187 | } else { 188 | context_data_position++; 189 | } 190 | value = 0; 191 | } 192 | value = context_w.charAt(0); 193 | for (i = 0; i < 16; i++) { 194 | context_data_val = (context_data_val << 1) | (value & 1); 195 | if (context_data_position == bitsPerChar - 1) { 196 | context_data_position = 0; 197 | context_data.append(getCharFromInt.doFunc(context_data_val)); 198 | context_data_val = 0; 199 | } else { 200 | context_data_position++; 201 | } 202 | value = value >> 1; 203 | } 204 | } 205 | context_enlargeIn--; 206 | if (context_enlargeIn == 0) { 207 | context_enlargeIn = powerOf2(context_numBits); 208 | context_numBits++; 209 | } 210 | context_dictionaryToCreate.remove(context_w); 211 | } else { 212 | value = context_dictionary.get(context_w); 213 | for (i = 0; i < context_numBits; i++) { 214 | context_data_val = (context_data_val << 1) | (value & 1); 215 | if (context_data_position == bitsPerChar - 1) { 216 | context_data_position = 0; 217 | context_data.append(getCharFromInt.doFunc(context_data_val)); 218 | context_data_val = 0; 219 | } else { 220 | context_data_position++; 221 | } 222 | value = value >> 1; 223 | } 224 | 225 | } 226 | context_enlargeIn--; 227 | if (context_enlargeIn == 0) { 228 | context_enlargeIn = powerOf2(context_numBits); 229 | context_numBits++; 230 | } 231 | // Add wc to the dictionary. 232 | context_dictionary.put(context_wc, context_dictSize++); 233 | context_w = context_c; 234 | } 235 | } 236 | 237 | // Output the code for w. 238 | if (!context_w.isEmpty()) { 239 | if (context_dictionaryToCreate.contains(context_w)) { 240 | if (context_w.charAt(0) < 256) { 241 | for (i = 0; i < context_numBits; i++) { 242 | context_data_val = (context_data_val << 1); 243 | if (context_data_position == bitsPerChar - 1) { 244 | context_data_position = 0; 245 | context_data.append(getCharFromInt.doFunc(context_data_val)); 246 | context_data_val = 0; 247 | } else { 248 | context_data_position++; 249 | } 250 | } 251 | value = context_w.charAt(0); 252 | for (i = 0; i < 8; i++) { 253 | context_data_val = (context_data_val << 1) | (value & 1); 254 | if (context_data_position == bitsPerChar - 1) { 255 | context_data_position = 0; 256 | context_data.append(getCharFromInt.doFunc(context_data_val)); 257 | context_data_val = 0; 258 | } else { 259 | context_data_position++; 260 | } 261 | value = value >> 1; 262 | } 263 | } else { 264 | value = 1; 265 | for (i = 0; i < context_numBits; i++) { 266 | context_data_val = (context_data_val << 1) | value; 267 | if (context_data_position == bitsPerChar - 1) { 268 | context_data_position = 0; 269 | context_data.append(getCharFromInt.doFunc(context_data_val)); 270 | context_data_val = 0; 271 | } else { 272 | context_data_position++; 273 | } 274 | value = 0; 275 | } 276 | value = context_w.charAt(0); 277 | for (i = 0; i < 16; i++) { 278 | context_data_val = (context_data_val << 1) | (value & 1); 279 | if (context_data_position == bitsPerChar - 1) { 280 | context_data_position = 0; 281 | context_data.append(getCharFromInt.doFunc(context_data_val)); 282 | context_data_val = 0; 283 | } else { 284 | context_data_position++; 285 | } 286 | value = value >> 1; 287 | } 288 | } 289 | context_enlargeIn--; 290 | if (context_enlargeIn == 0) { 291 | context_enlargeIn = powerOf2(context_numBits); 292 | context_numBits++; 293 | } 294 | context_dictionaryToCreate.remove(context_w); 295 | } else { 296 | value = context_dictionary.get(context_w); 297 | for (i = 0; i < context_numBits; i++) { 298 | context_data_val = (context_data_val << 1) | (value & 1); 299 | if (context_data_position == bitsPerChar - 1) { 300 | context_data_position = 0; 301 | context_data.append(getCharFromInt.doFunc(context_data_val)); 302 | context_data_val = 0; 303 | } else { 304 | context_data_position++; 305 | } 306 | value = value >> 1; 307 | } 308 | 309 | } 310 | context_enlargeIn--; 311 | if (context_enlargeIn == 0) { 312 | context_enlargeIn = powerOf2(context_numBits); 313 | context_numBits++; 314 | } 315 | } 316 | 317 | // Mark the end of the stream 318 | value = 2; 319 | for (i = 0; i < context_numBits; i++) { 320 | context_data_val = (context_data_val << 1) | (value & 1); 321 | if (context_data_position == bitsPerChar - 1) { 322 | context_data_position = 0; 323 | context_data.append(getCharFromInt.doFunc(context_data_val)); 324 | context_data_val = 0; 325 | } else { 326 | context_data_position++; 327 | } 328 | value = value >> 1; 329 | } 330 | 331 | // Flush the last char 332 | while (true) { 333 | context_data_val = (context_data_val << 1); 334 | if (context_data_position == bitsPerChar - 1) { 335 | context_data.append(getCharFromInt.doFunc(context_data_val)); 336 | break; 337 | } 338 | else 339 | context_data_position++; 340 | } 341 | return context_data.toString(); 342 | } 343 | 344 | private static abstract class DecompressFunctionWrapper { 345 | public abstract char doFunc(int i); 346 | } 347 | protected static class DecData { 348 | public char val; 349 | public int position; 350 | public int index; 351 | } 352 | 353 | public static String f(int i) { 354 | return String.valueOf((char) i); 355 | } 356 | public static char fc(int i) { 357 | return (char) i; 358 | } 359 | 360 | public static String decompress(final String compressed) { 361 | if (compressed == null) 362 | return ""; 363 | if (compressed.isEmpty()) 364 | return null; 365 | return LZString._decompress(compressed.length(), 32768, new DecompressFunctionWrapper() { 366 | @Override 367 | public char doFunc(int i) { 368 | return compressed.charAt(i); 369 | } 370 | }); 371 | } 372 | private static String _decompress(int length, int resetValue, DecompressFunctionWrapper getNextValue) { 373 | List dictionary = new ArrayList(); 374 | // TODO: is next an unused variable in original lz-string? 375 | @SuppressWarnings("unused") 376 | int next; 377 | int enlargeIn = 4; 378 | int dictSize = 4; 379 | int numBits = 3; 380 | String entry = ""; 381 | StringBuilder result = new StringBuilder(); 382 | String w; 383 | int bits, resb; int maxpower, power; 384 | String c = null; 385 | DecData data = new DecData(); 386 | data.val = getNextValue.doFunc(0); 387 | data.position = resetValue; 388 | data.index = 1; 389 | 390 | for (int i = 0; i < 3; i += 1) { 391 | dictionary.add(i, f(i)); 392 | } 393 | 394 | bits = 0; 395 | maxpower = (int) powerOf2(2); 396 | power = 1; 397 | while (power != maxpower) { 398 | resb = data.val & data.position; 399 | data.position >>= 1; 400 | if (data.position == 0) { 401 | data.position = resetValue; 402 | data.val = getNextValue.doFunc(data.index++); 403 | } 404 | bits |= (resb > 0 ? 1 : 0) * power; 405 | power <<= 1; 406 | } 407 | 408 | switch (next = bits) { 409 | case 0: 410 | bits = 0; 411 | maxpower = (int) powerOf2(8); 412 | power=1; 413 | while (power != maxpower) { 414 | resb = data.val & data.position; 415 | data.position >>= 1; 416 | if (data.position == 0) { 417 | data.position = resetValue; 418 | data.val = getNextValue.doFunc(data.index++); 419 | } 420 | bits |= (resb>0 ? 1 : 0) * power; 421 | power <<= 1; 422 | } 423 | c = f(bits); 424 | break; 425 | case 1: 426 | bits = 0; 427 | maxpower = powerOf2(16); 428 | power=1; 429 | while (power!=maxpower) { 430 | resb = data.val & data.position; 431 | data.position >>= 1; 432 | if (data.position == 0) { 433 | data.position = resetValue; 434 | data.val = getNextValue.doFunc(data.index++); 435 | } 436 | bits |= (resb>0 ? 1 : 0) * power; 437 | power <<= 1; 438 | } 439 | c = f(bits); 440 | break; 441 | case 2: 442 | return ""; 443 | } 444 | dictionary.add(3, c); 445 | w = c; 446 | result.append(w); 447 | while (true) { 448 | if (data.index > length) { 449 | return ""; 450 | } 451 | 452 | bits = 0; 453 | maxpower = powerOf2(numBits); 454 | power=1; 455 | while (power!=maxpower) { 456 | resb = data.val & data.position; 457 | data.position >>= 1; 458 | if (data.position == 0) { 459 | data.position = resetValue; 460 | data.val = getNextValue.doFunc(data.index++); 461 | } 462 | bits |= (resb>0 ? 1 : 0) * power; 463 | power <<= 1; 464 | } 465 | // TODO: very strange here, c above is as char/string, here further is a int, rename "c" in the switch as "cc" 466 | int cc; 467 | switch (cc = bits) { 468 | case 0: 469 | bits = 0; 470 | maxpower = powerOf2(8); 471 | power=1; 472 | while (power!=maxpower) { 473 | resb = data.val & data.position; 474 | data.position >>= 1; 475 | if (data.position == 0) { 476 | data.position = resetValue; 477 | data.val = getNextValue.doFunc(data.index++); 478 | } 479 | bits |= (resb>0 ? 1 : 0) * power; 480 | power <<= 1; 481 | } 482 | 483 | dictionary.add(dictSize++, f(bits)); 484 | cc = dictSize-1; 485 | enlargeIn--; 486 | break; 487 | case 1: 488 | bits = 0; 489 | maxpower = powerOf2(16); 490 | power=1; 491 | while (power!=maxpower) { 492 | resb = data.val & data.position; 493 | data.position >>= 1; 494 | if (data.position == 0) { 495 | data.position = resetValue; 496 | data.val = getNextValue.doFunc(data.index++); 497 | } 498 | bits |= (resb>0 ? 1 : 0) * power; 499 | power <<= 1; 500 | } 501 | dictionary.add(dictSize++, f(bits)); 502 | cc = dictSize-1; 503 | enlargeIn--; 504 | break; 505 | case 2: 506 | return result.toString(); 507 | } 508 | 509 | if (enlargeIn == 0) { 510 | enlargeIn = powerOf2(numBits); 511 | numBits++; 512 | } 513 | 514 | if (cc < dictionary.size() && dictionary.get(cc) != null) { 515 | entry = dictionary.get(cc); 516 | } else { 517 | if (cc == dictSize) { 518 | entry = w + w.charAt(0); 519 | } else { 520 | return null; 521 | } 522 | } 523 | result.append(entry); 524 | 525 | // Add w+entry[0] to the dictionary. 526 | dictionary.add(dictSize++, w + entry.charAt(0)); 527 | enlargeIn--; 528 | 529 | w = entry; 530 | 531 | if (enlargeIn == 0) { 532 | enlargeIn = powerOf2(numBits); 533 | numBits++; 534 | } 535 | 536 | } 537 | 538 | } 539 | 540 | private static int powerOf2(int power) { 541 | return 1 << power; 542 | } 543 | 544 | public static void main(String[] args) { 545 | String input; 546 | // input = "hello"; 547 | input = "hello1hello2hello3hello4hello5hello6hello7hello8hello9helloAhelloBhelloChelloDhelloEhelloF"; 548 | 549 | System.out.println(decompress(compress(input))); 550 | System.out.println(decompressFromBase64(compressToBase64(input))); 551 | System.out.println(decompressFromUTF16(compressToUTF16(input))); 552 | System.out.println(decompressFromEncodedURIComponent(compressToEncodedURIComponent(input))); 553 | } 554 | } 555 | -------------------------------------------------------------------------------- /refence/lz-string-1.3.3.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 Pieroxy 2 | // This work is free. You can redistribute it and/or modify it 3 | // under the terms of the WTFPL, Version 2 4 | // For more information see LICENSE.txt or http://www.wtfpl.net/ 5 | // 6 | // For more information, the home page: 7 | // http://pieroxy.net/blog/pages/lz-string/testing.html 8 | // 9 | // LZ-based compression algorithm, version 1.3.3 10 | var LZString = { 11 | 12 | 13 | // private property 14 | _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", 15 | _f : String.fromCharCode, 16 | 17 | compressToBase64 : function (input) { 18 | if (input == null) return ""; 19 | var output = ""; 20 | var chr1, chr2, chr3, enc1, enc2, enc3, enc4; 21 | var i = 0; 22 | 23 | input = LZString.compress(input); 24 | 25 | while (i < input.length*2) { 26 | 27 | if (i%2==0) { 28 | chr1 = input.charCodeAt(i/2) >> 8; 29 | chr2 = input.charCodeAt(i/2) & 255; 30 | if (i/2+1 < input.length) 31 | chr3 = input.charCodeAt(i/2+1) >> 8; 32 | else 33 | chr3 = NaN; 34 | } else { 35 | chr1 = input.charCodeAt((i-1)/2) & 255; 36 | if ((i+1)/2 < input.length) { 37 | chr2 = input.charCodeAt((i+1)/2) >> 8; 38 | chr3 = input.charCodeAt((i+1)/2) & 255; 39 | } else 40 | chr2=chr3=NaN; 41 | } 42 | i+=3; 43 | 44 | enc1 = chr1 >> 2; 45 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 46 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 47 | enc4 = chr3 & 63; 48 | 49 | if (isNaN(chr2)) { 50 | enc3 = enc4 = 64; 51 | } else if (isNaN(chr3)) { 52 | enc4 = 64; 53 | } 54 | 55 | output = output + 56 | LZString._keyStr.charAt(enc1) + LZString._keyStr.charAt(enc2) + 57 | LZString._keyStr.charAt(enc3) + LZString._keyStr.charAt(enc4); 58 | 59 | } 60 | 61 | return output; 62 | }, 63 | 64 | decompressFromBase64 : function (input) { 65 | if (input == null) return ""; 66 | var output = "", 67 | ol = 0, 68 | output_, 69 | chr1, chr2, chr3, 70 | enc1, enc2, enc3, enc4, 71 | i = 0, f=LZString._f; 72 | 73 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 74 | 75 | while (i < input.length) { 76 | 77 | enc1 = LZString._keyStr.indexOf(input.charAt(i++)); 78 | enc2 = LZString._keyStr.indexOf(input.charAt(i++)); 79 | enc3 = LZString._keyStr.indexOf(input.charAt(i++)); 80 | enc4 = LZString._keyStr.indexOf(input.charAt(i++)); 81 | 82 | chr1 = (enc1 << 2) | (enc2 >> 4); 83 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 84 | chr3 = ((enc3 & 3) << 6) | enc4; 85 | 86 | if (ol%2==0) { 87 | output_ = chr1 << 8; 88 | 89 | if (enc3 != 64) { 90 | output += f(output_ | chr2); 91 | } 92 | if (enc4 != 64) { 93 | output_ = chr3 << 8; 94 | } 95 | } else { 96 | output = output + f(output_ | chr1); 97 | 98 | if (enc3 != 64) { 99 | output_ = chr2 << 8; 100 | } 101 | if (enc4 != 64) { 102 | output += f(output_ | chr3); 103 | } 104 | } 105 | ol+=3; 106 | } 107 | 108 | return LZString.decompress(output); 109 | 110 | }, 111 | 112 | compressToUTF16 : function (input) { 113 | if (input == null) return ""; 114 | var output = "", 115 | i,c, 116 | current, 117 | status = 0, 118 | f = LZString._f; 119 | 120 | input = LZString.compress(input); 121 | 122 | for (i=0 ; i> 1)+32); 127 | current = (c & 1) << 14; 128 | break; 129 | case 1: 130 | output += f((current + (c >> 2))+32); 131 | current = (c & 3) << 13; 132 | break; 133 | case 2: 134 | output += f((current + (c >> 3))+32); 135 | current = (c & 7) << 12; 136 | break; 137 | case 3: 138 | output += f((current + (c >> 4))+32); 139 | current = (c & 15) << 11; 140 | break; 141 | case 4: 142 | output += f((current + (c >> 5))+32); 143 | current = (c & 31) << 10; 144 | break; 145 | case 5: 146 | output += f((current + (c >> 6))+32); 147 | current = (c & 63) << 9; 148 | break; 149 | case 6: 150 | output += f((current + (c >> 7))+32); 151 | current = (c & 127) << 8; 152 | break; 153 | case 7: 154 | output += f((current + (c >> 8))+32); 155 | current = (c & 255) << 7; 156 | break; 157 | case 8: 158 | output += f((current + (c >> 9))+32); 159 | current = (c & 511) << 6; 160 | break; 161 | case 9: 162 | output += f((current + (c >> 10))+32); 163 | current = (c & 1023) << 5; 164 | break; 165 | case 10: 166 | output += f((current + (c >> 11))+32); 167 | current = (c & 2047) << 4; 168 | break; 169 | case 11: 170 | output += f((current + (c >> 12))+32); 171 | current = (c & 4095) << 3; 172 | break; 173 | case 12: 174 | output += f((current + (c >> 13))+32); 175 | current = (c & 8191) << 2; 176 | break; 177 | case 13: 178 | output += f((current + (c >> 14))+32); 179 | current = (c & 16383) << 1; 180 | break; 181 | case 14: 182 | output += f((current + (c >> 15))+32, (c & 32767)+32); 183 | status = 0; 184 | break; 185 | } 186 | } 187 | 188 | return output + f(current + 32); 189 | }, 190 | 191 | 192 | decompressFromUTF16 : function (input) { 193 | if (input == null) return ""; 194 | var output = "", 195 | current,c, 196 | status=0, 197 | i = 0, 198 | f = LZString._f; 199 | 200 | while (i < input.length) { 201 | c = input.charCodeAt(i) - 32; 202 | 203 | switch (status++) { 204 | case 0: 205 | current = c << 1; 206 | break; 207 | case 1: 208 | output += f(current | (c >> 14)); 209 | current = (c&16383) << 2; 210 | break; 211 | case 2: 212 | output += f(current | (c >> 13)); 213 | current = (c&8191) << 3; 214 | break; 215 | case 3: 216 | output += f(current | (c >> 12)); 217 | current = (c&4095) << 4; 218 | break; 219 | case 4: 220 | output += f(current | (c >> 11)); 221 | current = (c&2047) << 5; 222 | break; 223 | case 5: 224 | output += f(current | (c >> 10)); 225 | current = (c&1023) << 6; 226 | break; 227 | case 6: 228 | output += f(current | (c >> 9)); 229 | current = (c&511) << 7; 230 | break; 231 | case 7: 232 | output += f(current | (c >> 8)); 233 | current = (c&255) << 8; 234 | break; 235 | case 8: 236 | output += f(current | (c >> 7)); 237 | current = (c&127) << 9; 238 | break; 239 | case 9: 240 | output += f(current | (c >> 6)); 241 | current = (c&63) << 10; 242 | break; 243 | case 10: 244 | output += f(current | (c >> 5)); 245 | current = (c&31) << 11; 246 | break; 247 | case 11: 248 | output += f(current | (c >> 4)); 249 | current = (c&15) << 12; 250 | break; 251 | case 12: 252 | output += f(current | (c >> 3)); 253 | current = (c&7) << 13; 254 | break; 255 | case 13: 256 | output += f(current | (c >> 2)); 257 | current = (c&3) << 14; 258 | break; 259 | case 14: 260 | output += f(current | (c >> 1)); 261 | current = (c&1) << 15; 262 | break; 263 | case 15: 264 | output += f(current | c); 265 | status=0; 266 | break; 267 | } 268 | 269 | 270 | i++; 271 | } 272 | 273 | return LZString.decompress(output); 274 | //return output; 275 | 276 | }, 277 | 278 | 279 | 280 | compress: function (uncompressed) { 281 | if (uncompressed == null) return ""; 282 | var i, value, 283 | context_dictionary= {}, 284 | context_dictionaryToCreate= {}, 285 | context_c="", 286 | context_wc="", 287 | context_w="", 288 | context_enlargeIn= 2, // Compensate for the first entry which should not count 289 | context_dictSize= 3, 290 | context_numBits= 2, 291 | context_data_string="", 292 | context_data_val=0, 293 | context_data_position=0, 294 | ii, 295 | f=LZString._f; 296 | 297 | for (ii = 0; ii < uncompressed.length; ii += 1) { 298 | context_c = uncompressed.charAt(ii); 299 | if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) { 300 | context_dictionary[context_c] = context_dictSize++; 301 | context_dictionaryToCreate[context_c] = true; 302 | } 303 | 304 | context_wc = context_w + context_c; 305 | if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) { 306 | context_w = context_wc; 307 | } else { 308 | if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) { 309 | if (context_w.charCodeAt(0)<256) { 310 | for (i=0 ; i> 1; 331 | } 332 | } else { 333 | value = 1; 334 | for (i=0 ; i> 1; 356 | } 357 | } 358 | context_enlargeIn--; 359 | if (context_enlargeIn == 0) { 360 | context_enlargeIn = Math.pow(2, context_numBits); 361 | context_numBits++; 362 | } 363 | delete context_dictionaryToCreate[context_w]; 364 | } else { 365 | value = context_dictionary[context_w]; 366 | for (i=0 ; i> 1; 376 | } 377 | 378 | 379 | } 380 | context_enlargeIn--; 381 | if (context_enlargeIn == 0) { 382 | context_enlargeIn = Math.pow(2, context_numBits); 383 | context_numBits++; 384 | } 385 | // Add wc to the dictionary. 386 | context_dictionary[context_wc] = context_dictSize++; 387 | context_w = String(context_c); 388 | } 389 | } 390 | 391 | // Output the code for w. 392 | if (context_w !== "") { 393 | if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) { 394 | if (context_w.charCodeAt(0)<256) { 395 | for (i=0 ; i> 1; 416 | } 417 | } else { 418 | value = 1; 419 | for (i=0 ; i> 1; 441 | } 442 | } 443 | context_enlargeIn--; 444 | if (context_enlargeIn == 0) { 445 | context_enlargeIn = Math.pow(2, context_numBits); 446 | context_numBits++; 447 | } 448 | delete context_dictionaryToCreate[context_w]; 449 | } else { 450 | value = context_dictionary[context_w]; 451 | for (i=0 ; i> 1; 461 | } 462 | 463 | 464 | } 465 | context_enlargeIn--; 466 | if (context_enlargeIn == 0) { 467 | context_enlargeIn = Math.pow(2, context_numBits); 468 | context_numBits++; 469 | } 470 | } 471 | 472 | // Mark the end of the stream 473 | value = 2; 474 | for (i=0 ; i> 1; 484 | } 485 | 486 | // Flush the last char 487 | while (true) { 488 | context_data_val = (context_data_val << 1); 489 | if (context_data_position == 15) { 490 | context_data_string += f(context_data_val); 491 | break; 492 | } 493 | else context_data_position++; 494 | } 495 | return context_data_string; 496 | }, 497 | 498 | decompress: function (compressed) { 499 | if (compressed == null) return ""; 500 | if (compressed == "") return null; 501 | var dictionary = [], 502 | next, 503 | enlargeIn = 4, 504 | dictSize = 4, 505 | numBits = 3, 506 | entry = "", 507 | result = "", 508 | i, 509 | w, 510 | bits, resb, maxpower, power, 511 | c, 512 | f = LZString._f, 513 | data = {string:compressed, val:compressed.charCodeAt(0), position:32768, index:1}; 514 | 515 | for (i = 0; i < 3; i += 1) { 516 | dictionary[i] = i; 517 | } 518 | 519 | bits = 0; 520 | maxpower = Math.pow(2,2); 521 | power=1; 522 | while (power!=maxpower) { 523 | resb = data.val & data.position; 524 | data.position >>= 1; 525 | if (data.position == 0) { 526 | data.position = 32768; 527 | data.val = data.string.charCodeAt(data.index++); 528 | } 529 | bits |= (resb>0 ? 1 : 0) * power; 530 | power <<= 1; 531 | } 532 | 533 | switch (next = bits) { 534 | case 0: 535 | bits = 0; 536 | maxpower = Math.pow(2,8); 537 | power=1; 538 | while (power!=maxpower) { 539 | resb = data.val & data.position; 540 | data.position >>= 1; 541 | if (data.position == 0) { 542 | data.position = 32768; 543 | data.val = data.string.charCodeAt(data.index++); 544 | } 545 | bits |= (resb>0 ? 1 : 0) * power; 546 | power <<= 1; 547 | } 548 | c = f(bits); 549 | break; 550 | case 1: 551 | bits = 0; 552 | maxpower = Math.pow(2,16); 553 | power=1; 554 | while (power!=maxpower) { 555 | resb = data.val & data.position; 556 | data.position >>= 1; 557 | if (data.position == 0) { 558 | data.position = 32768; 559 | data.val = data.string.charCodeAt(data.index++); 560 | } 561 | bits |= (resb>0 ? 1 : 0) * power; 562 | power <<= 1; 563 | } 564 | c = f(bits); 565 | break; 566 | case 2: 567 | return ""; 568 | } 569 | dictionary[3] = c; 570 | w = result = c; 571 | while (true) { 572 | if (data.index > data.string.length) { 573 | return ""; 574 | } 575 | 576 | bits = 0; 577 | maxpower = Math.pow(2,numBits); 578 | power=1; 579 | while (power!=maxpower) { 580 | resb = data.val & data.position; 581 | data.position >>= 1; 582 | if (data.position == 0) { 583 | data.position = 32768; 584 | data.val = data.string.charCodeAt(data.index++); 585 | } 586 | bits |= (resb>0 ? 1 : 0) * power; 587 | power <<= 1; 588 | } 589 | 590 | switch (c = bits) { 591 | case 0: 592 | bits = 0; 593 | maxpower = Math.pow(2,8); 594 | power=1; 595 | while (power!=maxpower) { 596 | resb = data.val & data.position; 597 | data.position >>= 1; 598 | if (data.position == 0) { 599 | data.position = 32768; 600 | data.val = data.string.charCodeAt(data.index++); 601 | } 602 | bits |= (resb>0 ? 1 : 0) * power; 603 | power <<= 1; 604 | } 605 | 606 | dictionary[dictSize++] = f(bits); 607 | c = dictSize-1; 608 | enlargeIn--; 609 | break; 610 | case 1: 611 | bits = 0; 612 | maxpower = Math.pow(2,16); 613 | power=1; 614 | while (power!=maxpower) { 615 | resb = data.val & data.position; 616 | data.position >>= 1; 617 | if (data.position == 0) { 618 | data.position = 32768; 619 | data.val = data.string.charCodeAt(data.index++); 620 | } 621 | bits |= (resb>0 ? 1 : 0) * power; 622 | power <<= 1; 623 | } 624 | dictionary[dictSize++] = f(bits); 625 | c = dictSize-1; 626 | enlargeIn--; 627 | break; 628 | case 2: 629 | return result; 630 | } 631 | 632 | if (enlargeIn == 0) { 633 | enlargeIn = Math.pow(2, numBits); 634 | numBits++; 635 | } 636 | 637 | if (dictionary[c]) { 638 | entry = dictionary[c]; 639 | } else { 640 | if (c === dictSize) { 641 | entry = w + w.charAt(0); 642 | } else { 643 | return null; 644 | } 645 | } 646 | result += entry; 647 | 648 | // Add w+entry[0] to the dictionary. 649 | dictionary[dictSize++] = w + entry.charAt(0); 650 | enlargeIn--; 651 | 652 | w = entry; 653 | 654 | if (enlargeIn == 0) { 655 | enlargeIn = Math.pow(2, numBits); 656 | numBits++; 657 | } 658 | 659 | } 660 | } 661 | }; 662 | 663 | if( typeof module !== 'undefined' && module != null ) { 664 | module.exports = LZString 665 | } 666 | --------------------------------------------------------------------------------