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