Inflate.cs 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. // Inflate.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License.
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17. // last saved (in emacs):
  18. // Time-stamp: <2010-January-08 18:32:12>
  19. //
  20. // ------------------------------------------------------------------
  21. //
  22. // This module defines classes for decompression. This code is derived
  23. // from the jzlib implementation of zlib, but significantly modified.
  24. // The object model is not the same, and many of the behaviors are
  25. // different. Nonetheless, in keeping with the license for jzlib, I am
  26. // reproducing the copyright to that code here.
  27. //
  28. // ------------------------------------------------------------------
  29. //
  30. // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  31. //
  32. // Redistribution and use in source and binary forms, with or without
  33. // modification, are permitted provided that the following conditions are met:
  34. //
  35. // 1. Redistributions of source code must retain the above copyright notice,
  36. // this list of conditions and the following disclaimer.
  37. //
  38. // 2. Redistributions in binary form must reproduce the above copyright
  39. // notice, this list of conditions and the following disclaimer in
  40. // the documentation and/or other materials provided with the distribution.
  41. //
  42. // 3. The names of the authors may not be used to endorse or promote products
  43. // derived from this software without specific prior written permission.
  44. //
  45. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  46. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  47. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  48. // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  49. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  50. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  51. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  52. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  53. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  54. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. //
  56. // -----------------------------------------------------------------------
  57. //
  58. // This program is based on zlib-1.1.3; credit to authors
  59. // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  60. // and contributors of zlib.
  61. //
  62. // -----------------------------------------------------------------------
  63. using System;
  64. namespace BestHTTP.Decompression.Zlib
  65. {
  66. sealed class InflateBlocks
  67. {
  68. private const int MANY = 1440;
  69. // Table for deflate from PKZIP's appnote.txt.
  70. internal static readonly int[] border = new int[]
  71. { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
  72. private enum InflateBlockMode
  73. {
  74. TYPE = 0, // get type bits (3, including end bit)
  75. LENS = 1, // get lengths for stored
  76. STORED = 2, // processing stored block
  77. TABLE = 3, // get table lengths
  78. BTREE = 4, // get bit lengths tree for a dynamic block
  79. DTREE = 5, // get length, distance trees for a dynamic block
  80. CODES = 6, // processing fixed or dynamic block
  81. DRY = 7, // output remaining window bytes
  82. DONE = 8, // finished last block, done
  83. BAD = 9, // ot a data error--stuck here
  84. }
  85. private InflateBlockMode mode; // current inflate_block mode
  86. internal int left; // if STORED, bytes left to copy
  87. internal int table; // table lengths (14 bits)
  88. internal int index; // index into blens (or border)
  89. internal int[] blens; // bit lengths of codes
  90. internal int[] bb = new int[1]; // bit length tree depth
  91. internal int[] tb = new int[1]; // bit length decoding tree
  92. internal InflateCodes codes = new InflateCodes(); // if CODES, current state
  93. internal int last; // true if this block is the last block
  94. internal ZlibCodec _codec; // pointer back to this zlib stream
  95. // mode independent information
  96. internal int bitk; // bits in bit buffer
  97. internal int bitb; // bit buffer
  98. internal int[] hufts; // single malloc for tree space
  99. internal byte[] window; // sliding window
  100. internal int end; // one byte after sliding window
  101. internal int readAt; // window read pointer
  102. internal int writeAt; // window write pointer
  103. internal System.Object checkfn; // check function
  104. internal uint check; // check on output
  105. internal InfTree inftree = new InfTree();
  106. internal InflateBlocks(ZlibCodec codec, System.Object checkfn, int w)
  107. {
  108. _codec = codec;
  109. hufts = new int[MANY * 3];
  110. window = Extensions.VariableSizedBufferPool.Get(w, true);
  111. end = w;
  112. this.checkfn = checkfn;
  113. mode = InflateBlockMode.TYPE;
  114. Reset();
  115. }
  116. internal uint Reset()
  117. {
  118. uint oldCheck = check;
  119. mode = InflateBlockMode.TYPE;
  120. bitk = 0;
  121. bitb = 0;
  122. readAt = writeAt = 0;
  123. if (checkfn != null)
  124. _codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
  125. return oldCheck;
  126. }
  127. internal int Process(int r)
  128. {
  129. int t; // temporary storage
  130. int b; // bit buffer
  131. int k; // bits in bit buffer
  132. int p; // input data pointer
  133. int n; // bytes available there
  134. int q; // output window write pointer
  135. int m; // bytes to end of window or read pointer
  136. // copy input/output information to locals (UPDATE macro restores)
  137. p = _codec.NextIn;
  138. n = _codec.AvailableBytesIn;
  139. b = bitb;
  140. k = bitk;
  141. q = writeAt;
  142. m = (int)(q < readAt ? readAt - q - 1 : end - q);
  143. // process input based on current state
  144. while (true)
  145. {
  146. switch (mode)
  147. {
  148. case InflateBlockMode.TYPE:
  149. while (k < (3))
  150. {
  151. if (n != 0)
  152. {
  153. r = ZlibConstants.Z_OK;
  154. }
  155. else
  156. {
  157. bitb = b; bitk = k;
  158. _codec.AvailableBytesIn = n;
  159. _codec.TotalBytesIn += p - _codec.NextIn;
  160. _codec.NextIn = p;
  161. writeAt = q;
  162. return Flush(r);
  163. }
  164. n--;
  165. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  166. k += 8;
  167. }
  168. t = (int)(b & 7);
  169. last = t & 1;
  170. switch ((uint)t >> 1)
  171. {
  172. case 0: // stored
  173. b >>= 3; k -= (3);
  174. t = k & 7; // go to byte boundary
  175. b >>= t; k -= t;
  176. mode = InflateBlockMode.LENS; // get length of stored block
  177. break;
  178. case 1: // fixed
  179. int[] bl = new int[1];
  180. int[] bd = new int[1];
  181. int[][] tl = new int[1][];
  182. int[][] td = new int[1][];
  183. InfTree.inflate_trees_fixed(bl, bd, tl, td, _codec);
  184. codes.Init(bl[0], bd[0], tl[0], 0, td[0], 0);
  185. b >>= 3; k -= 3;
  186. mode = InflateBlockMode.CODES;
  187. break;
  188. case 2: // dynamic
  189. b >>= 3; k -= 3;
  190. mode = InflateBlockMode.TABLE;
  191. break;
  192. case 3: // illegal
  193. b >>= 3; k -= 3;
  194. mode = InflateBlockMode.BAD;
  195. _codec.Message = "invalid block type";
  196. r = ZlibConstants.Z_DATA_ERROR;
  197. bitb = b; bitk = k;
  198. _codec.AvailableBytesIn = n;
  199. _codec.TotalBytesIn += p - _codec.NextIn;
  200. _codec.NextIn = p;
  201. writeAt = q;
  202. return Flush(r);
  203. }
  204. break;
  205. case InflateBlockMode.LENS:
  206. while (k < (32))
  207. {
  208. if (n != 0)
  209. {
  210. r = ZlibConstants.Z_OK;
  211. }
  212. else
  213. {
  214. bitb = b; bitk = k;
  215. _codec.AvailableBytesIn = n;
  216. _codec.TotalBytesIn += p - _codec.NextIn;
  217. _codec.NextIn = p;
  218. writeAt = q;
  219. return Flush(r);
  220. }
  221. ;
  222. n--;
  223. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  224. k += 8;
  225. }
  226. if ( ( ((~b)>>16) & 0xffff) != (b & 0xffff))
  227. {
  228. mode = InflateBlockMode.BAD;
  229. _codec.Message = "invalid stored block lengths";
  230. r = ZlibConstants.Z_DATA_ERROR;
  231. bitb = b; bitk = k;
  232. _codec.AvailableBytesIn = n;
  233. _codec.TotalBytesIn += p - _codec.NextIn;
  234. _codec.NextIn = p;
  235. writeAt = q;
  236. return Flush(r);
  237. }
  238. left = (b & 0xffff);
  239. b = k = 0; // dump bits
  240. mode = left != 0 ? InflateBlockMode.STORED : (last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
  241. break;
  242. case InflateBlockMode.STORED:
  243. if (n == 0)
  244. {
  245. bitb = b; bitk = k;
  246. _codec.AvailableBytesIn = n;
  247. _codec.TotalBytesIn += p - _codec.NextIn;
  248. _codec.NextIn = p;
  249. writeAt = q;
  250. return Flush(r);
  251. }
  252. if (m == 0)
  253. {
  254. if (q == end && readAt != 0)
  255. {
  256. q = 0; m = (int)(q < readAt ? readAt - q - 1 : end - q);
  257. }
  258. if (m == 0)
  259. {
  260. writeAt = q;
  261. r = Flush(r);
  262. q = writeAt; m = (int)(q < readAt ? readAt - q - 1 : end - q);
  263. if (q == end && readAt != 0)
  264. {
  265. q = 0; m = (int)(q < readAt ? readAt - q - 1 : end - q);
  266. }
  267. if (m == 0)
  268. {
  269. bitb = b; bitk = k;
  270. _codec.AvailableBytesIn = n;
  271. _codec.TotalBytesIn += p - _codec.NextIn;
  272. _codec.NextIn = p;
  273. writeAt = q;
  274. return Flush(r);
  275. }
  276. }
  277. }
  278. r = ZlibConstants.Z_OK;
  279. t = left;
  280. if (t > n)
  281. t = n;
  282. if (t > m)
  283. t = m;
  284. Array.Copy(_codec.InputBuffer, p, window, q, t);
  285. p += t; n -= t;
  286. q += t; m -= t;
  287. if ((left -= t) != 0)
  288. break;
  289. mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE;
  290. break;
  291. case InflateBlockMode.TABLE:
  292. while (k < (14))
  293. {
  294. if (n != 0)
  295. {
  296. r = ZlibConstants.Z_OK;
  297. }
  298. else
  299. {
  300. bitb = b; bitk = k;
  301. _codec.AvailableBytesIn = n;
  302. _codec.TotalBytesIn += p - _codec.NextIn;
  303. _codec.NextIn = p;
  304. writeAt = q;
  305. return Flush(r);
  306. }
  307. n--;
  308. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  309. k += 8;
  310. }
  311. table = t = (b & 0x3fff);
  312. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  313. {
  314. mode = InflateBlockMode.BAD;
  315. _codec.Message = "too many length or distance symbols";
  316. r = ZlibConstants.Z_DATA_ERROR;
  317. bitb = b; bitk = k;
  318. _codec.AvailableBytesIn = n;
  319. _codec.TotalBytesIn += p - _codec.NextIn;
  320. _codec.NextIn = p;
  321. writeAt = q;
  322. return Flush(r);
  323. }
  324. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  325. if (blens == null || blens.Length < t)
  326. {
  327. blens = new int[t];
  328. }
  329. else
  330. {
  331. Array.Clear(blens, 0, t);
  332. // for (int i = 0; i < t; i++)
  333. // {
  334. // blens[i] = 0;
  335. // }
  336. }
  337. b >>= 14;
  338. k -= 14;
  339. index = 0;
  340. mode = InflateBlockMode.BTREE;
  341. goto case InflateBlockMode.BTREE;
  342. case InflateBlockMode.BTREE:
  343. while (index < 4 + (table >> 10))
  344. {
  345. while (k < (3))
  346. {
  347. if (n != 0)
  348. {
  349. r = ZlibConstants.Z_OK;
  350. }
  351. else
  352. {
  353. bitb = b; bitk = k;
  354. _codec.AvailableBytesIn = n;
  355. _codec.TotalBytesIn += p - _codec.NextIn;
  356. _codec.NextIn = p;
  357. writeAt = q;
  358. return Flush(r);
  359. }
  360. n--;
  361. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  362. k += 8;
  363. }
  364. blens[border[index++]] = b & 7;
  365. b >>= 3; k -= 3;
  366. }
  367. while (index < 19)
  368. {
  369. blens[border[index++]] = 0;
  370. }
  371. bb[0] = 7;
  372. t = inftree.inflate_trees_bits(blens, bb, tb, hufts, _codec);
  373. if (t != ZlibConstants.Z_OK)
  374. {
  375. r = t;
  376. if (r == ZlibConstants.Z_DATA_ERROR)
  377. {
  378. blens = null;
  379. mode = InflateBlockMode.BAD;
  380. }
  381. bitb = b; bitk = k;
  382. _codec.AvailableBytesIn = n;
  383. _codec.TotalBytesIn += p - _codec.NextIn;
  384. _codec.NextIn = p;
  385. writeAt = q;
  386. return Flush(r);
  387. }
  388. index = 0;
  389. mode = InflateBlockMode.DTREE;
  390. goto case InflateBlockMode.DTREE;
  391. case InflateBlockMode.DTREE:
  392. while (true)
  393. {
  394. t = table;
  395. if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)))
  396. {
  397. break;
  398. }
  399. int i, j, c;
  400. t = bb[0];
  401. while (k < t)
  402. {
  403. if (n != 0)
  404. {
  405. r = ZlibConstants.Z_OK;
  406. }
  407. else
  408. {
  409. bitb = b; bitk = k;
  410. _codec.AvailableBytesIn = n;
  411. _codec.TotalBytesIn += p - _codec.NextIn;
  412. _codec.NextIn = p;
  413. writeAt = q;
  414. return Flush(r);
  415. }
  416. n--;
  417. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  418. k += 8;
  419. }
  420. t = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 1];
  421. c = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 2];
  422. if (c < 16)
  423. {
  424. b >>= t; k -= t;
  425. blens[index++] = c;
  426. }
  427. else
  428. {
  429. // c == 16..18
  430. i = c == 18 ? 7 : c - 14;
  431. j = c == 18 ? 11 : 3;
  432. while (k < (t + i))
  433. {
  434. if (n != 0)
  435. {
  436. r = ZlibConstants.Z_OK;
  437. }
  438. else
  439. {
  440. bitb = b; bitk = k;
  441. _codec.AvailableBytesIn = n;
  442. _codec.TotalBytesIn += p - _codec.NextIn;
  443. _codec.NextIn = p;
  444. writeAt = q;
  445. return Flush(r);
  446. }
  447. n--;
  448. b |= (_codec.InputBuffer[p++] & 0xff) << k;
  449. k += 8;
  450. }
  451. b >>= t; k -= t;
  452. j += (b & InternalInflateConstants.InflateMask[i]);
  453. b >>= i; k -= i;
  454. i = index;
  455. t = table;
  456. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1))
  457. {
  458. blens = null;
  459. mode = InflateBlockMode.BAD;
  460. _codec.Message = "invalid bit length repeat";
  461. r = ZlibConstants.Z_DATA_ERROR;
  462. bitb = b; bitk = k;
  463. _codec.AvailableBytesIn = n;
  464. _codec.TotalBytesIn += p - _codec.NextIn;
  465. _codec.NextIn = p;
  466. writeAt = q;
  467. return Flush(r);
  468. }
  469. c = (c == 16) ? blens[i-1] : 0;
  470. do
  471. {
  472. blens[i++] = c;
  473. }
  474. while (--j != 0);
  475. index = i;
  476. }
  477. }
  478. tb[0] = -1;
  479. {
  480. int[] bl = new int[] { 9 }; // must be <= 9 for lookahead assumptions
  481. int[] bd = new int[] { 6 }; // must be <= 9 for lookahead assumptions
  482. int[] tl = new int[1];
  483. int[] td = new int[1];
  484. t = table;
  485. t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl, bd, tl, td, hufts, _codec);
  486. if (t != ZlibConstants.Z_OK)
  487. {
  488. if (t == ZlibConstants.Z_DATA_ERROR)
  489. {
  490. blens = null;
  491. mode = InflateBlockMode.BAD;
  492. }
  493. r = t;
  494. bitb = b; bitk = k;
  495. _codec.AvailableBytesIn = n;
  496. _codec.TotalBytesIn += p - _codec.NextIn;
  497. _codec.NextIn = p;
  498. writeAt = q;
  499. return Flush(r);
  500. }
  501. codes.Init(bl[0], bd[0], hufts, tl[0], hufts, td[0]);
  502. }
  503. mode = InflateBlockMode.CODES;
  504. goto case InflateBlockMode.CODES;
  505. case InflateBlockMode.CODES:
  506. bitb = b; bitk = k;
  507. _codec.AvailableBytesIn = n;
  508. _codec.TotalBytesIn += p - _codec.NextIn;
  509. _codec.NextIn = p;
  510. writeAt = q;
  511. r = codes.Process(this, r);
  512. if (r != ZlibConstants.Z_STREAM_END)
  513. {
  514. return Flush(r);
  515. }
  516. r = ZlibConstants.Z_OK;
  517. p = _codec.NextIn;
  518. n = _codec.AvailableBytesIn;
  519. b = bitb;
  520. k = bitk;
  521. q = writeAt;
  522. m = (int)(q < readAt ? readAt - q - 1 : end - q);
  523. if (last == 0)
  524. {
  525. mode = InflateBlockMode.TYPE;
  526. break;
  527. }
  528. mode = InflateBlockMode.DRY;
  529. goto case InflateBlockMode.DRY;
  530. case InflateBlockMode.DRY:
  531. writeAt = q;
  532. r = Flush(r);
  533. q = writeAt; m = (int)(q < readAt ? readAt - q - 1 : end - q);
  534. if (readAt != writeAt)
  535. {
  536. bitb = b; bitk = k;
  537. _codec.AvailableBytesIn = n;
  538. _codec.TotalBytesIn += p - _codec.NextIn;
  539. _codec.NextIn = p;
  540. writeAt = q;
  541. return Flush(r);
  542. }
  543. mode = InflateBlockMode.DONE;
  544. goto case InflateBlockMode.DONE;
  545. case InflateBlockMode.DONE:
  546. r = ZlibConstants.Z_STREAM_END;
  547. bitb = b;
  548. bitk = k;
  549. _codec.AvailableBytesIn = n;
  550. _codec.TotalBytesIn += p - _codec.NextIn;
  551. _codec.NextIn = p;
  552. writeAt = q;
  553. return Flush(r);
  554. case InflateBlockMode.BAD:
  555. r = ZlibConstants.Z_DATA_ERROR;
  556. bitb = b; bitk = k;
  557. _codec.AvailableBytesIn = n;
  558. _codec.TotalBytesIn += p - _codec.NextIn;
  559. _codec.NextIn = p;
  560. writeAt = q;
  561. return Flush(r);
  562. default:
  563. r = ZlibConstants.Z_STREAM_ERROR;
  564. bitb = b; bitk = k;
  565. _codec.AvailableBytesIn = n;
  566. _codec.TotalBytesIn += p - _codec.NextIn;
  567. _codec.NextIn = p;
  568. writeAt = q;
  569. return Flush(r);
  570. }
  571. }
  572. }
  573. internal void Free()
  574. {
  575. Reset();
  576. Extensions.VariableSizedBufferPool.Release(window);
  577. window = null;
  578. hufts = null;
  579. }
  580. internal void SetDictionary(byte[] d, int start, int n)
  581. {
  582. Array.Copy(d, start, window, 0, n);
  583. readAt = writeAt = n;
  584. }
  585. // Returns true if inflate is currently at the end of a block generated
  586. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  587. internal int SyncPoint()
  588. {
  589. return mode == InflateBlockMode.LENS ? 1 : 0;
  590. }
  591. // copy as much as possible from the sliding window to the output area
  592. internal int Flush(int r)
  593. {
  594. int nBytes;
  595. for (int pass=0; pass < 2; pass++)
  596. {
  597. if (pass==0)
  598. {
  599. // compute number of bytes to copy as far as end of window
  600. nBytes = (int)((readAt <= writeAt ? writeAt : end) - readAt);
  601. }
  602. else
  603. {
  604. // compute bytes to copy
  605. nBytes = writeAt - readAt;
  606. }
  607. // workitem 8870
  608. if (nBytes == 0)
  609. {
  610. if (r == ZlibConstants.Z_BUF_ERROR)
  611. r = ZlibConstants.Z_OK;
  612. return r;
  613. }
  614. if (nBytes > _codec.AvailableBytesOut)
  615. nBytes = _codec.AvailableBytesOut;
  616. if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
  617. r = ZlibConstants.Z_OK;
  618. // update counters
  619. _codec.AvailableBytesOut -= nBytes;
  620. _codec.TotalBytesOut += nBytes;
  621. // update check information
  622. if (checkfn != null)
  623. _codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
  624. // copy as far as end of window
  625. Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
  626. _codec.NextOut += nBytes;
  627. readAt += nBytes;
  628. // see if more to copy at beginning of window
  629. if (readAt == end && pass == 0)
  630. {
  631. // wrap pointers
  632. readAt = 0;
  633. if (writeAt == end)
  634. writeAt = 0;
  635. }
  636. else pass++;
  637. }
  638. // done
  639. return r;
  640. }
  641. }
  642. internal static class InternalInflateConstants
  643. {
  644. // And'ing with mask[n] masks the lower n bits
  645. internal static readonly int[] InflateMask = new int[] {
  646. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  647. 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
  648. 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
  649. 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
  650. }
  651. sealed class InflateCodes
  652. {
  653. // waiting for "i:"=input,
  654. // "o:"=output,
  655. // "x:"=nothing
  656. private const int START = 0; // x: set up for LEN
  657. private const int LEN = 1; // i: get length/literal/eob next
  658. private const int LENEXT = 2; // i: getting length extra (have base)
  659. private const int DIST = 3; // i: get distance next
  660. private const int DISTEXT = 4; // i: getting distance extra
  661. private const int COPY = 5; // o: copying bytes in window, waiting for space
  662. private const int LIT = 6; // o: got literal, waiting for output space
  663. private const int WASH = 7; // o: got eob, possibly still output waiting
  664. private const int END = 8; // x: got eob and all data flushed
  665. private const int BADCODE = 9; // x: got error
  666. internal int mode; // current inflate_codes mode
  667. // mode dependent information
  668. internal int len;
  669. internal int[] tree; // pointer into tree
  670. internal int tree_index = 0;
  671. internal int need; // bits needed
  672. internal int lit;
  673. // if EXT or COPY, where and how much
  674. internal int bitsToGet; // bits to get for extra
  675. internal int dist; // distance back to copy from
  676. internal byte lbits; // ltree bits decoded per branch
  677. internal byte dbits; // dtree bits decoder per branch
  678. internal int[] ltree; // literal/length/eob tree
  679. internal int ltree_index; // literal/length/eob tree
  680. internal int[] dtree; // distance tree
  681. internal int dtree_index; // distance tree
  682. internal InflateCodes()
  683. {
  684. }
  685. internal void Init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index)
  686. {
  687. mode = START;
  688. lbits = (byte)bl;
  689. dbits = (byte)bd;
  690. ltree = tl;
  691. ltree_index = tl_index;
  692. dtree = td;
  693. dtree_index = td_index;
  694. tree = null;
  695. }
  696. internal int Process(InflateBlocks blocks, int r)
  697. {
  698. int j; // temporary storage
  699. int tindex; // temporary pointer
  700. int e; // extra bits or operation
  701. int b = 0; // bit buffer
  702. int k = 0; // bits in bit buffer
  703. int p = 0; // input data pointer
  704. int n; // bytes available there
  705. int q; // output window write pointer
  706. int m; // bytes to end of window or read pointer
  707. int f; // pointer to copy strings from
  708. ZlibCodec z = blocks._codec;
  709. // copy input/output information to locals (UPDATE macro restores)
  710. p = z.NextIn;
  711. n = z.AvailableBytesIn;
  712. b = blocks.bitb;
  713. k = blocks.bitk;
  714. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  715. // process input and output based on current state
  716. while (true)
  717. {
  718. switch (mode)
  719. {
  720. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  721. case START: // x: set up for LEN
  722. if (m >= 258 && n >= 10)
  723. {
  724. blocks.bitb = b; blocks.bitk = k;
  725. z.AvailableBytesIn = n;
  726. z.TotalBytesIn += p - z.NextIn;
  727. z.NextIn = p;
  728. blocks.writeAt = q;
  729. r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);
  730. p = z.NextIn;
  731. n = z.AvailableBytesIn;
  732. b = blocks.bitb;
  733. k = blocks.bitk;
  734. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  735. if (r != ZlibConstants.Z_OK)
  736. {
  737. mode = (r == ZlibConstants.Z_STREAM_END) ? WASH : BADCODE;
  738. break;
  739. }
  740. }
  741. need = lbits;
  742. tree = ltree;
  743. tree_index = ltree_index;
  744. mode = LEN;
  745. goto case LEN;
  746. case LEN: // i: get length/literal/eob next
  747. j = need;
  748. while (k < j)
  749. {
  750. if (n != 0)
  751. r = ZlibConstants.Z_OK;
  752. else
  753. {
  754. blocks.bitb = b; blocks.bitk = k;
  755. z.AvailableBytesIn = n;
  756. z.TotalBytesIn += p - z.NextIn;
  757. z.NextIn = p;
  758. blocks.writeAt = q;
  759. return blocks.Flush(r);
  760. }
  761. n--;
  762. b |= (z.InputBuffer[p++] & 0xff) << k;
  763. k += 8;
  764. }
  765. tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
  766. b >>= (tree[tindex + 1]);
  767. k -= (tree[tindex + 1]);
  768. e = tree[tindex];
  769. if (e == 0)
  770. {
  771. // literal
  772. lit = tree[tindex + 2];
  773. mode = LIT;
  774. break;
  775. }
  776. if ((e & 16) != 0)
  777. {
  778. // length
  779. bitsToGet = e & 15;
  780. len = tree[tindex + 2];
  781. mode = LENEXT;
  782. break;
  783. }
  784. if ((e & 64) == 0)
  785. {
  786. // next table
  787. need = e;
  788. tree_index = tindex / 3 + tree[tindex + 2];
  789. break;
  790. }
  791. if ((e & 32) != 0)
  792. {
  793. // end of block
  794. mode = WASH;
  795. break;
  796. }
  797. mode = BADCODE; // invalid code
  798. z.Message = "invalid literal/length code";
  799. r = ZlibConstants.Z_DATA_ERROR;
  800. blocks.bitb = b; blocks.bitk = k;
  801. z.AvailableBytesIn = n;
  802. z.TotalBytesIn += p - z.NextIn;
  803. z.NextIn = p;
  804. blocks.writeAt = q;
  805. return blocks.Flush(r);
  806. case LENEXT: // i: getting length extra (have base)
  807. j = bitsToGet;
  808. while (k < j)
  809. {
  810. if (n != 0)
  811. r = ZlibConstants.Z_OK;
  812. else
  813. {
  814. blocks.bitb = b; blocks.bitk = k;
  815. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  816. blocks.writeAt = q;
  817. return blocks.Flush(r);
  818. }
  819. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  820. k += 8;
  821. }
  822. len += (b & InternalInflateConstants.InflateMask[j]);
  823. b >>= j;
  824. k -= j;
  825. need = dbits;
  826. tree = dtree;
  827. tree_index = dtree_index;
  828. mode = DIST;
  829. goto case DIST;
  830. case DIST: // i: get distance next
  831. j = need;
  832. while (k < j)
  833. {
  834. if (n != 0)
  835. r = ZlibConstants.Z_OK;
  836. else
  837. {
  838. blocks.bitb = b; blocks.bitk = k;
  839. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  840. blocks.writeAt = q;
  841. return blocks.Flush(r);
  842. }
  843. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  844. k += 8;
  845. }
  846. tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
  847. b >>= tree[tindex + 1];
  848. k -= tree[tindex + 1];
  849. e = (tree[tindex]);
  850. if ((e & 0x10) != 0)
  851. {
  852. // distance
  853. bitsToGet = e & 15;
  854. dist = tree[tindex + 2];
  855. mode = DISTEXT;
  856. break;
  857. }
  858. if ((e & 64) == 0)
  859. {
  860. // next table
  861. need = e;
  862. tree_index = tindex / 3 + tree[tindex + 2];
  863. break;
  864. }
  865. mode = BADCODE; // invalid code
  866. z.Message = "invalid distance code";
  867. r = ZlibConstants.Z_DATA_ERROR;
  868. blocks.bitb = b; blocks.bitk = k;
  869. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  870. blocks.writeAt = q;
  871. return blocks.Flush(r);
  872. case DISTEXT: // i: getting distance extra
  873. j = bitsToGet;
  874. while (k < j)
  875. {
  876. if (n != 0)
  877. r = ZlibConstants.Z_OK;
  878. else
  879. {
  880. blocks.bitb = b; blocks.bitk = k;
  881. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  882. blocks.writeAt = q;
  883. return blocks.Flush(r);
  884. }
  885. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  886. k += 8;
  887. }
  888. dist += (b & InternalInflateConstants.InflateMask[j]);
  889. b >>= j;
  890. k -= j;
  891. mode = COPY;
  892. goto case COPY;
  893. case COPY: // o: copying bytes in window, waiting for space
  894. f = q - dist;
  895. while (f < 0)
  896. {
  897. // modulo window size-"while" instead
  898. f += blocks.end; // of "if" handles invalid distances
  899. }
  900. while (len != 0)
  901. {
  902. if (m == 0)
  903. {
  904. if (q == blocks.end && blocks.readAt != 0)
  905. {
  906. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  907. }
  908. if (m == 0)
  909. {
  910. blocks.writeAt = q; r = blocks.Flush(r);
  911. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  912. if (q == blocks.end && blocks.readAt != 0)
  913. {
  914. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  915. }
  916. if (m == 0)
  917. {
  918. blocks.bitb = b; blocks.bitk = k;
  919. z.AvailableBytesIn = n;
  920. z.TotalBytesIn += p - z.NextIn;
  921. z.NextIn = p;
  922. blocks.writeAt = q;
  923. return blocks.Flush(r);
  924. }
  925. }
  926. }
  927. blocks.window[q++] = blocks.window[f++]; m--;
  928. if (f == blocks.end)
  929. f = 0;
  930. len--;
  931. }
  932. mode = START;
  933. break;
  934. case LIT: // o: got literal, waiting for output space
  935. if (m == 0)
  936. {
  937. if (q == blocks.end && blocks.readAt != 0)
  938. {
  939. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  940. }
  941. if (m == 0)
  942. {
  943. blocks.writeAt = q; r = blocks.Flush(r);
  944. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  945. if (q == blocks.end && blocks.readAt != 0)
  946. {
  947. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  948. }
  949. if (m == 0)
  950. {
  951. blocks.bitb = b; blocks.bitk = k;
  952. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  953. blocks.writeAt = q;
  954. return blocks.Flush(r);
  955. }
  956. }
  957. }
  958. r = ZlibConstants.Z_OK;
  959. blocks.window[q++] = (byte)lit; m--;
  960. mode = START;
  961. break;
  962. case WASH: // o: got eob, possibly more output
  963. if (k > 7)
  964. {
  965. // return unused byte, if any
  966. k -= 8;
  967. n++;
  968. p--; // can always return one
  969. }
  970. blocks.writeAt = q; r = blocks.Flush(r);
  971. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  972. if (blocks.readAt != blocks.writeAt)
  973. {
  974. blocks.bitb = b; blocks.bitk = k;
  975. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  976. blocks.writeAt = q;
  977. return blocks.Flush(r);
  978. }
  979. mode = END;
  980. goto case END;
  981. case END:
  982. r = ZlibConstants.Z_STREAM_END;
  983. blocks.bitb = b; blocks.bitk = k;
  984. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  985. blocks.writeAt = q;
  986. return blocks.Flush(r);
  987. case BADCODE: // x: got error
  988. r = ZlibConstants.Z_DATA_ERROR;
  989. blocks.bitb = b; blocks.bitk = k;
  990. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  991. blocks.writeAt = q;
  992. return blocks.Flush(r);
  993. default:
  994. r = ZlibConstants.Z_STREAM_ERROR;
  995. blocks.bitb = b; blocks.bitk = k;
  996. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  997. blocks.writeAt = q;
  998. return blocks.Flush(r);
  999. }
  1000. }
  1001. }
  1002. // Called with number of bytes left to write in window at least 258
  1003. // (the maximum string length) and number of input bytes available
  1004. // at least ten. The ten bytes are six bytes for the longest length/
  1005. // distance pair plus four bytes for overloading the bit buffer.
  1006. internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
  1007. {
  1008. int t; // temporary pointer
  1009. int[] tp; // temporary pointer
  1010. int tp_index; // temporary pointer
  1011. int e; // extra bits or operation
  1012. int b; // bit buffer
  1013. int k; // bits in bit buffer
  1014. int p; // input data pointer
  1015. int n; // bytes available there
  1016. int q; // output window write pointer
  1017. int m; // bytes to end of window or read pointer
  1018. int ml; // mask for literal/length tree
  1019. int md; // mask for distance tree
  1020. int c; // bytes to copy
  1021. int d; // distance back to copy from
  1022. int r; // copy source pointer
  1023. int tp_index_t_3; // (tp_index+t)*3
  1024. // load input, output, bit values
  1025. p = z.NextIn; n = z.AvailableBytesIn; b = s.bitb; k = s.bitk;
  1026. q = s.writeAt; m = q < s.readAt ? s.readAt - q - 1 : s.end - q;
  1027. // initialize masks
  1028. ml = InternalInflateConstants.InflateMask[bl];
  1029. md = InternalInflateConstants.InflateMask[bd];
  1030. // do until not enough input or output space for fast loop
  1031. do
  1032. {
  1033. // assume called with m >= 258 && n >= 10
  1034. // get literal/length code
  1035. while (k < (20))
  1036. {
  1037. // max bits for literal/length code
  1038. n--;
  1039. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1040. }
  1041. t = b & ml;
  1042. tp = tl;
  1043. tp_index = tl_index;
  1044. tp_index_t_3 = (tp_index + t) * 3;
  1045. if ((e = tp[tp_index_t_3]) == 0)
  1046. {
  1047. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1048. s.window[q++] = (byte)tp[tp_index_t_3 + 2];
  1049. m--;
  1050. continue;
  1051. }
  1052. do
  1053. {
  1054. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1055. if ((e & 16) != 0)
  1056. {
  1057. e &= 15;
  1058. c = tp[tp_index_t_3 + 2] + ((int)b & InternalInflateConstants.InflateMask[e]);
  1059. b >>= e; k -= e;
  1060. // decode distance base of block to copy
  1061. while (k < 15)
  1062. {
  1063. // max bits for distance code
  1064. n--;
  1065. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1066. }
  1067. t = b & md;
  1068. tp = td;
  1069. tp_index = td_index;
  1070. tp_index_t_3 = (tp_index + t) * 3;
  1071. e = tp[tp_index_t_3];
  1072. do
  1073. {
  1074. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1075. if ((e & 16) != 0)
  1076. {
  1077. // get extra bits to add to distance base
  1078. e &= 15;
  1079. while (k < e)
  1080. {
  1081. // get extra bits (up to 13)
  1082. n--;
  1083. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1084. }
  1085. d = tp[tp_index_t_3 + 2] + (b & InternalInflateConstants.InflateMask[e]);
  1086. b >>= e; k -= e;
  1087. // do the copy
  1088. m -= c;
  1089. if (q >= d)
  1090. {
  1091. // offset before dest
  1092. // just copy
  1093. r = q - d;
  1094. if (q - r > 0 && 2 > (q - r))
  1095. {
  1096. s.window[q++] = s.window[r++]; // minimum count is three,
  1097. s.window[q++] = s.window[r++]; // so unroll loop a little
  1098. c -= 2;
  1099. }
  1100. else
  1101. {
  1102. Array.Copy(s.window, r, s.window, q, 2);
  1103. q += 2; r += 2; c -= 2;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. // else offset after destination
  1109. r = q - d;
  1110. do
  1111. {
  1112. r += s.end; // force pointer in window
  1113. }
  1114. while (r < 0); // covers invalid distances
  1115. e = s.end - r;
  1116. if (c > e)
  1117. {
  1118. // if source crosses,
  1119. c -= e; // wrapped copy
  1120. if (q - r > 0 && e > (q - r))
  1121. {
  1122. do
  1123. {
  1124. s.window[q++] = s.window[r++];
  1125. }
  1126. while (--e != 0);
  1127. }
  1128. else
  1129. {
  1130. Array.Copy(s.window, r, s.window, q, e);
  1131. q += e; r += e; e = 0;
  1132. }
  1133. r = 0; // copy rest from start of window
  1134. }
  1135. }
  1136. // copy all or what's left
  1137. if (q - r > 0 && c > (q - r))
  1138. {
  1139. do
  1140. {
  1141. s.window[q++] = s.window[r++];
  1142. }
  1143. while (--c != 0);
  1144. }
  1145. else
  1146. {
  1147. Array.Copy(s.window, r, s.window, q, c);
  1148. q += c; r += c; c = 0;
  1149. }
  1150. break;
  1151. }
  1152. else if ((e & 64) == 0)
  1153. {
  1154. t += tp[tp_index_t_3 + 2];
  1155. t += (b & InternalInflateConstants.InflateMask[e]);
  1156. tp_index_t_3 = (tp_index + t) * 3;
  1157. e = tp[tp_index_t_3];
  1158. }
  1159. else
  1160. {
  1161. z.Message = "invalid distance code";
  1162. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1163. s.bitb = b; s.bitk = k;
  1164. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1165. s.writeAt = q;
  1166. return ZlibConstants.Z_DATA_ERROR;
  1167. }
  1168. }
  1169. while (true);
  1170. break;
  1171. }
  1172. if ((e & 64) == 0)
  1173. {
  1174. t += tp[tp_index_t_3 + 2];
  1175. t += (b & InternalInflateConstants.InflateMask[e]);
  1176. tp_index_t_3 = (tp_index + t) * 3;
  1177. if ((e = tp[tp_index_t_3]) == 0)
  1178. {
  1179. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1180. s.window[q++] = (byte)tp[tp_index_t_3 + 2];
  1181. m--;
  1182. break;
  1183. }
  1184. }
  1185. else if ((e & 32) != 0)
  1186. {
  1187. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1188. s.bitb = b; s.bitk = k;
  1189. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1190. s.writeAt = q;
  1191. return ZlibConstants.Z_STREAM_END;
  1192. }
  1193. else
  1194. {
  1195. z.Message = "invalid literal/length code";
  1196. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1197. s.bitb = b; s.bitk = k;
  1198. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1199. s.writeAt = q;
  1200. return ZlibConstants.Z_DATA_ERROR;
  1201. }
  1202. }
  1203. while (true);
  1204. }
  1205. while (m >= 258 && n >= 10);
  1206. // not enough input or output--restore pointers and return
  1207. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1208. s.bitb = b; s.bitk = k;
  1209. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1210. s.writeAt = q;
  1211. return ZlibConstants.Z_OK;
  1212. }
  1213. }
  1214. internal sealed class InflateManager
  1215. {
  1216. // preset dictionary flag in zlib header
  1217. private const int PRESET_DICT = 0x20;
  1218. private const int Z_DEFLATED = 8;
  1219. private enum InflateManagerMode
  1220. {
  1221. METHOD = 0, // waiting for method byte
  1222. FLAG = 1, // waiting for flag byte
  1223. DICT4 = 2, // four dictionary check bytes to go
  1224. DICT3 = 3, // three dictionary check bytes to go
  1225. DICT2 = 4, // two dictionary check bytes to go
  1226. DICT1 = 5, // one dictionary check byte to go
  1227. DICT0 = 6, // waiting for inflateSetDictionary
  1228. BLOCKS = 7, // decompressing blocks
  1229. CHECK4 = 8, // four check bytes to go
  1230. CHECK3 = 9, // three check bytes to go
  1231. CHECK2 = 10, // two check bytes to go
  1232. CHECK1 = 11, // one check byte to go
  1233. DONE = 12, // finished check, done
  1234. BAD = 13, // got an error--stay here
  1235. }
  1236. private InflateManagerMode mode; // current inflate mode
  1237. internal ZlibCodec _codec; // pointer back to this zlib stream
  1238. // mode dependent information
  1239. internal int method; // if FLAGS, method byte
  1240. // if CHECK, check values to compare
  1241. internal uint computedCheck; // computed check value
  1242. internal uint expectedCheck; // stream check value
  1243. // if BAD, inflateSync's marker bytes count
  1244. internal int marker;
  1245. // mode independent information
  1246. //internal int nowrap; // flag for no wrapper
  1247. private bool _handleRfc1950HeaderBytes = true;
  1248. internal bool HandleRfc1950HeaderBytes
  1249. {
  1250. get { return _handleRfc1950HeaderBytes; }
  1251. set { _handleRfc1950HeaderBytes = value; }
  1252. }
  1253. internal int wbits; // log2(window size) (8..15, defaults to 15)
  1254. internal InflateBlocks blocks; // current inflate_blocks state
  1255. public InflateManager() { }
  1256. public InflateManager(bool expectRfc1950HeaderBytes)
  1257. {
  1258. _handleRfc1950HeaderBytes = expectRfc1950HeaderBytes;
  1259. }
  1260. internal int Reset()
  1261. {
  1262. _codec.TotalBytesIn = _codec.TotalBytesOut = 0;
  1263. _codec.Message = null;
  1264. mode = HandleRfc1950HeaderBytes ? InflateManagerMode.METHOD : InflateManagerMode.BLOCKS;
  1265. blocks.Reset();
  1266. return ZlibConstants.Z_OK;
  1267. }
  1268. internal int End()
  1269. {
  1270. if (blocks != null)
  1271. blocks.Free();
  1272. blocks = null;
  1273. return ZlibConstants.Z_OK;
  1274. }
  1275. internal int Initialize(ZlibCodec codec, int w)
  1276. {
  1277. _codec = codec;
  1278. _codec.Message = null;
  1279. blocks = null;
  1280. // handle undocumented nowrap option (no zlib header or check)
  1281. //nowrap = 0;
  1282. //if (w < 0)
  1283. //{
  1284. // w = - w;
  1285. // nowrap = 1;
  1286. //}
  1287. // set window size
  1288. if (w < 8 || w > 15)
  1289. {
  1290. End();
  1291. throw new ZlibException("Bad window size.");
  1292. //return ZlibConstants.Z_STREAM_ERROR;
  1293. }
  1294. wbits = w;
  1295. blocks = new InflateBlocks(codec,
  1296. HandleRfc1950HeaderBytes ? this : null,
  1297. 1 << w);
  1298. // reset state
  1299. Reset();
  1300. return ZlibConstants.Z_OK;
  1301. }
  1302. internal int Inflate(FlushType flush)
  1303. {
  1304. int b;
  1305. if (_codec.InputBuffer == null)
  1306. throw new ZlibException("InputBuffer is null. ");
  1307. // int f = (flush == FlushType.Finish)
  1308. // ? ZlibConstants.Z_BUF_ERROR
  1309. // : ZlibConstants.Z_OK;
  1310. // workitem 8870
  1311. int f = ZlibConstants.Z_OK;
  1312. int r = ZlibConstants.Z_BUF_ERROR;
  1313. while (true)
  1314. {
  1315. switch (mode)
  1316. {
  1317. case InflateManagerMode.METHOD:
  1318. if (_codec.AvailableBytesIn == 0) return r;
  1319. r = f;
  1320. _codec.AvailableBytesIn--;
  1321. _codec.TotalBytesIn++;
  1322. if (((method = _codec.InputBuffer[_codec.NextIn++]) & 0xf) != Z_DEFLATED)
  1323. {
  1324. mode = InflateManagerMode.BAD;
  1325. _codec.Message = String.Format("unknown compression method (0x{0:X2})", method);
  1326. marker = 5; // can't try inflateSync
  1327. break;
  1328. }
  1329. if ((method >> 4) + 8 > wbits)
  1330. {
  1331. mode = InflateManagerMode.BAD;
  1332. _codec.Message = String.Format("invalid window size ({0})", (method >> 4) + 8);
  1333. marker = 5; // can't try inflateSync
  1334. break;
  1335. }
  1336. mode = InflateManagerMode.FLAG;
  1337. break;
  1338. case InflateManagerMode.FLAG:
  1339. if (_codec.AvailableBytesIn == 0) return r;
  1340. r = f;
  1341. _codec.AvailableBytesIn--;
  1342. _codec.TotalBytesIn++;
  1343. b = (_codec.InputBuffer[_codec.NextIn++]) & 0xff;
  1344. if ((((method << 8) + b) % 31) != 0)
  1345. {
  1346. mode = InflateManagerMode.BAD;
  1347. _codec.Message = "incorrect header check";
  1348. marker = 5; // can't try inflateSync
  1349. break;
  1350. }
  1351. mode = ((b & PRESET_DICT) == 0)
  1352. ? InflateManagerMode.BLOCKS
  1353. : InflateManagerMode.DICT4;
  1354. break;
  1355. case InflateManagerMode.DICT4:
  1356. if (_codec.AvailableBytesIn == 0) return r;
  1357. r = f;
  1358. _codec.AvailableBytesIn--;
  1359. _codec.TotalBytesIn++;
  1360. expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
  1361. mode = InflateManagerMode.DICT3;
  1362. break;
  1363. case InflateManagerMode.DICT3:
  1364. if (_codec.AvailableBytesIn == 0) return r;
  1365. r = f;
  1366. _codec.AvailableBytesIn--;
  1367. _codec.TotalBytesIn++;
  1368. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
  1369. mode = InflateManagerMode.DICT2;
  1370. break;
  1371. case InflateManagerMode.DICT2:
  1372. if (_codec.AvailableBytesIn == 0) return r;
  1373. r = f;
  1374. _codec.AvailableBytesIn--;
  1375. _codec.TotalBytesIn++;
  1376. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
  1377. mode = InflateManagerMode.DICT1;
  1378. break;
  1379. case InflateManagerMode.DICT1:
  1380. if (_codec.AvailableBytesIn == 0) return r;
  1381. r = f;
  1382. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1383. expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
  1384. _codec._Adler32 = expectedCheck;
  1385. mode = InflateManagerMode.DICT0;
  1386. return ZlibConstants.Z_NEED_DICT;
  1387. case InflateManagerMode.DICT0:
  1388. mode = InflateManagerMode.BAD;
  1389. _codec.Message = "need dictionary";
  1390. marker = 0; // can try inflateSync
  1391. return ZlibConstants.Z_STREAM_ERROR;
  1392. case InflateManagerMode.BLOCKS:
  1393. r = blocks.Process(r);
  1394. if (r == ZlibConstants.Z_DATA_ERROR)
  1395. {
  1396. mode = InflateManagerMode.BAD;
  1397. marker = 0; // can try inflateSync
  1398. break;
  1399. }
  1400. if (r == ZlibConstants.Z_OK) r = f;
  1401. if (r != ZlibConstants.Z_STREAM_END)
  1402. return r;
  1403. r = f;
  1404. computedCheck = blocks.Reset();
  1405. if (!HandleRfc1950HeaderBytes)
  1406. {
  1407. mode = InflateManagerMode.DONE;
  1408. return ZlibConstants.Z_STREAM_END;
  1409. }
  1410. mode = InflateManagerMode.CHECK4;
  1411. break;
  1412. case InflateManagerMode.CHECK4:
  1413. if (_codec.AvailableBytesIn == 0) return r;
  1414. r = f;
  1415. _codec.AvailableBytesIn--;
  1416. _codec.TotalBytesIn++;
  1417. expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
  1418. mode = InflateManagerMode.CHECK3;
  1419. break;
  1420. case InflateManagerMode.CHECK3:
  1421. if (_codec.AvailableBytesIn == 0) return r;
  1422. r = f;
  1423. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1424. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
  1425. mode = InflateManagerMode.CHECK2;
  1426. break;
  1427. case InflateManagerMode.CHECK2:
  1428. if (_codec.AvailableBytesIn == 0) return r;
  1429. r = f;
  1430. _codec.AvailableBytesIn--;
  1431. _codec.TotalBytesIn++;
  1432. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
  1433. mode = InflateManagerMode.CHECK1;
  1434. break;
  1435. case InflateManagerMode.CHECK1:
  1436. if (_codec.AvailableBytesIn == 0) return r;
  1437. r = f;
  1438. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1439. expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
  1440. if (computedCheck != expectedCheck)
  1441. {
  1442. mode = InflateManagerMode.BAD;
  1443. _codec.Message = "incorrect data check";
  1444. marker = 5; // can't try inflateSync
  1445. break;
  1446. }
  1447. mode = InflateManagerMode.DONE;
  1448. return ZlibConstants.Z_STREAM_END;
  1449. case InflateManagerMode.DONE:
  1450. return ZlibConstants.Z_STREAM_END;
  1451. case InflateManagerMode.BAD:
  1452. throw new ZlibException(String.Format("Bad state ({0})", _codec.Message));
  1453. default:
  1454. throw new ZlibException("Stream error.");
  1455. }
  1456. }
  1457. }
  1458. internal int SetDictionary(byte[] dictionary)
  1459. {
  1460. int index = 0;
  1461. int length = dictionary.Length;
  1462. if (mode != InflateManagerMode.DICT0)
  1463. throw new ZlibException("Stream error.");
  1464. if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
  1465. {
  1466. return ZlibConstants.Z_DATA_ERROR;
  1467. }
  1468. _codec._Adler32 = Adler.Adler32(0, null, 0, 0);
  1469. if (length >= (1 << wbits))
  1470. {
  1471. length = (1 << wbits) - 1;
  1472. index = dictionary.Length - length;
  1473. }
  1474. blocks.SetDictionary(dictionary, index, length);
  1475. mode = InflateManagerMode.BLOCKS;
  1476. return ZlibConstants.Z_OK;
  1477. }
  1478. private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
  1479. internal int Sync()
  1480. {
  1481. int n; // number of bytes to look at
  1482. int p; // pointer to bytes
  1483. int m; // number of marker bytes found in a row
  1484. long r, w; // temporaries to save total_in and total_out
  1485. // set up
  1486. if (mode != InflateManagerMode.BAD)
  1487. {
  1488. mode = InflateManagerMode.BAD;
  1489. marker = 0;
  1490. }
  1491. if ((n = _codec.AvailableBytesIn) == 0)
  1492. return ZlibConstants.Z_BUF_ERROR;
  1493. p = _codec.NextIn;
  1494. m = marker;
  1495. // search
  1496. while (n != 0 && m < 4)
  1497. {
  1498. if (_codec.InputBuffer[p] == mark[m])
  1499. {
  1500. m++;
  1501. }
  1502. else if (_codec.InputBuffer[p] != 0)
  1503. {
  1504. m = 0;
  1505. }
  1506. else
  1507. {
  1508. m = 4 - m;
  1509. }
  1510. p++; n--;
  1511. }
  1512. // restore
  1513. _codec.TotalBytesIn += p - _codec.NextIn;
  1514. _codec.NextIn = p;
  1515. _codec.AvailableBytesIn = n;
  1516. marker = m;
  1517. // return no joy or set up to restart on a new block
  1518. if (m != 4)
  1519. {
  1520. return ZlibConstants.Z_DATA_ERROR;
  1521. }
  1522. r = _codec.TotalBytesIn;
  1523. w = _codec.TotalBytesOut;
  1524. Reset();
  1525. _codec.TotalBytesIn = r;
  1526. _codec.TotalBytesOut = w;
  1527. mode = InflateManagerMode.BLOCKS;
  1528. return ZlibConstants.Z_OK;
  1529. }
  1530. // Returns true if inflate is currently at the end of a block generated
  1531. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1532. // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  1533. // but removes the length bytes of the resulting empty stored block. When
  1534. // decompressing, PPP checks that at the end of input packet, inflate is
  1535. // waiting for these length bytes.
  1536. internal int SyncPoint(ZlibCodec z)
  1537. {
  1538. return blocks.SyncPoint();
  1539. }
  1540. }
  1541. }