Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
1 /* -*-mode:java; c-basic-offset:2; -*- */
3 Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This program is based on zlib-1.1.3, so all credit should go authors
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
32 * and contributors of zlib.
35 package org.apidesign.bck2brwsr.emul.zip;
37 import org.apidesign.bck2brwsr.emul.lang.System;
41 static final private int MAX_WBITS=15; // 32K LZ77 window
43 // preset dictionary flag in zlib header
44 static final private int PRESET_DICT=0x20;
46 static final int Z_NO_FLUSH=0;
47 static final int Z_PARTIAL_FLUSH=1;
48 static final int Z_SYNC_FLUSH=2;
49 static final int Z_FULL_FLUSH=3;
50 static final int Z_FINISH=4;
52 static final private int Z_DEFLATED=8;
54 static final private int Z_OK=0;
55 static final private int Z_STREAM_END=1;
56 static final private int Z_NEED_DICT=2;
57 static final private int Z_ERRNO=-1;
58 static final private int Z_STREAM_ERROR=-2;
59 static final private int Z_DATA_ERROR=-3;
60 static final private int Z_MEM_ERROR=-4;
61 static final private int Z_BUF_ERROR=-5;
62 static final private int Z_VERSION_ERROR=-6;
64 static final private int METHOD=0; // waiting for method byte
65 static final private int FLAG=1; // waiting for flag byte
66 static final private int DICT4=2; // four dictionary check bytes to go
67 static final private int DICT3=3; // three dictionary check bytes to go
68 static final private int DICT2=4; // two dictionary check bytes to go
69 static final private int DICT1=5; // one dictionary check byte to go
70 static final int DICT0=6; // waiting for inflateSetDictionary
71 static final private int BLOCKS=7; // decompressing blocks
72 static final private int CHECK4=8; // four check bytes to go
73 static final private int CHECK3=9; // three check bytes to go
74 static final private int CHECK2=10; // two check bytes to go
75 static final private int CHECK1=11; // one check byte to go
76 static final private int DONE=12; // finished check, done
77 static final private int BAD=13; // got an error--stay here
79 static final private int HEAD=14;
80 static final private int LENGTH=15;
81 static final private int TIME=16;
82 static final private int OS=17;
83 static final private int EXLEN=18;
84 static final private int EXTRA=19;
85 static final private int NAME=20;
86 static final private int COMMENT=21;
87 static final private int HCRC=22;
88 static final private int FLAGS=23;
90 int mode; // current inflate mode
92 // mode dependent information
93 int method; // if FLAGS, method byte
95 // if CHECK, check values to compare
96 long was = -1; // computed check value
97 long need; // stream check value
99 // if BAD, inflateSync's marker bytes count
102 // mode independent information
103 int wrap; // flag for no wrapper
104 int wbits; // log2(window size) (8..15, defaults to 15)
106 InfBlocks blocks; // current inflate_blocks state
108 private final ZStream z;
112 private int need_bytes = -1;
113 private byte[] crcbuf=new byte[4];
115 GZIPHeader gheader = null;
118 if(z == null) return Z_STREAM_ERROR;
120 z.total_in = z.total_out = 0;
123 this.need_bytes = -1;
139 int inflateInit(int w){
143 // handle undocumented wrap option (no zlib header or check)
156 return Z_STREAM_ERROR;
158 if(blocks != null && wbits != w){
166 this.blocks=new InfBlocks(z, 1<<w);
180 if(z == null || z.next_in == null){
181 if(f == Z_FINISH && this.mode==HEAD)
183 return Z_STREAM_ERROR;
186 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
197 try { r=readBytes(2, r, f); }
198 catch(Return e){ return e.r; }
200 if((wrap&2)!=0 && this.need == 0x8b1fL) { // gzip header
202 checksum(2, this.need);
205 gheader=new GZIPHeader();
213 this.method = ((int)this.need)&0xff;
214 b=((int)(this.need>>8))&0xff;
216 if((wrap&1)==0 || // check if zlib header allowed
217 (((this.method << 8)+b) % 31)!=0){
219 z.msg = "incorrect header check";
220 // since zlib 1.2, it is allowted to inflateSync for this case.
222 this.marker = 5; // can't try inflateSync
227 if((this.method&0xf)!=Z_DEFLATED){
229 z.msg="unknown compression method";
230 // since zlib 1.2, it is allowted to inflateSync for this case.
232 this.marker = 5; // can't try inflateSync
237 if((this.method>>4)+8>this.wbits){
239 z.msg="invalid window size";
240 // since zlib 1.2, it is allowted to inflateSync for this case.
242 this.marker = 5; // can't try inflateSync
247 z.adler=new Adler32();
249 if((b&PRESET_DICT)==0){
256 if(z.avail_in==0)return r;r=f;
258 z.avail_in--; z.total_in++;
259 this.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
263 if(z.avail_in==0)return r;r=f;
265 z.avail_in--; z.total_in++;
266 this.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
270 if(z.avail_in==0)return r;r=f;
272 z.avail_in--; z.total_in++;
273 this.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
277 if(z.avail_in==0)return r;r=f;
279 z.avail_in--; z.total_in++;
280 this.need += (z.next_in[z.next_in_index++]&0xffL);
281 z.adler.reset(this.need);
286 z.msg = "need dictionary";
287 this.marker = 0; // can try inflateSync
288 return Z_STREAM_ERROR;
290 r = this.blocks.proc(r);
291 if(r == Z_DATA_ERROR){
293 this.marker = 0; // can try inflateSync
299 if(r != Z_STREAM_END){
303 this.was=z.adler.getValue();
312 if(z.avail_in==0)return r;r=f;
314 z.avail_in--; z.total_in++;
315 this.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
319 if(z.avail_in==0)return r;r=f;
321 z.avail_in--; z.total_in++;
322 this.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
326 if(z.avail_in==0)return r;r=f;
328 z.avail_in--; z.total_in++;
329 this.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
333 if(z.avail_in==0)return r;r=f;
335 z.avail_in--; z.total_in++;
336 this.need+=(z.next_in[z.next_in_index++]&0xffL);
338 if(flags!=0){ // gzip
339 this.need = ((this.need&0xff000000)>>24 |
340 (this.need&0x00ff0000)>>8 |
341 (this.need&0x0000ff00)<<8 |
342 (this.need&0x0000ffff)<<24)&0xffffffffL;
345 if(((int)(this.was)) != ((int)(this.need))){
346 z.msg = "incorrect data check";
350 this.marker = 5; // can't try inflateSync
354 else if(flags!=0 && gheader!=null){
355 gheader.crc = this.need;
360 if (wrap!=0 && flags!=0) {
362 try { r=readBytes(4, r, f); }
363 catch(Return e){ return e.r; }
365 if(z.msg!=null && z.msg.equals("incorrect data check")){
367 this.marker = 5; // can't try inflateSync
371 if (this.need != (z.total_out & 0xffffffffL)) {
372 z.msg = "incorrect length check";
379 if(z.msg!=null && z.msg.equals("incorrect data check")){
381 this.marker = 5; // can't try inflateSync
394 try { r=readBytes(2, r, f); }
395 catch(Return e){ return e.r; }
397 flags = ((int)this.need)&0xffff;
399 if ((flags & 0xff) != Z_DEFLATED) {
400 z.msg = "unknown compression method";
404 if ((flags & 0xe000)!=0) {
405 z.msg = "unknown header flags set";
410 if ((flags & 0x0200)!=0){
411 checksum(2, this.need);
417 try { r=readBytes(4, r, f); }
418 catch(Return e){ return e.r; }
420 gheader.time = this.need;
421 if ((flags & 0x0200)!=0){
422 checksum(4, this.need);
426 try { r=readBytes(2, r, f); }
427 catch(Return e){ return e.r; }
429 gheader.xflags = ((int)this.need)&0xff;
430 gheader.os = (((int)this.need)>>8)&0xff;
432 if ((flags & 0x0200)!=0){
433 checksum(2, this.need);
437 if ((flags & 0x0400)!=0) {
438 try { r=readBytes(2, r, f); }
439 catch(Return e){ return e.r; }
441 gheader.extra = new byte[((int)this.need)&0xffff];
443 if ((flags & 0x0200)!=0){
444 checksum(2, this.need);
447 else if(gheader!=null){
453 if ((flags & 0x0400)!=0) {
457 byte[] foo = tmp_array;
459 if(foo.length == gheader.extra.length){
460 System.arraycopy(foo, 0, gheader.extra, 0, foo.length);
463 z.msg = "bad extra field length";
469 catch(Return e){ return e.r; }
471 else if(gheader!=null){
476 if ((flags & 0x0800)!=0) {
480 gheader.name=tmp_array;
484 catch(Return e){ return e.r; }
486 else if(gheader!=null){
491 if ((flags & 0x1000)!=0) {
495 gheader.comment=tmp_array;
499 catch(Return e){ return e.r; }
501 else if(gheader!=null){
502 gheader.comment=null;
506 if ((flags & 0x0200)!=0) {
507 try { r=readBytes(2, r, f); }
508 catch(Return e){ return e.r; }
510 gheader.hcrc=(int)(this.need&0xffff);
512 if(this.need != (z.adler.getValue()&0xffffL)){
514 z.msg = "header crc mismatch";
515 this.marker = 5; // can't try inflateSync
519 z.adler = new CRC32();
524 return Z_STREAM_ERROR;
529 int inflateSetDictionary(byte[] dictionary, int dictLength){
530 if(z==null || (this.mode != DICT0 && this.wrap != 0)){
531 return Z_STREAM_ERROR;
535 int length = dictLength;
537 if(this.mode==DICT0){
538 long adler_need=z.adler.getValue();
540 z.adler.update(dictionary, 0, dictLength);
541 if(z.adler.getValue()!=adler_need){
548 if(length >= (1<<this.wbits)){
549 length = (1<<this.wbits)-1;
550 index=dictLength - length;
552 this.blocks.set_dictionary(dictionary, index, length);
557 static private byte[] mark = {(byte)0, (byte)0, (byte)0xff, (byte)0xff};
560 int n; // number of bytes to look at
561 int p; // pointer to bytes
562 int m; // number of marker bytes found in a row
563 long r, w; // temporaries to save total_in and total_out
567 return Z_STREAM_ERROR;
568 if(this.mode != BAD){
572 if((n=z.avail_in)==0)
578 while (n!=0 && m < 4){
579 if(z.next_in[p] == mark[m]){
582 else if(z.next_in[p]!=0){
592 z.total_in += p-z.next_in_index;
597 // return no joy or set up to restart on a new block
601 r=z.total_in; w=z.total_out;
603 z.total_in=r; z.total_out = w;
609 // Returns true if inflate is currently at the end of a block generated
610 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
611 // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
612 // but removes the length bytes of the resulting empty stored block. When
613 // decompressing, PPP checks that at the end of input packet, inflate is
614 // waiting for these length bytes.
615 int inflateSyncPoint(){
616 if(z == null || this.blocks == null)
617 return Z_STREAM_ERROR;
618 return this.blocks.sync_point();
621 private int readBytes(int n, int r, int f) throws Return{
622 if(need_bytes == -1){
627 if(z.avail_in==0){ throw new Return(r); }; r=f;
628 z.avail_in--; z.total_in++;
629 this.need = this.need |
630 ((z.next_in[z.next_in_index++]&0xff)<<((n-need_bytes)*8));
637 this.need&=0xffffffffL;
642 class Return extends Exception{
644 Return(int r){this.r=r; }
647 private byte[] tmp_array;
648 private int readString(int r, int f) throws Return{
650 byte[] arr = new byte[4092];
653 if(z.avail_in==0){ throw new Return(r); }; r=f;
654 z.avail_in--; z.total_in++;
655 b = z.next_in[z.next_in_index];
656 if(b!=0) arr = append(arr, z.next_in[z.next_in_index], at++);
657 z.adler.update(z.next_in, z.next_in_index, 1);
661 tmp_array = copy(arr, at);
666 private int readBytes(int r, int f) throws Return{
668 byte[] arr = new byte[4092];
671 if(z.avail_in==0){ throw new Return(r); }; r=f;
672 z.avail_in--; z.total_in++;
673 b = z.next_in[z.next_in_index];
674 arr = append(arr, z.next_in[z.next_in_index], at++);
675 z.adler.update(z.next_in, z.next_in_index, 1);
680 tmp_array = copy(arr, at);
685 private static byte[] copy(byte[] arr, int len) {
686 byte[] ret = new byte[len];
687 org.apidesign.bck2brwsr.emul.lang.System.arraycopy(arr, 0, ret, 0, len);
690 private static byte[] append(byte[] arr, byte b, int index) {
695 private void checksum(int n, long v){
696 for(int i=0; i<n; i++){
697 crcbuf[i]=(byte)(v&0xff);
700 z.adler.update(crcbuf, 0, n);
703 public GZIPHeader getGZIPHeader(){
707 boolean inParsingHeader(){