-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompressor.js
223 lines (189 loc) · 6.7 KB
/
compressor.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
// ported from the C code at http://www.fastlz.org
// only supports level 1 (fast compression)
(function(window)
{
var MAX_COPY = 32;
var MAX_LEN = 264; /* 256 + 8 */
var MAX_DISTANCE = 8192;
var FASTLZ_READU16 = function(p, i)
{
return p.charCodeAt(i) + (p.charCodeAt(i+1)<<8);
};
var HASH_LOG = 13;
var HASH_SIZE = (1<< HASH_LOG);
var HASH_MASK = (HASH_SIZE-1);
var HASH_FUNCTION = function(p, i)
{
var v = FASTLZ_READU16(p, i);
v ^= FASTLZ_READU16(p, i+1)^(v>>(16-HASH_LOG));
v &= HASH_MASK;
return v;
};
var fastlz_compress = function(ip)
{
var ip_index = 0;
var length = ip.length;
var ip_bound_index = length - 2;
var ip_limit_index = length - 12;
var op = [];
var op_index = 0;
var htab = [];
var hslot_index = 0;
var hval = 0;
var copy = 0;
/* sanity check */
if(length < 4)
{
if(length)
{
/* create literal copy only */
op[op_index++] = String.fromCharCode(length-1) ;
ip_bound_index++;
while(ip_index <= ip_bound_index)
{
op[op_index++] = ip[ip_index++];
}
}
return op.join("");
}
/* initializes hash table */
for (hslot_index = 0; hslot_index < HASH_SIZE; hslot_index++)
{
htab[hslot_index] = ip_index;
}
/* we start with literal copy */
copy = 2;
op[op_index++] = String.fromCharCode(MAX_COPY-1);
op[op_index++] = ip[ip_index++];
op[op_index++] = ip[ip_index++];
/* main loop */
while(ip_index < ip_limit_index)
{
var ref_index = 0;
var distance = 0;
/* minimum match length */
var len = 3;
/* comparison starting-point */
var anchor_index = ip_index;
/* find potential match */
hval = HASH_FUNCTION(ip, ip_index);
hslot_index = hval;
ref_index = htab[hval];
/* calculate distance to the match */
distance = anchor_index - ref_index;
/* update hash table */
htab[hslot_index] = anchor_index;
/* is this a match? check the first 3 bytes */
if(distance===0 ||
(distance >= MAX_DISTANCE) ||
ip[ref_index++] !== ip[ip_index++] || ip[ref_index++] !== ip[ip_index++] || ip[ref_index++] !== ip[ip_index++])
{
/* goto literal: */
op[op_index++] = ip[anchor_index++];
ip_index = anchor_index;
copy++;
if(copy === MAX_COPY)
{
copy = 0;
op[op_index++] = String.fromCharCode(MAX_COPY-1);
}
continue;
}
/* last matched byte */
ip_index = anchor_index + len;
/* distance is biased */
distance--;
if(!distance)
{
/* zero distance means a run */
var x = ip[ip_index-1];
while(ip_index < ip_bound_index) {
if(ip[ref_index++] !== x) { break; } else { ip_index++; }
}
}
else
{
for(;;)
{
/* safe because the outer check against ip limit */
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
if(ip[ref_index++] !== ip[ip_index++]) { break; }
while(ip_index < ip_bound_index)
{
if(ip[ref_index++] !== ip[ip_index++]) { break; }
}
break;
}
}
/* if we have copied something, adjust the copy count */
if(copy) {
/* copy is biased, '0' means 1 byte copy */
op[op_index-copy-1] = String.fromCharCode(copy-1);
}
else
{
/* back, to overwrite the copy count */
op_index--;
}
/* reset literal counter */
copy = 0;
/* length is biased, '1' means a match of 3 bytes */
ip_index -= 3;
len = ip_index - anchor_index;
while(len > MAX_LEN-2) {
op[op_index++] = String.fromCharCode((7 << 5) + (distance >> 8));
op[op_index++] = String.fromCharCode(MAX_LEN - 2 - 7 -2);
op[op_index++] = String.fromCharCode((distance & 255));
len -= MAX_LEN-2;
}
if(len < 7)
{
op[op_index++] = String.fromCharCode((len << 5) + (distance >> 8));
op[op_index++] = String.fromCharCode((distance & 255));
}
else
{
op[op_index++] = String.fromCharCode((7 << 5) + (distance >> 8));
op[op_index++] = String.fromCharCode(len - 7);
op[op_index++] = String.fromCharCode((distance & 255));
}
/* update the hash at match boundary */
hval = HASH_FUNCTION(ip, ip_index);
htab[hval] = ip_index++;
hval = HASH_FUNCTION(ip, ip_index);
htab[hval] = ip_index++;
/* assuming literal copy */
op[op_index++] = String.fromCharCode(MAX_COPY-1);
}
/* left-over as literal copy */
ip_bound_index++;
while(ip_index <= ip_bound_index)
{
op[op_index++] = ip[ip_index++];
copy++;
if(copy === MAX_COPY)
{
copy = 0;
op[op_index++] = String.fromCharCode(MAX_COPY-1);
}
}
/* if we have copied something, adjust the copy length */
if(copy)
{
op[op_index-copy-1] = String.fromCharCode(copy-1);
}
else
{
op_index--;
}
return op.slice(0,op_index).join("");
};
if (! window.FastLz) { FastLz = {}; }
FastLz.compressor = fastlz_compress;
})(window);