File size: 5,350 Bytes
04ec17f |
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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
// Copyright © 2013 Pieroxy <pieroxy@pieroxy.net>
// This work is free. You can redistribute it and/or modify it
// under the terms of the WTFPL, Version 2
// For more information see LICENSE.txt or http://www.wtfpl.net/
//
// LZ-based compression algorithm, version 1.0.2-rc1
var LZString = {
writeBit : function(value, data) {
data.val = (data.val << 1) | value;
if (data.position == 15) {
data.position = 0;
data.string += String.fromCharCode(data.val);
data.val = 0;
} else {
data.position++;
}
},
writeBits : function(numBits, value, data) {
if (typeof(value)=="string")
value = value.charCodeAt(0);
for (var i=0 ; i<numBits ; i++) {
this.writeBit(value&1, data);
value = value >> 1;
}
},
produceW : function (context) {
if (Object.prototype.hasOwnProperty.call(context.dictionaryToCreate,context.w)) {
if (context.w.charCodeAt(0)<256) {
this.writeBits(context.numBits, 0, context.data);
this.writeBits(8, context.w, context.data);
} else {
this.writeBits(context.numBits, 1, context.data);
this.writeBits(16, context.w, context.data);
}
this.decrementEnlargeIn(context);
delete context.dictionaryToCreate[context.w];
} else {
this.writeBits(context.numBits, context.dictionary[context.w], context.data);
}
this.decrementEnlargeIn(context);
},
decrementEnlargeIn : function(context) {
context.enlargeIn--;
if (context.enlargeIn == 0) {
context.enlargeIn = Math.pow(2, context.numBits);
context.numBits++;
}
},
compress: function (uncompressed) {
var context = {
dictionary: {},
dictionaryToCreate: {},
c:"",
wc:"",
w:"",
enlargeIn: 2, // Compensate for the first entry which should not count
dictSize: 3,
numBits: 2,
result: "",
data: {string:"", val:0, position:0}
}, i;
for (i = 0; i < uncompressed.length; i += 1) {
context.c = uncompressed.charAt(i);
if (!Object.prototype.hasOwnProperty.call(context.dictionary,context.c)) {
context.dictionary[context.c] = context.dictSize++;
context.dictionaryToCreate[context.c] = true;
}
context.wc = context.w + context.c;
if (Object.prototype.hasOwnProperty.call(context.dictionary,context.wc)) {
context.w = context.wc;
} else {
this.produceW(context);
// Add wc to the dictionary.
context.dictionary[context.wc] = context.dictSize++;
context.w = String(context.c);
}
}
// Output the code for w.
if (context.w !== "") {
this.produceW(context);
}
// Mark the end of the stream
this.writeBits(context.numBits, 2, context.data);
// Flush the last char
while (context.data.val>0) this.writeBit(0,context.data)
return context.data.string;
},
readBit : function(data) {
var res = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = 32768;
data.val = data.string.charCodeAt(data.index++);
}
//data.val = (data.val << 1);
return res>0 ? 1 : 0;
},
readBits : function(numBits, data) {
var res = 0;
var maxpower = Math.pow(2,numBits);
var power=1;
while (power!=maxpower) {
res |= this.readBit(data) * power;
power <<= 1;
}
return res;
},
decompress: function (compressed) {
var dictionary = {},
next,
enlargeIn = 4,
dictSize = 4,
numBits = 3,
entry = "",
result = "",
i,
w,
c,
errorCount=0,
literal,
data = {string:compressed, val:compressed.charCodeAt(0), position:32768, index:1};
for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
}
next = this.readBits(2, data);
switch (next) {
case 0:
c = String.fromCharCode(this.readBits(8, data));
break;
case 1:
c = String.fromCharCode(this.readBits(16, data));
break;
case 2:
return "";
}
dictionary[3] = c;
w = result = c;
while (true) {
c = this.readBits(numBits, data);
switch (c) {
case 0:
if (errorCount++ > 10000) return "Error";
c = String.fromCharCode(this.readBits(8, data));
dictionary[dictSize++] = c;
c = dictSize-1;
enlargeIn--;
break;
case 1:
c = String.fromCharCode(this.readBits(16, data));
dictionary[dictSize++] = c;
c = dictSize-1;
enlargeIn--;
break;
case 2:
return result;
}
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c];
} else {
if (c === dictSize) {
entry = w + w.charAt(0);
} else {
return null;
}
}
result += entry;
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry.charAt(0);
enlargeIn--;
w = entry;
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
return result;
}
};
|