/plugins/FTP/trunk/com/jcraft/jzlib/Inflate.java

# · Java · 374 lines · 260 code · 61 blank · 53 comment · 67 complexity · fab2cda8949e3c92317e2bfc80feb698 MD5 · raw file

  1. /* -*-mode:java; c-basic-offset:2; -*- */
  2. /*
  3. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the distribution.
  11. 3. The names of the authors may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  16. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /*
  25. * This program is based on zlib-1.1.3, so all credit should go authors
  26. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  27. * and contributors of zlib.
  28. */
  29. package com.jcraft.jzlib;
  30. final class Inflate{
  31. static final private int MAX_WBITS=15; // 32K LZ77 window
  32. // preset dictionary flag in zlib header
  33. static final private int PRESET_DICT=0x20;
  34. static final int Z_NO_FLUSH=0;
  35. static final int Z_PARTIAL_FLUSH=1;
  36. static final int Z_SYNC_FLUSH=2;
  37. static final int Z_FULL_FLUSH=3;
  38. static final int Z_FINISH=4;
  39. static final private int Z_DEFLATED=8;
  40. static final private int Z_OK=0;
  41. static final private int Z_STREAM_END=1;
  42. static final private int Z_NEED_DICT=2;
  43. static final private int Z_ERRNO=-1;
  44. static final private int Z_STREAM_ERROR=-2;
  45. static final private int Z_DATA_ERROR=-3;
  46. static final private int Z_MEM_ERROR=-4;
  47. static final private int Z_BUF_ERROR=-5;
  48. static final private int Z_VERSION_ERROR=-6;
  49. static final private int METHOD=0; // waiting for method byte
  50. static final private int FLAG=1; // waiting for flag byte
  51. static final private int DICT4=2; // four dictionary check bytes to go
  52. static final private int DICT3=3; // three dictionary check bytes to go
  53. static final private int DICT2=4; // two dictionary check bytes to go
  54. static final private int DICT1=5; // one dictionary check byte to go
  55. static final private int DICT0=6; // waiting for inflateSetDictionary
  56. static final private int BLOCKS=7; // decompressing blocks
  57. static final private int CHECK4=8; // four check bytes to go
  58. static final private int CHECK3=9; // three check bytes to go
  59. static final private int CHECK2=10; // two check bytes to go
  60. static final private int CHECK1=11; // one check byte to go
  61. static final private int DONE=12; // finished check, done
  62. static final private int BAD=13; // got an error--stay here
  63. int mode; // current inflate mode
  64. // mode dependent information
  65. int method; // if FLAGS, method byte
  66. // if CHECK, check values to compare
  67. long[] was=new long[1] ; // computed check value
  68. long need; // stream check value
  69. // if BAD, inflateSync's marker bytes count
  70. int marker;
  71. // mode independent information
  72. int nowrap; // flag for no wrapper
  73. int wbits; // log2(window size) (8..15, defaults to 15)
  74. InfBlocks blocks; // current inflate_blocks state
  75. int inflateReset(ZStream z){
  76. if(z == null || z.istate == null) return Z_STREAM_ERROR;
  77. z.total_in = z.total_out = 0;
  78. z.msg = null;
  79. z.istate.mode = z.istate.nowrap!=0 ? BLOCKS : METHOD;
  80. z.istate.blocks.reset(z, null);
  81. return Z_OK;
  82. }
  83. int inflateEnd(ZStream z){
  84. if(blocks != null)
  85. blocks.free(z);
  86. blocks=null;
  87. // ZFREE(z, z->state);
  88. return Z_OK;
  89. }
  90. int inflateInit(ZStream z, int w){
  91. z.msg = null;
  92. blocks = null;
  93. // handle undocumented nowrap option (no zlib header or check)
  94. nowrap = 0;
  95. if(w < 0){
  96. w = - w;
  97. nowrap = 1;
  98. }
  99. // set window size
  100. if(w<8 ||w>15){
  101. inflateEnd(z);
  102. return Z_STREAM_ERROR;
  103. }
  104. wbits=w;
  105. z.istate.blocks=new InfBlocks(z,
  106. z.istate.nowrap!=0 ? null : this,
  107. 1<<w);
  108. // reset state
  109. inflateReset(z);
  110. return Z_OK;
  111. }
  112. int inflate(ZStream z, int f){
  113. int r;
  114. int b;
  115. if(z == null || z.istate == null || z.next_in == null)
  116. return Z_STREAM_ERROR;
  117. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  118. r = Z_BUF_ERROR;
  119. while (true){
  120. //System.out.println("mode: "+z.istate.mode);
  121. switch (z.istate.mode){
  122. case METHOD:
  123. if(z.avail_in==0)return r;r=f;
  124. z.avail_in--; z.total_in++;
  125. if(((z.istate.method = z.next_in[z.next_in_index++])&0xf)!=Z_DEFLATED){
  126. z.istate.mode = BAD;
  127. z.msg="unknown compression method";
  128. z.istate.marker = 5; // can't try inflateSync
  129. break;
  130. }
  131. if((z.istate.method>>4)+8>z.istate.wbits){
  132. z.istate.mode = BAD;
  133. z.msg="invalid window size";
  134. z.istate.marker = 5; // can't try inflateSync
  135. break;
  136. }
  137. z.istate.mode=FLAG;
  138. case FLAG:
  139. if(z.avail_in==0)return r;r=f;
  140. z.avail_in--; z.total_in++;
  141. b = (z.next_in[z.next_in_index++])&0xff;
  142. if((((z.istate.method << 8)+b) % 31)!=0){
  143. z.istate.mode = BAD;
  144. z.msg = "incorrect header check";
  145. z.istate.marker = 5; // can't try inflateSync
  146. break;
  147. }
  148. if((b&PRESET_DICT)==0){
  149. z.istate.mode = BLOCKS;
  150. break;
  151. }
  152. z.istate.mode = DICT4;
  153. case DICT4:
  154. if(z.avail_in==0)return r;r=f;
  155. z.avail_in--; z.total_in++;
  156. z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
  157. z.istate.mode=DICT3;
  158. case DICT3:
  159. if(z.avail_in==0)return r;r=f;
  160. z.avail_in--; z.total_in++;
  161. z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
  162. z.istate.mode=DICT2;
  163. case DICT2:
  164. if(z.avail_in==0)return r;r=f;
  165. z.avail_in--; z.total_in++;
  166. z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
  167. z.istate.mode=DICT1;
  168. case DICT1:
  169. if(z.avail_in==0)return r;r=f;
  170. z.avail_in--; z.total_in++;
  171. z.istate.need += (z.next_in[z.next_in_index++]&0xffL);
  172. z.adler = z.istate.need;
  173. z.istate.mode = DICT0;
  174. return Z_NEED_DICT;
  175. case DICT0:
  176. z.istate.mode = BAD;
  177. z.msg = "need dictionary";
  178. z.istate.marker = 0; // can try inflateSync
  179. return Z_STREAM_ERROR;
  180. case BLOCKS:
  181. r = z.istate.blocks.proc(z, r);
  182. if(r == Z_DATA_ERROR){
  183. z.istate.mode = BAD;
  184. z.istate.marker = 0; // can try inflateSync
  185. break;
  186. }
  187. if(r == Z_OK){
  188. r = f;
  189. }
  190. if(r != Z_STREAM_END){
  191. return r;
  192. }
  193. r = f;
  194. z.istate.blocks.reset(z, z.istate.was);
  195. if(z.istate.nowrap!=0){
  196. z.istate.mode=DONE;
  197. break;
  198. }
  199. z.istate.mode=CHECK4;
  200. case CHECK4:
  201. if(z.avail_in==0)return r;r=f;
  202. z.avail_in--; z.total_in++;
  203. z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
  204. z.istate.mode=CHECK3;
  205. case CHECK3:
  206. if(z.avail_in==0)return r;r=f;
  207. z.avail_in--; z.total_in++;
  208. z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
  209. z.istate.mode = CHECK2;
  210. case CHECK2:
  211. if(z.avail_in==0)return r;r=f;
  212. z.avail_in--; z.total_in++;
  213. z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
  214. z.istate.mode = CHECK1;
  215. case CHECK1:
  216. if(z.avail_in==0)return r;r=f;
  217. z.avail_in--; z.total_in++;
  218. z.istate.need+=(z.next_in[z.next_in_index++]&0xffL);
  219. if(((int)(z.istate.was[0])) != ((int)(z.istate.need))){
  220. z.istate.mode = BAD;
  221. z.msg = "incorrect data check";
  222. z.istate.marker = 5; // can't try inflateSync
  223. break;
  224. }
  225. z.istate.mode = DONE;
  226. case DONE:
  227. return Z_STREAM_END;
  228. case BAD:
  229. return Z_DATA_ERROR;
  230. default:
  231. return Z_STREAM_ERROR;
  232. }
  233. }
  234. }
  235. int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength){
  236. int index=0;
  237. int length = dictLength;
  238. if(z==null || z.istate == null|| z.istate.mode != DICT0)
  239. return Z_STREAM_ERROR;
  240. if(z._adler.adler32(1L, dictionary, 0, dictLength)!=z.adler){
  241. return Z_DATA_ERROR;
  242. }
  243. z.adler = z._adler.adler32(0, null, 0, 0);
  244. if(length >= (1<<z.istate.wbits)){
  245. length = (1<<z.istate.wbits)-1;
  246. index=dictLength - length;
  247. }
  248. z.istate.blocks.set_dictionary(dictionary, index, length);
  249. z.istate.mode = BLOCKS;
  250. return Z_OK;
  251. }
  252. static private byte[] mark = {(byte)0, (byte)0, (byte)0xff, (byte)0xff};
  253. int inflateSync(ZStream z){
  254. int n; // number of bytes to look at
  255. int p; // pointer to bytes
  256. int m; // number of marker bytes found in a row
  257. long r, w; // temporaries to save total_in and total_out
  258. // set up
  259. if(z == null || z.istate == null)
  260. return Z_STREAM_ERROR;
  261. if(z.istate.mode != BAD){
  262. z.istate.mode = BAD;
  263. z.istate.marker = 0;
  264. }
  265. if((n=z.avail_in)==0)
  266. return Z_BUF_ERROR;
  267. p=z.next_in_index;
  268. m=z.istate.marker;
  269. // search
  270. while (n!=0 && m < 4){
  271. if(z.next_in[p] == mark[m]){
  272. m++;
  273. }
  274. else if(z.next_in[p]!=0){
  275. m = 0;
  276. }
  277. else{
  278. m = 4 - m;
  279. }
  280. p++; n--;
  281. }
  282. // restore
  283. z.total_in += p-z.next_in_index;
  284. z.next_in_index = p;
  285. z.avail_in = n;
  286. z.istate.marker = m;
  287. // return no joy or set up to restart on a new block
  288. if(m != 4){
  289. return Z_DATA_ERROR;
  290. }
  291. r=z.total_in; w=z.total_out;
  292. inflateReset(z);
  293. z.total_in=r; z.total_out = w;
  294. z.istate.mode = BLOCKS;
  295. return Z_OK;
  296. }
  297. // Returns true if inflate is currently at the end of a block generated
  298. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  299. // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  300. // but removes the length bytes of the resulting empty stored block. When
  301. // decompressing, PPP checks that at the end of input packet, inflate is
  302. // waiting for these length bytes.
  303. int inflateSyncPoint(ZStream z){
  304. if(z == null || z.istate == null || z.istate.blocks == null)
  305. return Z_STREAM_ERROR;
  306. return z.istate.blocks.sync_point();
  307. }
  308. }