Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / Inflate.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["java.lang.Exception"], "JU.Inflate", ["JU.Adler32", "$.CRC32", "$.GZIPHeader", "$.InfBlocks", "java.io.ByteArrayOutputStream"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.mode = 0;
5 this.method = 0;
6 this.was = -1;
7 this.need = 0;
8 this.marker = 0;
9 this.wrap = 0;
10 this.wbits = 0;
11 this.blocks = null;
12 this.z = null;
13 this.flags = 0;
14 this.need_bytes = -1;
15 this.crcbuf = null;
16 this.gheader = null;
17 if (!Clazz.isClassDefined ("JU.Inflate.Return")) {
18 JU.Inflate.$Inflate$Return$ ();
19 }
20 this.tmp_string = null;
21 Clazz.instantialize (this, arguments);
22 }, JU, "Inflate");
23 Clazz.prepareFields (c$, function () {
24 this.crcbuf =  Clazz.newByteArray (4, 0);
25 });
26 Clazz.defineMethod (c$, "reset", 
27 function () {
28 this.inflateReset ();
29 });
30 Clazz.defineMethod (c$, "inflateReset", 
31 function () {
32 if (this.z == null) return -2;
33 this.z.total_in = this.z.total_out = 0;
34 this.z.msg = null;
35 this.mode = 14;
36 this.need_bytes = -1;
37 this.blocks.reset ();
38 return 0;
39 });
40 Clazz.defineMethod (c$, "inflateEnd", 
41 function () {
42 if (this.blocks != null) {
43 this.blocks.free ();
44 }return 0;
45 });
46 Clazz.makeConstructor (c$, 
47 function (z) {
48 this.z = z;
49 }, "JU.ZStream");
50 Clazz.defineMethod (c$, "inflateInit", 
51 function (w) {
52 this.z.msg = null;
53 this.blocks = null;
54 this.wrap = 0;
55 if (w < 0) {
56 w = -w;
57 } else {
58 this.wrap = (w >> 4) + 1;
59 if (w < 48) w &= 15;
60 }if (w < 8 || w > 15) {
61 this.inflateEnd ();
62 return -2;
63 }if (this.blocks != null && this.wbits != w) {
64 this.blocks.free ();
65 this.blocks = null;
66 }this.wbits = w;
67 this.blocks =  new JU.InfBlocks (this.z, 1 << w);
68 this.inflateReset ();
69 return 0;
70 }, "~N");
71 Clazz.defineMethod (c$, "inflate", 
72 function (f) {
73 var r;
74 var b;
75 if (this.z == null || this.z.next_in == null) {
76 if (f == 4 && this.mode == 14) return 0;
77 return -2;
78 }f = f == 4 ? -5 : 0;
79 r = -5;
80 while (true) {
81 switch (this.mode) {
82 case 14:
83 if (this.wrap == 0) {
84 this.mode = 7;
85 break;
86 }try {
87 r = this.readBytes (2, r, f);
88 } catch (e) {
89 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
90 return e.r;
91 } else {
92 throw e;
93 }
94 }
95 if ((this.wrap & 2) != 0 && this.need == 0x8b1f) {
96 this.z.checksum =  new JU.CRC32 ();
97 this.checksum (2, this.need);
98 if (this.gheader == null) this.gheader =  new JU.GZIPHeader ();
99 this.mode = 23;
100 break;
101 }this.flags = 0;
102 this.method = (this.need) & 0xff;
103 b = ((this.need >> 8)) & 0xff;
104 if ((this.wrap & 1) == 0 || (((this.method << 8) + b) % 31) != 0) {
105 this.mode = 13;
106 this.z.msg = "incorrect header check";
107 break;
108 }if ((this.method & 0xf) != 8) {
109 this.mode = 13;
110 this.z.msg = "unknown compression method";
111 break;
112 }if ((this.method >> 4) + 8 > this.wbits) {
113 this.mode = 13;
114 this.z.msg = "invalid window size";
115 break;
116 }this.z.checksum =  new JU.Adler32 ();
117 if ((b & 32) == 0) {
118 this.mode = 7;
119 break;
120 }this.mode = 2;
121 case 2:
122 if (this.z.avail_in == 0) return r;
123 r = f;
124 this.z.avail_in--;
125 this.z.total_in++;
126 this.need = ((this.z.next_in[this.z.next_in_index++] & 0xff) << 24) & 0xff000000;
127 this.mode = 3;
128 case 3:
129 if (this.z.avail_in == 0) return r;
130 r = f;
131 this.z.avail_in--;
132 this.z.total_in++;
133 this.need += ((this.z.next_in[this.z.next_in_index++] & 0xff) << 16) & 0xff0000;
134 this.mode = 4;
135 case 4:
136 if (this.z.avail_in == 0) return r;
137 r = f;
138 this.z.avail_in--;
139 this.z.total_in++;
140 this.need += ((this.z.next_in[this.z.next_in_index++] & 0xff) << 8) & 0xff00;
141 this.mode = 5;
142 case 5:
143 if (this.z.avail_in == 0) return r;
144 r = f;
145 this.z.avail_in--;
146 this.z.total_in++;
147 this.need += (this.z.next_in[this.z.next_in_index++] & 0xff);
148 this.z.checksum.resetLong (this.need);
149 this.mode = 6;
150 return 2;
151 case 6:
152 this.mode = 13;
153 this.z.msg = "need dictionary";
154 this.marker = 0;
155 return -2;
156 case 7:
157 r = this.blocks.proc (r);
158 if (r == -3) {
159 this.mode = 13;
160 this.marker = 0;
161 break;
162 }if (r == 0) {
163 r = f;
164 }if (r != 1) {
165 return r;
166 }r = f;
167 this.was = this.z.checksum.getValue ();
168 this.blocks.reset ();
169 if (this.wrap == 0) {
170 this.mode = 12;
171 break;
172 }this.mode = 8;
173 case 8:
174 if (this.z.avail_in == 0) return r;
175 r = f;
176 this.z.avail_in--;
177 this.z.total_in++;
178 this.need = ((this.z.next_in[this.z.next_in_index++] & 0xff) << 24) & 0xff000000;
179 this.mode = 9;
180 case 9:
181 if (this.z.avail_in == 0) return r;
182 r = f;
183 this.z.avail_in--;
184 this.z.total_in++;
185 this.need += ((this.z.next_in[this.z.next_in_index++] & 0xff) << 16) & 0xff0000;
186 this.mode = 10;
187 case 10:
188 if (this.z.avail_in == 0) return r;
189 r = f;
190 this.z.avail_in--;
191 this.z.total_in++;
192 this.need += ((this.z.next_in[this.z.next_in_index++] & 0xff) << 8) & 0xff00;
193 this.mode = 11;
194 case 11:
195 if (this.z.avail_in == 0) return r;
196 r = f;
197 this.z.avail_in--;
198 this.z.total_in++;
199 this.need += (this.z.next_in[this.z.next_in_index++] & 0xff);
200 if (this.flags != 0) {
201 this.need = ((this.need & 0xff000000) >> 24 | (this.need & 0x00ff0000) >> 8 | (this.need & 0x0000ff00) << 8 | (this.need & 0x0000ffff) << 24) & 0xffffffff;
202 }if (((this.was)) != ((this.need))) {
203 this.z.msg = "incorrect data check";
204 } else if (this.flags != 0 && this.gheader != null) {
205 this.gheader.crc = this.need;
206 }this.mode = 15;
207 case 15:
208 if (this.wrap != 0 && this.flags != 0) {
209 try {
210 r = this.readBytes (4, r, f);
211 } catch (e) {
212 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
213 return e.r;
214 } else {
215 throw e;
216 }
217 }
218 if (this.z.msg != null && this.z.msg.equals ("incorrect data check")) {
219 this.mode = 13;
220 this.marker = 5;
221 break;
222 }if (this.need != (this.z.total_out & 0xffffffff)) {
223 this.z.msg = "incorrect length check";
224 this.mode = 13;
225 break;
226 }this.z.msg = null;
227 } else {
228 if (this.z.msg != null && this.z.msg.equals ("incorrect data check")) {
229 this.mode = 13;
230 this.marker = 5;
231 break;
232 }}this.mode = 12;
233 case 12:
234 return 1;
235 case 13:
236 return -3;
237 case 23:
238 try {
239 r = this.readBytes (2, r, f);
240 } catch (e) {
241 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
242 return e.r;
243 } else {
244 throw e;
245 }
246 }
247 this.flags = (this.need) & 0xffff;
248 if ((this.flags & 0xff) != 8) {
249 this.z.msg = "unknown compression method";
250 this.mode = 13;
251 break;
252 }if ((this.flags & 0xe000) != 0) {
253 this.z.msg = "unknown header flags set";
254 this.mode = 13;
255 break;
256 }if ((this.flags & 0x0200) != 0) {
257 this.checksum (2, this.need);
258 }this.mode = 16;
259 case 16:
260 try {
261 r = this.readBytes (4, r, f);
262 } catch (e) {
263 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
264 return e.r;
265 } else {
266 throw e;
267 }
268 }
269 if (this.gheader != null) this.gheader.time = this.need;
270 if ((this.flags & 0x0200) != 0) {
271 this.checksum (4, this.need);
272 }this.mode = 17;
273 case 17:
274 try {
275 r = this.readBytes (2, r, f);
276 } catch (e) {
277 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
278 return e.r;
279 } else {
280 throw e;
281 }
282 }
283 if (this.gheader != null) {
284 this.gheader.xflags = (this.need) & 0xff;
285 this.gheader.os = ((this.need) >> 8) & 0xff;
286 }if ((this.flags & 0x0200) != 0) {
287 this.checksum (2, this.need);
288 }this.mode = 18;
289 case 18:
290 if ((this.flags & 0x0400) != 0) {
291 try {
292 r = this.readBytes (2, r, f);
293 } catch (e) {
294 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
295 return e.r;
296 } else {
297 throw e;
298 }
299 }
300 if (this.gheader != null) {
301 this.gheader.extra =  Clazz.newByteArray ((this.need) & 0xffff, 0);
302 }if ((this.flags & 0x0200) != 0) {
303 this.checksum (2, this.need);
304 }} else if (this.gheader != null) {
305 this.gheader.extra = null;
306 }this.mode = 19;
307 case 19:
308 if ((this.flags & 0x0400) != 0) {
309 try {
310 r = this.readBytes (r, f);
311 if (this.gheader != null) {
312 var foo = this.tmp_string.toByteArray ();
313 this.tmp_string = null;
314 if (foo.length == this.gheader.extra.length) {
315 System.arraycopy (foo, 0, this.gheader.extra, 0, foo.length);
316 } else {
317 this.z.msg = "bad extra field length";
318 this.mode = 13;
319 break;
320 }}} catch (e) {
321 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
322 return e.r;
323 } else {
324 throw e;
325 }
326 }
327 } else if (this.gheader != null) {
328 this.gheader.extra = null;
329 }this.mode = 20;
330 case 20:
331 if ((this.flags & 0x0800) != 0) {
332 try {
333 r = this.readString (r, f);
334 if (this.gheader != null) {
335 this.gheader.name = this.tmp_string.toByteArray ();
336 }this.tmp_string = null;
337 } catch (e) {
338 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
339 return e.r;
340 } else {
341 throw e;
342 }
343 }
344 } else if (this.gheader != null) {
345 this.gheader.name = null;
346 }this.mode = 21;
347 case 21:
348 if ((this.flags & 0x1000) != 0) {
349 try {
350 r = this.readString (r, f);
351 if (this.gheader != null) {
352 this.gheader.comment = this.tmp_string.toByteArray ();
353 }this.tmp_string = null;
354 } catch (e) {
355 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
356 return e.r;
357 } else {
358 throw e;
359 }
360 }
361 } else if (this.gheader != null) {
362 this.gheader.comment = null;
363 }this.mode = 22;
364 case 22:
365 if ((this.flags & 0x0200) != 0) {
366 try {
367 r = this.readBytes (2, r, f);
368 } catch (e) {
369 if (Clazz.exceptionOf (e, JU.Inflate.Return)) {
370 return e.r;
371 } else {
372 throw e;
373 }
374 }
375 if (this.gheader != null) {
376 this.gheader.hcrc = (this.need & 0xffff);
377 }if (this.need != (this.z.checksum.getValue () & 0xffff)) {
378 this.mode = 13;
379 this.z.msg = "header crc mismatch";
380 this.marker = 5;
381 break;
382 }}this.z.checksum =  new JU.CRC32 ();
383 this.mode = 7;
384 break;
385 default:
386 return -2;
387 }
388 }
389 }, "~N");
390 Clazz.defineMethod (c$, "inflateSetDictionary", 
391 function (dictionary, dictLength) {
392 if (this.z == null || (this.mode != 6 && this.wrap != 0)) {
393 return -2;
394 }var index = 0;
395 var length = dictLength;
396 if (this.mode == 6) {
397 var adler_need = this.z.checksum.getValue ();
398 this.z.checksum.reset ();
399 this.z.checksum.update (dictionary, 0, dictLength);
400 if (this.z.checksum.getValue () != adler_need) {
401 return -3;
402 }}this.z.checksum.reset ();
403 if (length >= (1 << this.wbits)) {
404 length = (1 << this.wbits) - 1;
405 index = dictLength - length;
406 }this.blocks.set_dictionary (dictionary, index, length);
407 this.mode = 7;
408 return 0;
409 }, "~A,~N");
410 Clazz.defineMethod (c$, "inflateSync", 
411 function () {
412 var n;
413 var p;
414 var m;
415 var r;
416 var w;
417 if (this.z == null) return -2;
418 if (this.mode != 13) {
419 this.mode = 13;
420 this.marker = 0;
421 }if ((n = this.z.avail_in) == 0) return -5;
422 p = this.z.next_in_index;
423 m = this.marker;
424 while (n != 0 && m < 4) {
425 if (this.z.next_in[p] == JU.Inflate.mark[m]) {
426 m++;
427 } else if (this.z.next_in[p] != 0) {
428 m = 0;
429 } else {
430 m = 4 - m;
431 }p++;
432 n--;
433 }
434 this.z.total_in += p - this.z.next_in_index;
435 this.z.next_in_index = p;
436 this.z.avail_in = n;
437 this.marker = m;
438 if (m != 4) {
439 return -3;
440 }r = this.z.total_in;
441 w = this.z.total_out;
442 this.inflateReset ();
443 this.z.total_in = r;
444 this.z.total_out = w;
445 this.mode = 7;
446 return 0;
447 });
448 Clazz.defineMethod (c$, "inflateSyncPoint", 
449 function () {
450 if (this.z == null || this.blocks == null) return -2;
451 return this.blocks.sync_point ();
452 });
453 Clazz.defineMethod (c$, "readBytes", 
454  function (n, r, f) {
455 if (this.need_bytes == -1) {
456 this.need_bytes = n;
457 this.need = 0;
458 }while (this.need_bytes > 0) {
459 if (this.z.avail_in == 0) {
460 throw Clazz.innerTypeInstance (JU.Inflate.Return, this, null, r);
461 }r = f;
462 this.z.avail_in--;
463 this.z.total_in++;
464 this.need = this.need | ((this.z.next_in[this.z.next_in_index++] & 0xff) << ((n - this.need_bytes) * 8));
465 this.need_bytes--;
466 }
467 if (n == 2) {
468 this.need &= 0xffff;
469 } else if (n == 4) {
470 this.need &= 0xffffffff;
471 }this.need_bytes = -1;
472 return r;
473 }, "~N,~N,~N");
474 Clazz.defineMethod (c$, "readString", 
475  function (r, f) {
476 if (this.tmp_string == null) {
477 this.tmp_string =  new java.io.ByteArrayOutputStream ();
478 }var b = 0;
479 do {
480 if (this.z.avail_in == 0) {
481 throw Clazz.innerTypeInstance (JU.Inflate.Return, this, null, r);
482 }r = f;
483 this.z.avail_in--;
484 this.z.total_in++;
485 b = this.z.next_in[this.z.next_in_index];
486 if (b != 0) this.tmp_string.write (this.z.next_in, this.z.next_in_index, 1);
487 this.z.checksum.update (this.z.next_in, this.z.next_in_index, 1);
488 this.z.next_in_index++;
489 } while (b != 0);
490 return r;
491 }, "~N,~N");
492 Clazz.defineMethod (c$, "readBytes", 
493  function (r, f) {
494 if (this.tmp_string == null) {
495 this.tmp_string =  new java.io.ByteArrayOutputStream ();
496 }while (this.need > 0) {
497 if (this.z.avail_in == 0) {
498 throw Clazz.innerTypeInstance (JU.Inflate.Return, this, null, r);
499 }r = f;
500 this.z.avail_in--;
501 this.z.total_in++;
502 this.tmp_string.write (this.z.next_in, this.z.next_in_index, 1);
503 this.z.checksum.update (this.z.next_in, this.z.next_in_index, 1);
504 this.z.next_in_index++;
505 this.need--;
506 }
507 return r;
508 }, "~N,~N");
509 Clazz.defineMethod (c$, "checksum", 
510  function (n, v) {
511 for (var i = 0; i < n; i++) {
512 this.crcbuf[i] = (v & 0xff);
513 v >>= 8;
514 }
515 this.z.checksum.update (this.crcbuf, 0, n);
516 }, "~N,~N");
517 Clazz.defineMethod (c$, "getGZIPHeader", 
518 function () {
519 return this.gheader;
520 });
521 Clazz.defineMethod (c$, "inParsingHeader", 
522 function () {
523 switch (this.mode) {
524 case 14:
525 case 2:
526 case 3:
527 case 4:
528 case 5:
529 case 23:
530 case 16:
531 case 17:
532 case 18:
533 case 19:
534 case 20:
535 case 21:
536 case 22:
537 return true;
538 default:
539 return false;
540 }
541 });
542 c$.$Inflate$Return$ = function () {
543 Clazz.pu$h(self.c$);
544 c$ = Clazz.decorateAsClass (function () {
545 Clazz.prepareCallback (this, arguments);
546 this.r = 0;
547 Clazz.instantialize (this, arguments);
548 }, JU.Inflate, "Return", Exception);
549 Clazz.makeConstructor (c$, 
550 function (a) {
551 Clazz.superConstructor (this, JU.Inflate.Return, []);
552 this.r = a;
553 }, "~N");
554 c$ = Clazz.p0p ();
555 };
556 Clazz.defineStatics (c$,
557 "PRESET_DICT", 0x20,
558 "Z_NO_FLUSH", 0,
559 "Z_PARTIAL_FLUSH", 1,
560 "Z_SYNC_FLUSH", 2,
561 "Z_FULL_FLUSH", 3,
562 "Z_FINISH", 4,
563 "Z_DEFLATED", 8,
564 "Z_OK", 0,
565 "Z_STREAM_END", 1,
566 "Z_NEED_DICT", 2,
567 "Z_STREAM_ERROR", -2,
568 "Z_DATA_ERROR", -3,
569 "Z_BUF_ERROR", -5,
570 "DICT4", 2,
571 "DICT3", 3,
572 "DICT2", 4,
573 "DICT1", 5,
574 "DICT0", 6,
575 "BLOCKS", 7,
576 "CHECK4", 8,
577 "CHECK3", 9,
578 "CHECK2", 10,
579 "CHECK1", 11,
580 "DONE", 12,
581 "BAD", 13,
582 "HEAD", 14,
583 "LENGTH", 15,
584 "TIME", 16,
585 "OS", 17,
586 "EXLEN", 18,
587 "EXTRA", 19,
588 "NAME", 20,
589 "COMMENT", 21,
590 "HCRC", 22,
591 "FLAGS", 23,
592 "mark",  Clazz.newByteArray (-1, [0, 0, 0xff, 0xff]));
593 });