Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / InfCodes.js
1 Clazz.declarePackage ("JU");
2 c$ = Clazz.decorateAsClass (function () {
3 this.mode = 0;
4 this.len = 0;
5 this.tree = null;
6 this.tree_index = 0;
7 this.need = 0;
8 this.lit = 0;
9 this.get = 0;
10 this.dist = 0;
11 this.lbits = 0;
12 this.dbits = 0;
13 this.ltree = null;
14 this.ltree_index = 0;
15 this.dtree = null;
16 this.dtree_index = 0;
17 this.z = null;
18 this.s = null;
19 Clazz.instantialize (this, arguments);
20 }, JU, "InfCodes");
21 Clazz.makeConstructor (c$, 
22 function (z, s) {
23 this.z = z;
24 this.s = s;
25 }, "JU.ZStream,JU.InfBlocks");
26 Clazz.defineMethod (c$, "init", 
27 function (bl, bd, tl, tl_index, td, td_index) {
28 this.mode = 0;
29 this.lbits = bl;
30 this.dbits = bd;
31 this.ltree = tl;
32 this.ltree_index = tl_index;
33 this.dtree = td;
34 this.dtree_index = td_index;
35 this.tree = null;
36 }, "~N,~N,~A,~N,~A,~N");
37 Clazz.defineMethod (c$, "proc", 
38 function (r) {
39 var j;
40 var tindex;
41 var e;
42 var b = 0;
43 var k = 0;
44 var p = 0;
45 var n;
46 var q;
47 var m;
48 var f;
49 p = this.z.next_in_index;
50 n = this.z.avail_in;
51 b = this.s.bitb;
52 k = this.s.bitk;
53 q = this.s.write;
54 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
55 while (true) {
56 switch (this.mode) {
57 case 0:
58 if (m >= 258 && n >= 10) {
59 this.s.bitb = b;
60 this.s.bitk = k;
61 this.z.avail_in = n;
62 this.z.total_in += p - this.z.next_in_index;
63 this.z.next_in_index = p;
64 this.s.write = q;
65 r = this.inflate_fast (this.lbits, this.dbits, this.ltree, this.ltree_index, this.dtree, this.dtree_index, this.s, this.z);
66 p = this.z.next_in_index;
67 n = this.z.avail_in;
68 b = this.s.bitb;
69 k = this.s.bitk;
70 q = this.s.write;
71 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
72 if (r != 0) {
73 this.mode = r == 1 ? 7 : 9;
74 break;
75 }}this.need = this.lbits;
76 this.tree = this.ltree;
77 this.tree_index = this.ltree_index;
78 this.mode = 1;
79 case 1:
80 j = this.need;
81 while (k < (j)) {
82 if (n != 0) r = 0;
83  else {
84 this.s.bitb = b;
85 this.s.bitk = k;
86 this.z.avail_in = n;
87 this.z.total_in += p - this.z.next_in_index;
88 this.z.next_in_index = p;
89 this.s.write = q;
90 return this.s.inflate_flush (r);
91 }n--;
92 b |= (this.z.next_in[p++] & 0xff) << k;
93 k += 8;
94 }
95 tindex = (this.tree_index + (b & JU.InfCodes.inflate_mask[j])) * 3;
96 b >>>= (this.tree[tindex + 1]);
97 k -= (this.tree[tindex + 1]);
98 e = this.tree[tindex];
99 if (e == 0) {
100 this.lit = this.tree[tindex + 2];
101 this.mode = 6;
102 break;
103 }if ((e & 16) != 0) {
104 this.get = e & 15;
105 this.len = this.tree[tindex + 2];
106 this.mode = 2;
107 break;
108 }if ((e & 64) == 0) {
109 this.need = e;
110 this.tree_index = Clazz.doubleToInt (tindex / 3) + this.tree[tindex + 2];
111 break;
112 }if ((e & 32) != 0) {
113 this.mode = 7;
114 break;
115 }this.mode = 9;
116 this.z.msg = "invalid literal/length code";
117 r = -3;
118 this.s.bitb = b;
119 this.s.bitk = k;
120 this.z.avail_in = n;
121 this.z.total_in += p - this.z.next_in_index;
122 this.z.next_in_index = p;
123 this.s.write = q;
124 return this.s.inflate_flush (r);
125 case 2:
126 j = this.get;
127 while (k < (j)) {
128 if (n != 0) r = 0;
129  else {
130 this.s.bitb = b;
131 this.s.bitk = k;
132 this.z.avail_in = n;
133 this.z.total_in += p - this.z.next_in_index;
134 this.z.next_in_index = p;
135 this.s.write = q;
136 return this.s.inflate_flush (r);
137 }n--;
138 b |= (this.z.next_in[p++] & 0xff) << k;
139 k += 8;
140 }
141 this.len += (b & JU.InfCodes.inflate_mask[j]);
142 b >>= j;
143 k -= j;
144 this.need = this.dbits;
145 this.tree = this.dtree;
146 this.tree_index = this.dtree_index;
147 this.mode = 3;
148 case 3:
149 j = this.need;
150 while (k < (j)) {
151 if (n != 0) r = 0;
152  else {
153 this.s.bitb = b;
154 this.s.bitk = k;
155 this.z.avail_in = n;
156 this.z.total_in += p - this.z.next_in_index;
157 this.z.next_in_index = p;
158 this.s.write = q;
159 return this.s.inflate_flush (r);
160 }n--;
161 b |= (this.z.next_in[p++] & 0xff) << k;
162 k += 8;
163 }
164 tindex = (this.tree_index + (b & JU.InfCodes.inflate_mask[j])) * 3;
165 b >>= this.tree[tindex + 1];
166 k -= this.tree[tindex + 1];
167 e = (this.tree[tindex]);
168 if ((e & 16) != 0) {
169 this.get = e & 15;
170 this.dist = this.tree[tindex + 2];
171 this.mode = 4;
172 break;
173 }if ((e & 64) == 0) {
174 this.need = e;
175 this.tree_index = Clazz.doubleToInt (tindex / 3) + this.tree[tindex + 2];
176 break;
177 }this.mode = 9;
178 this.z.msg = "invalid distance code";
179 r = -3;
180 this.s.bitb = b;
181 this.s.bitk = k;
182 this.z.avail_in = n;
183 this.z.total_in += p - this.z.next_in_index;
184 this.z.next_in_index = p;
185 this.s.write = q;
186 return this.s.inflate_flush (r);
187 case 4:
188 j = this.get;
189 while (k < (j)) {
190 if (n != 0) r = 0;
191  else {
192 this.s.bitb = b;
193 this.s.bitk = k;
194 this.z.avail_in = n;
195 this.z.total_in += p - this.z.next_in_index;
196 this.z.next_in_index = p;
197 this.s.write = q;
198 return this.s.inflate_flush (r);
199 }n--;
200 b |= (this.z.next_in[p++] & 0xff) << k;
201 k += 8;
202 }
203 this.dist += (b & JU.InfCodes.inflate_mask[j]);
204 b >>= j;
205 k -= j;
206 this.mode = 5;
207 case 5:
208 f = q - this.dist;
209 while (f < 0) {
210 f += this.s.end;
211 }
212 while (this.len != 0) {
213 if (m == 0) {
214 if (q == this.s.end && this.s.read != 0) {
215 q = 0;
216 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
217 }if (m == 0) {
218 this.s.write = q;
219 r = this.s.inflate_flush (r);
220 q = this.s.write;
221 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
222 if (q == this.s.end && this.s.read != 0) {
223 q = 0;
224 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
225 }if (m == 0) {
226 this.s.bitb = b;
227 this.s.bitk = k;
228 this.z.avail_in = n;
229 this.z.total_in += p - this.z.next_in_index;
230 this.z.next_in_index = p;
231 this.s.write = q;
232 return this.s.inflate_flush (r);
233 }}}this.s.window[q++] = this.s.window[f++];
234 m--;
235 if (f == this.s.end) f = 0;
236 this.len--;
237 }
238 this.mode = 0;
239 break;
240 case 6:
241 if (m == 0) {
242 if (q == this.s.end && this.s.read != 0) {
243 q = 0;
244 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
245 }if (m == 0) {
246 this.s.write = q;
247 r = this.s.inflate_flush (r);
248 q = this.s.write;
249 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
250 if (q == this.s.end && this.s.read != 0) {
251 q = 0;
252 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
253 }if (m == 0) {
254 this.s.bitb = b;
255 this.s.bitk = k;
256 this.z.avail_in = n;
257 this.z.total_in += p - this.z.next_in_index;
258 this.z.next_in_index = p;
259 this.s.write = q;
260 return this.s.inflate_flush (r);
261 }}}r = 0;
262 this.s.window[q++] = this.lit;
263 m--;
264 this.mode = 0;
265 break;
266 case 7:
267 if (k > 7) {
268 k -= 8;
269 n++;
270 p--;
271 }this.s.write = q;
272 r = this.s.inflate_flush (r);
273 q = this.s.write;
274 m = q < this.s.read ? this.s.read - q - 1 : this.s.end - q;
275 if (this.s.read != this.s.write) {
276 this.s.bitb = b;
277 this.s.bitk = k;
278 this.z.avail_in = n;
279 this.z.total_in += p - this.z.next_in_index;
280 this.z.next_in_index = p;
281 this.s.write = q;
282 return this.s.inflate_flush (r);
283 }this.mode = 8;
284 case 8:
285 r = 1;
286 this.s.bitb = b;
287 this.s.bitk = k;
288 this.z.avail_in = n;
289 this.z.total_in += p - this.z.next_in_index;
290 this.z.next_in_index = p;
291 this.s.write = q;
292 return this.s.inflate_flush (r);
293 case 9:
294 r = -3;
295 this.s.bitb = b;
296 this.s.bitk = k;
297 this.z.avail_in = n;
298 this.z.total_in += p - this.z.next_in_index;
299 this.z.next_in_index = p;
300 this.s.write = q;
301 return this.s.inflate_flush (r);
302 default:
303 r = -2;
304 this.s.bitb = b;
305 this.s.bitk = k;
306 this.z.avail_in = n;
307 this.z.total_in += p - this.z.next_in_index;
308 this.z.next_in_index = p;
309 this.s.write = q;
310 return this.s.inflate_flush (r);
311 }
312 }
313 }, "~N");
314 Clazz.defineMethod (c$, "free", 
315 function (z) {
316 }, "JU.ZStream");
317 Clazz.defineMethod (c$, "inflate_fast", 
318 function (bl, bd, tl, tl_index, td, td_index, s, z) {
319 var t;
320 var tp;
321 var tp_index;
322 var e;
323 var b;
324 var k;
325 var p;
326 var n;
327 var q;
328 var m;
329 var ml;
330 var md;
331 var c;
332 var d;
333 var r;
334 var tp_index_t_3;
335 p = z.next_in_index;
336 n = z.avail_in;
337 b = s.bitb;
338 k = s.bitk;
339 q = s.write;
340 m = q < s.read ? s.read - q - 1 : s.end - q;
341 ml = JU.InfCodes.inflate_mask[bl];
342 md = JU.InfCodes.inflate_mask[bd];
343 do {
344 while (k < (20)) {
345 n--;
346 b |= (z.next_in[p++] & 0xff) << k;
347 k += 8;
348 }
349 t = b & ml;
350 tp = tl;
351 tp_index = tl_index;
352 tp_index_t_3 = (tp_index + t) * 3;
353 if ((e = tp[tp_index_t_3]) == 0) {
354 b >>= (tp[tp_index_t_3 + 1]);
355 k -= (tp[tp_index_t_3 + 1]);
356 s.window[q++] = tp[tp_index_t_3 + 2];
357 m--;
358 continue;
359 }do {
360 b >>= (tp[tp_index_t_3 + 1]);
361 k -= (tp[tp_index_t_3 + 1]);
362 if ((e & 16) != 0) {
363 e &= 15;
364 c = tp[tp_index_t_3 + 2] + (b & JU.InfCodes.inflate_mask[e]);
365 b >>= e;
366 k -= e;
367 while (k < (15)) {
368 n--;
369 b |= (z.next_in[p++] & 0xff) << k;
370 k += 8;
371 }
372 t = b & md;
373 tp = td;
374 tp_index = td_index;
375 tp_index_t_3 = (tp_index + t) * 3;
376 e = tp[tp_index_t_3];
377 do {
378 b >>= (tp[tp_index_t_3 + 1]);
379 k -= (tp[tp_index_t_3 + 1]);
380 if ((e & 16) != 0) {
381 e &= 15;
382 while (k < (e)) {
383 n--;
384 b |= (z.next_in[p++] & 0xff) << k;
385 k += 8;
386 }
387 d = tp[tp_index_t_3 + 2] + (b & JU.InfCodes.inflate_mask[e]);
388 b >>= (e);
389 k -= (e);
390 m -= c;
391 if (q >= d) {
392 r = q - d;
393 if (q - r > 0 && 2 > (q - r)) {
394 s.window[q++] = s.window[r++];
395 s.window[q++] = s.window[r++];
396 c -= 2;
397 } else {
398 System.arraycopy (s.window, r, s.window, q, 2);
399 q += 2;
400 r += 2;
401 c -= 2;
402 }} else {
403 r = q - d;
404 do {
405 r += s.end;
406 } while (r < 0);
407 e = s.end - r;
408 if (c > e) {
409 c -= e;
410 if (q - r > 0 && e > (q - r)) {
411 do {
412 s.window[q++] = s.window[r++];
413 } while (--e != 0);
414 } else {
415 System.arraycopy (s.window, r, s.window, q, e);
416 q += e;
417 r += e;
418 e = 0;
419 }r = 0;
420 }}if (q - r > 0 && c > (q - r)) {
421 do {
422 s.window[q++] = s.window[r++];
423 } while (--c != 0);
424 } else {
425 System.arraycopy (s.window, r, s.window, q, c);
426 q += c;
427 r += c;
428 c = 0;
429 }break;
430 } else if ((e & 64) == 0) {
431 t += tp[tp_index_t_3 + 2];
432 t += (b & JU.InfCodes.inflate_mask[e]);
433 tp_index_t_3 = (tp_index + t) * 3;
434 e = tp[tp_index_t_3];
435 } else {
436 z.msg = "invalid distance code";
437 c = z.avail_in - n;
438 c = (k >> 3) < c ? k >> 3 : c;
439 n += c;
440 p -= c;
441 k -= c << 3;
442 s.bitb = b;
443 s.bitk = k;
444 z.avail_in = n;
445 z.total_in += p - z.next_in_index;
446 z.next_in_index = p;
447 s.write = q;
448 return -3;
449 }} while (true);
450 break;
451 }if ((e & 64) == 0) {
452 t += tp[tp_index_t_3 + 2];
453 t += (b & JU.InfCodes.inflate_mask[e]);
454 tp_index_t_3 = (tp_index + t) * 3;
455 if ((e = tp[tp_index_t_3]) == 0) {
456 b >>= (tp[tp_index_t_3 + 1]);
457 k -= (tp[tp_index_t_3 + 1]);
458 s.window[q++] = tp[tp_index_t_3 + 2];
459 m--;
460 break;
461 }} else if ((e & 32) != 0) {
462 c = z.avail_in - n;
463 c = (k >> 3) < c ? k >> 3 : c;
464 n += c;
465 p -= c;
466 k -= c << 3;
467 s.bitb = b;
468 s.bitk = k;
469 z.avail_in = n;
470 z.total_in += p - z.next_in_index;
471 z.next_in_index = p;
472 s.write = q;
473 return 1;
474 } else {
475 z.msg = "invalid literal/length code";
476 c = z.avail_in - n;
477 c = (k >> 3) < c ? k >> 3 : c;
478 n += c;
479 p -= c;
480 k -= c << 3;
481 s.bitb = b;
482 s.bitk = k;
483 z.avail_in = n;
484 z.total_in += p - z.next_in_index;
485 z.next_in_index = p;
486 s.write = q;
487 return -3;
488 }} while (true);
489 } while (m >= 258 && n >= 10);
490 c = z.avail_in - n;
491 c = (k >> 3) < c ? k >> 3 : c;
492 n += c;
493 p -= c;
494 k -= c << 3;
495 s.bitb = b;
496 s.bitk = k;
497 z.avail_in = n;
498 z.total_in += p - z.next_in_index;
499 z.next_in_index = p;
500 s.write = q;
501 return 0;
502 }, "~N,~N,~A,~N,~A,~N,JU.InfBlocks,JU.ZStream");
503 Clazz.defineStatics (c$,
504 "inflate_mask",  Clazz.newIntArray (-1, [0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff]),
505 "Z_OK", 0,
506 "Z_STREAM_END", 1,
507 "Z_STREAM_ERROR", -2,
508 "Z_DATA_ERROR", -3,
509 "START", 0,
510 "LEN", 1,
511 "LENEXT", 2,
512 "DIST", 3,
513 "DISTEXT", 4,
514 "COPY", 5,
515 "LIT", 6,
516 "WASH", 7,
517 "END", 8,
518 "BADCODE", 9);