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