Inflate.cs 15 KB

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