-
Notifications
You must be signed in to change notification settings - Fork 1
/
lzf.js
282 lines (250 loc) · 8.35 KB
/
lzf.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Date : June 16 2012
// Converted by Pantelis Kalogiros to support binary data
// previous info
/**
* LZF - LZF compression implementation for JavaScript
*
* Provides LZF compression/decompression of strings
* Compatible with lzf_compress & lzf_decompress PHP functions (see LZF in PECL)
* Based on C source of LZF functions by Marc Alexander Lehmann ([email protected])
* Based on C source of LZF PHP extension by Marcin Gibula ([email protected])
*
* @class Provides methods for LZF compression/decompression
* @author Alexey A.Znaev ([email protected]) (http://xbsoft.org)
* @copyright Copyright (C) 2011-2012 Alexey A.Znaev
* @license http://www.gnu.org/licenses GNU Public License version 3
* @version 1.0
*/
(function( parent ) {
"use strict";
parent = parent || window;
// ArrayBuffer.slice shim
if( ArrayBuffer && !ArrayBuffer.prototype.slice )
ArrayBuffer.prototype.slice = function(a, b) {
var e = new Uint8Array( this ), c;
!a && ( a = 0 );
!b && ( b = this.byteLength );
e = e.subarray( a, b );
c = new Uint8Array( b - a );
c.set( e );
return c.buffer;
};
// class constants
var _HLOG = 16,
_HSIZE = ( 1 << ( _HLOG ) ),
_MAX_LIT = ( 1 << 5 ),
_MAX_OFF = ( 1 << 13 ),
_MAX_REF = ( ( 1 << 8 ) + ( 1 << 3 ) ),
// private methods
_FRST = function( p, sD ) {
return( ( ( sD[ p ] ) << 8 ) | sD[ p + 1 ] );
},
_NEXT = function( v, p, sD ) {
return( ( v << 8 ) | sD[ p + 2 ] );
},
_IDX = function( h ) {
return( ( ( h >> ( 3 * 8 - _HLOG ) ) - h ) & ( _HSIZE - 1 ) );
};
//
// Main Object
//
parent.LZF = {
/**
* @return ArrayBuffer the decompressed data
*
* @param ArrayBuffer compressed arraybuffer
* @param Boolean if true, then the first 32bits are registered as the expected
* decompressed size. If false/undefined a regular js array is used
**/
decompress : function lzfDecode( data, hasSize ) {
var range = new Uint32Array( data, 0, 1 ),
sC = new Uint8Array( data, hasSize ? 4 : 0 ),
clock = 0; //aRes length
var ip = 0, iEnd = sC.byteLength,
aRes,
ctrl, len, ref;
hasSize && ( aRes = new Uint8Array( range[ 0 ] ) ) || ( aRes = [] );
do {
ctrl = sC[ ip++ ];
if(ctrl < (1 << 5))
{
++ctrl;
if( ip + ctrl > iEnd ) console.log( "LZF JS ERROR 1 ");
switch (ctrl)
{
case 32: aRes[ clock++ ] =(sC[(ip++)]); case 31: aRes[ clock++ ] =(sC[(ip++)]); case 30: aRes[ clock++ ] =(sC[(ip++)]); case 29: aRes[ clock++ ] =(sC[(ip++)]);
case 28: aRes[ clock++ ] =(sC[(ip++)]); case 27: aRes[ clock++ ] =(sC[(ip++)]); case 26: aRes[ clock++ ] =(sC[(ip++)]); case 25: aRes[ clock++ ] =(sC[(ip++)]);
case 24: aRes[ clock++ ] =(sC[(ip++)]); case 23: aRes[ clock++ ] =(sC[(ip++)]); case 22: aRes[ clock++ ] =(sC[(ip++)]); case 21: aRes[ clock++ ] =(sC[(ip++)]);
case 20: aRes[ clock++ ] =(sC[(ip++)]); case 19: aRes[ clock++ ] =(sC[(ip++)]); case 18: aRes[ clock++ ] =(sC[(ip++)]); case 17: aRes[ clock++ ] =(sC[(ip++)]);
case 16: aRes[ clock++ ] =(sC[(ip++)]); case 15: aRes[ clock++ ] =(sC[(ip++)]); case 14: aRes[ clock++ ] =(sC[(ip++)]); case 13: aRes[ clock++ ] =(sC[(ip++)]);
case 12: aRes[ clock++ ] =(sC[(ip++)]); case 11: aRes[ clock++ ] =(sC[(ip++)]); case 10: aRes[ clock++ ] =(sC[(ip++)]); case 9: aRes[ clock++ ] =(sC[(ip++)]);
case 8: aRes[ clock++ ] =(sC[(ip++)]); case 7: aRes[ clock++ ] =(sC[(ip++)]); case 6: aRes[ clock++ ] =(sC[(ip++)]); case 5: aRes[ clock++ ] =(sC[(ip++)]);
case 4: aRes[ clock++ ] =(sC[(ip++)]); case 3: aRes[ clock++ ] =(sC[(ip++)]); case 2: aRes[ clock++ ] =(sC[(ip++)]); case 1: aRes[ clock++ ] =(sC[(ip++)]);
}
}
else
{
len = ctrl >> 5;
ref = clock - ((ctrl & 0x1f) << 8) - 1;
if(ip > iEnd) console.log( "LZF JS ERROR 2 ");
if(len == 7){
len += sC[(ip++)];
if(ip > iEnd) console.log( "LZF JS ERROR 3 ");
}
ref -= sC[(ip++)];
if(ref < 0) console.log( "LZF JS ERROR 4 ");
if(ref >= clock) console.log( "LZF JS ERROR 5 ");
switch (len)
{
default:
{
len += 2;
if (clock >= ref + len)
{
// Disjunct areas
for( var jk = 0; jk < len; ++jk )
aRes[ clock++ ] =( aRes[ ref + jk ] );
}
else
{
do
aRes[ clock++ ] =(aRes[ref++]);
while (--len);
}
break;
}
case 9: aRes[ clock++ ] =(aRes[ref++]);
case 8: aRes[ clock++ ] =(aRes[ref++]);
case 7: aRes[ clock++ ] =(aRes[ref++]);
case 6: aRes[ clock++ ] =(aRes[ref++]);
case 5: aRes[ clock++ ] =(aRes[ref++]);
case 4: aRes[ clock++ ] =(aRes[ref++]);
case 3: aRes[ clock++ ] =(aRes[ref++]);
case 2: aRes[ clock++ ] =(aRes[ref++]);
case 1: aRes[ clock++ ] =(aRes[ref++]);
case 0: aRes[ clock++ ] =(aRes[ref++]); // two octets more
aRes[ clock++ ] =(aRes[ref++]);
}
}
} while( ip < iEnd );
if( !hasSize )
{
var ret = new Uint8Array( aRes.length );
ret.set( aRes );
return ret.buffer;
}
return aRes.buffer; //returning a buffer
},
/**
* @return ArrayBuffer compressed data
*
* @param ArrayBuffer the data we want to compress
* @param Boolean if true, then the first 32bits are registered as the expected
* decompressed size. If false, or undefined no overhead is added
**/
compress : function( inputBuffer, includeSize ) {
var sD = new Uint8Array( inputBuffer ),
htab = [],
iEnd = sD.byteLength,
aRes = [],
ip = 0,
op = 1,
lit = 0,
hval,
hslot,
ref,
off,
len,
maxlen;
hval = _FRST( ip, sD );
while( ip < iEnd - 2 ) {
hval = _NEXT( hval, ip, sD );
hslot = _IDX( hval );
ref = htab[ hslot ] ? htab[ hslot ] : 0;
htab[ hslot ] = ip;
if( ref < ip
&&( off = ip - ref - 1 ) < _MAX_OFF
&& ip + 4 < iEnd
&& ref > 0
&& sD[ ref ] === sD[ ip ]
&& sD[ ref + 1 ] === sD[ ip + 1 ]
&& sD[ ref + 2 ] === sD[ ip + 2 ]
)
{
len = 2;
maxlen = iEnd - ip - len;
maxlen = maxlen > _MAX_REF ? _MAX_REF : maxlen;
if( lit > 0 )
aRes[ op - lit - 1 ] = ( lit - 1 ) & 255;
else
--op;
for(;;) {
if( maxlen > 16 ) {
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
++len; if( sD[ ref + len ] != sD[ ip + len ] ) break;
}
do len++; while( len < maxlen && sD[ ref + len ] == sD[ ip + len ] );
break;
}
len -= 2;
++ip;
if( len < 7 )
aRes[ op++ ] = ( ( off >> 8 ) + ( len << 5 ) ) & 255;
else {
aRes[ op++ ] = ( ( off >> 8 ) +( 7 << 5 ) ) & 255;
aRes[ op++ ] = ( len - 7 ) & 255;
}
aRes[ op++ ] = ( off ) & 255;
lit = 0;
++op;
ip += len + 1;
if( ip >= iEnd - 2 )
break;
--ip;
hval = _FRST( ip, sD );
hval = _NEXT( hval, ip, sD );
htab[ _IDX( hval ) ] = ip++;
} else {
++lit;
aRes[ op++ ] = ( sD[ ip++ ] ) & 255;
if( lit === _MAX_LIT ) {
aRes[ op - lit - 1 ] = ( lit - 1 ) & 255;
lit = 0;
++op;
}
}
}
while( ip < iEnd ) {
++lit;
aRes[ op++ ] = ( sD[ ip++ ] ) & 255;
if( lit === _MAX_LIT ) {
aRes[ op - lit - 1 ] = ( lit - 1 ) & 255;
lit = 0;
++op;
}
}
sD = null;
if( lit > 0 )
aRes[ op - lit - 1 ] = ( lit - 1 ) & 255;
if( includeSize )
aRes.unshift( aRes.length );
var ret = new Uint8Array( aRes.length );
ret.set( aRes );
return ret.buffer;
}
};
})( window ); // <-- your object here