Inflate.cs 15 KB

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