Inflate.cs 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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 = new byte[w];
  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. window = null;
  577. hufts = null;
  578. }
  579. internal void SetDictionary(byte[] d, int start, int n)
  580. {
  581. Array.Copy(d, start, window, 0, n);
  582. readAt = writeAt = n;
  583. }
  584. // Returns true if inflate is currently at the end of a block generated
  585. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  586. internal int SyncPoint()
  587. {
  588. return mode == InflateBlockMode.LENS ? 1 : 0;
  589. }
  590. // copy as much as possible from the sliding window to the output area
  591. internal int Flush(int r)
  592. {
  593. int nBytes;
  594. for (int pass=0; pass < 2; pass++)
  595. {
  596. if (pass==0)
  597. {
  598. // compute number of bytes to copy as far as end of window
  599. nBytes = (int)((readAt <= writeAt ? writeAt : end) - readAt);
  600. }
  601. else
  602. {
  603. // compute bytes to copy
  604. nBytes = writeAt - readAt;
  605. }
  606. // workitem 8870
  607. if (nBytes == 0)
  608. {
  609. if (r == ZlibConstants.Z_BUF_ERROR)
  610. r = ZlibConstants.Z_OK;
  611. return r;
  612. }
  613. if (nBytes > _codec.AvailableBytesOut)
  614. nBytes = _codec.AvailableBytesOut;
  615. if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
  616. r = ZlibConstants.Z_OK;
  617. // update counters
  618. _codec.AvailableBytesOut -= nBytes;
  619. _codec.TotalBytesOut += nBytes;
  620. // update check information
  621. if (checkfn != null)
  622. _codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
  623. // copy as far as end of window
  624. Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
  625. _codec.NextOut += nBytes;
  626. readAt += nBytes;
  627. // see if more to copy at beginning of window
  628. if (readAt == end && pass == 0)
  629. {
  630. // wrap pointers
  631. readAt = 0;
  632. if (writeAt == end)
  633. writeAt = 0;
  634. }
  635. else pass++;
  636. }
  637. // done
  638. return r;
  639. }
  640. }
  641. internal static class InternalInflateConstants
  642. {
  643. // And'ing with mask[n] masks the lower n bits
  644. internal static readonly int[] InflateMask = new int[] {
  645. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  646. 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
  647. 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
  648. 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
  649. }
  650. sealed class InflateCodes
  651. {
  652. // waiting for "i:"=input,
  653. // "o:"=output,
  654. // "x:"=nothing
  655. private const int START = 0; // x: set up for LEN
  656. private const int LEN = 1; // i: get length/literal/eob next
  657. private const int LENEXT = 2; // i: getting length extra (have base)
  658. private const int DIST = 3; // i: get distance next
  659. private const int DISTEXT = 4; // i: getting distance extra
  660. private const int COPY = 5; // o: copying bytes in window, waiting for space
  661. private const int LIT = 6; // o: got literal, waiting for output space
  662. private const int WASH = 7; // o: got eob, possibly still output waiting
  663. private const int END = 8; // x: got eob and all data flushed
  664. private const int BADCODE = 9; // x: got error
  665. internal int mode; // current inflate_codes mode
  666. // mode dependent information
  667. internal int len;
  668. internal int[] tree; // pointer into tree
  669. internal int tree_index = 0;
  670. internal int need; // bits needed
  671. internal int lit;
  672. // if EXT or COPY, where and how much
  673. internal int bitsToGet; // bits to get for extra
  674. internal int dist; // distance back to copy from
  675. internal byte lbits; // ltree bits decoded per branch
  676. internal byte dbits; // dtree bits decoder per branch
  677. internal int[] ltree; // literal/length/eob tree
  678. internal int ltree_index; // literal/length/eob tree
  679. internal int[] dtree; // distance tree
  680. internal int dtree_index; // distance tree
  681. internal InflateCodes()
  682. {
  683. }
  684. internal void Init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index)
  685. {
  686. mode = START;
  687. lbits = (byte)bl;
  688. dbits = (byte)bd;
  689. ltree = tl;
  690. ltree_index = tl_index;
  691. dtree = td;
  692. dtree_index = td_index;
  693. tree = null;
  694. }
  695. internal int Process(InflateBlocks blocks, int r)
  696. {
  697. int j; // temporary storage
  698. int tindex; // temporary pointer
  699. int e; // extra bits or operation
  700. int b = 0; // bit buffer
  701. int k = 0; // bits in bit buffer
  702. int p = 0; // input data pointer
  703. int n; // bytes available there
  704. int q; // output window write pointer
  705. int m; // bytes to end of window or read pointer
  706. int f; // pointer to copy strings from
  707. ZlibCodec z = blocks._codec;
  708. // copy input/output information to locals (UPDATE macro restores)
  709. p = z.NextIn;
  710. n = z.AvailableBytesIn;
  711. b = blocks.bitb;
  712. k = blocks.bitk;
  713. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  714. // process input and output based on current state
  715. while (true)
  716. {
  717. switch (mode)
  718. {
  719. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  720. case START: // x: set up for LEN
  721. if (m >= 258 && n >= 10)
  722. {
  723. blocks.bitb = b; blocks.bitk = k;
  724. z.AvailableBytesIn = n;
  725. z.TotalBytesIn += p - z.NextIn;
  726. z.NextIn = p;
  727. blocks.writeAt = q;
  728. r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);
  729. p = z.NextIn;
  730. n = z.AvailableBytesIn;
  731. b = blocks.bitb;
  732. k = blocks.bitk;
  733. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  734. if (r != ZlibConstants.Z_OK)
  735. {
  736. mode = (r == ZlibConstants.Z_STREAM_END) ? WASH : BADCODE;
  737. break;
  738. }
  739. }
  740. need = lbits;
  741. tree = ltree;
  742. tree_index = ltree_index;
  743. mode = LEN;
  744. goto case LEN;
  745. case LEN: // i: get length/literal/eob next
  746. j = need;
  747. while (k < j)
  748. {
  749. if (n != 0)
  750. r = ZlibConstants.Z_OK;
  751. else
  752. {
  753. blocks.bitb = b; blocks.bitk = k;
  754. z.AvailableBytesIn = n;
  755. z.TotalBytesIn += p - z.NextIn;
  756. z.NextIn = p;
  757. blocks.writeAt = q;
  758. return blocks.Flush(r);
  759. }
  760. n--;
  761. b |= (z.InputBuffer[p++] & 0xff) << k;
  762. k += 8;
  763. }
  764. tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
  765. b >>= (tree[tindex + 1]);
  766. k -= (tree[tindex + 1]);
  767. e = tree[tindex];
  768. if (e == 0)
  769. {
  770. // literal
  771. lit = tree[tindex + 2];
  772. mode = LIT;
  773. break;
  774. }
  775. if ((e & 16) != 0)
  776. {
  777. // length
  778. bitsToGet = e & 15;
  779. len = tree[tindex + 2];
  780. mode = LENEXT;
  781. break;
  782. }
  783. if ((e & 64) == 0)
  784. {
  785. // next table
  786. need = e;
  787. tree_index = tindex / 3 + tree[tindex + 2];
  788. break;
  789. }
  790. if ((e & 32) != 0)
  791. {
  792. // end of block
  793. mode = WASH;
  794. break;
  795. }
  796. mode = BADCODE; // invalid code
  797. z.Message = "invalid literal/length code";
  798. r = ZlibConstants.Z_DATA_ERROR;
  799. blocks.bitb = b; blocks.bitk = k;
  800. z.AvailableBytesIn = n;
  801. z.TotalBytesIn += p - z.NextIn;
  802. z.NextIn = p;
  803. blocks.writeAt = q;
  804. return blocks.Flush(r);
  805. case LENEXT: // i: getting length extra (have base)
  806. j = bitsToGet;
  807. while (k < j)
  808. {
  809. if (n != 0)
  810. r = ZlibConstants.Z_OK;
  811. else
  812. {
  813. blocks.bitb = b; blocks.bitk = k;
  814. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  815. blocks.writeAt = q;
  816. return blocks.Flush(r);
  817. }
  818. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  819. k += 8;
  820. }
  821. len += (b & InternalInflateConstants.InflateMask[j]);
  822. b >>= j;
  823. k -= j;
  824. need = dbits;
  825. tree = dtree;
  826. tree_index = dtree_index;
  827. mode = DIST;
  828. goto case DIST;
  829. case DIST: // i: get distance next
  830. j = need;
  831. while (k < j)
  832. {
  833. if (n != 0)
  834. r = ZlibConstants.Z_OK;
  835. else
  836. {
  837. blocks.bitb = b; blocks.bitk = k;
  838. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  839. blocks.writeAt = q;
  840. return blocks.Flush(r);
  841. }
  842. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  843. k += 8;
  844. }
  845. tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
  846. b >>= tree[tindex + 1];
  847. k -= tree[tindex + 1];
  848. e = (tree[tindex]);
  849. if ((e & 0x10) != 0)
  850. {
  851. // distance
  852. bitsToGet = e & 15;
  853. dist = tree[tindex + 2];
  854. mode = DISTEXT;
  855. break;
  856. }
  857. if ((e & 64) == 0)
  858. {
  859. // next table
  860. need = e;
  861. tree_index = tindex / 3 + tree[tindex + 2];
  862. break;
  863. }
  864. mode = BADCODE; // invalid code
  865. z.Message = "invalid distance code";
  866. r = ZlibConstants.Z_DATA_ERROR;
  867. blocks.bitb = b; blocks.bitk = k;
  868. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  869. blocks.writeAt = q;
  870. return blocks.Flush(r);
  871. case DISTEXT: // i: getting distance extra
  872. j = bitsToGet;
  873. while (k < j)
  874. {
  875. if (n != 0)
  876. r = ZlibConstants.Z_OK;
  877. else
  878. {
  879. blocks.bitb = b; blocks.bitk = k;
  880. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  881. blocks.writeAt = q;
  882. return blocks.Flush(r);
  883. }
  884. n--; b |= (z.InputBuffer[p++] & 0xff) << k;
  885. k += 8;
  886. }
  887. dist += (b & InternalInflateConstants.InflateMask[j]);
  888. b >>= j;
  889. k -= j;
  890. mode = COPY;
  891. goto case COPY;
  892. case COPY: // o: copying bytes in window, waiting for space
  893. f = q - dist;
  894. while (f < 0)
  895. {
  896. // modulo window size-"while" instead
  897. f += blocks.end; // of "if" handles invalid distances
  898. }
  899. while (len != 0)
  900. {
  901. if (m == 0)
  902. {
  903. if (q == blocks.end && blocks.readAt != 0)
  904. {
  905. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  906. }
  907. if (m == 0)
  908. {
  909. blocks.writeAt = q; r = blocks.Flush(r);
  910. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  911. if (q == blocks.end && blocks.readAt != 0)
  912. {
  913. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  914. }
  915. if (m == 0)
  916. {
  917. blocks.bitb = b; blocks.bitk = k;
  918. z.AvailableBytesIn = n;
  919. z.TotalBytesIn += p - z.NextIn;
  920. z.NextIn = p;
  921. blocks.writeAt = q;
  922. return blocks.Flush(r);
  923. }
  924. }
  925. }
  926. blocks.window[q++] = blocks.window[f++]; m--;
  927. if (f == blocks.end)
  928. f = 0;
  929. len--;
  930. }
  931. mode = START;
  932. break;
  933. case LIT: // o: got literal, waiting for output space
  934. if (m == 0)
  935. {
  936. if (q == blocks.end && blocks.readAt != 0)
  937. {
  938. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  939. }
  940. if (m == 0)
  941. {
  942. blocks.writeAt = q; r = blocks.Flush(r);
  943. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  944. if (q == blocks.end && blocks.readAt != 0)
  945. {
  946. q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  947. }
  948. if (m == 0)
  949. {
  950. blocks.bitb = b; blocks.bitk = k;
  951. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  952. blocks.writeAt = q;
  953. return blocks.Flush(r);
  954. }
  955. }
  956. }
  957. r = ZlibConstants.Z_OK;
  958. blocks.window[q++] = (byte)lit; m--;
  959. mode = START;
  960. break;
  961. case WASH: // o: got eob, possibly more output
  962. if (k > 7)
  963. {
  964. // return unused byte, if any
  965. k -= 8;
  966. n++;
  967. p--; // can always return one
  968. }
  969. blocks.writeAt = q; r = blocks.Flush(r);
  970. q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
  971. if (blocks.readAt != blocks.writeAt)
  972. {
  973. blocks.bitb = b; blocks.bitk = k;
  974. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  975. blocks.writeAt = q;
  976. return blocks.Flush(r);
  977. }
  978. mode = END;
  979. goto case END;
  980. case END:
  981. r = ZlibConstants.Z_STREAM_END;
  982. blocks.bitb = b; blocks.bitk = k;
  983. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  984. blocks.writeAt = q;
  985. return blocks.Flush(r);
  986. case BADCODE: // x: got error
  987. r = ZlibConstants.Z_DATA_ERROR;
  988. blocks.bitb = b; blocks.bitk = k;
  989. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  990. blocks.writeAt = q;
  991. return blocks.Flush(r);
  992. default:
  993. r = ZlibConstants.Z_STREAM_ERROR;
  994. blocks.bitb = b; blocks.bitk = k;
  995. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  996. blocks.writeAt = q;
  997. return blocks.Flush(r);
  998. }
  999. }
  1000. }
  1001. // Called with number of bytes left to write in window at least 258
  1002. // (the maximum string length) and number of input bytes available
  1003. // at least ten. The ten bytes are six bytes for the longest length/
  1004. // distance pair plus four bytes for overloading the bit buffer.
  1005. internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
  1006. {
  1007. int t; // temporary pointer
  1008. int[] tp; // temporary pointer
  1009. int tp_index; // temporary pointer
  1010. int e; // extra bits or operation
  1011. int b; // bit buffer
  1012. int k; // bits in bit buffer
  1013. int p; // input data pointer
  1014. int n; // bytes available there
  1015. int q; // output window write pointer
  1016. int m; // bytes to end of window or read pointer
  1017. int ml; // mask for literal/length tree
  1018. int md; // mask for distance tree
  1019. int c; // bytes to copy
  1020. int d; // distance back to copy from
  1021. int r; // copy source pointer
  1022. int tp_index_t_3; // (tp_index+t)*3
  1023. // load input, output, bit values
  1024. p = z.NextIn; n = z.AvailableBytesIn; b = s.bitb; k = s.bitk;
  1025. q = s.writeAt; m = q < s.readAt ? s.readAt - q - 1 : s.end - q;
  1026. // initialize masks
  1027. ml = InternalInflateConstants.InflateMask[bl];
  1028. md = InternalInflateConstants.InflateMask[bd];
  1029. // do until not enough input or output space for fast loop
  1030. do
  1031. {
  1032. // assume called with m >= 258 && n >= 10
  1033. // get literal/length code
  1034. while (k < (20))
  1035. {
  1036. // max bits for literal/length code
  1037. n--;
  1038. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1039. }
  1040. t = b & ml;
  1041. tp = tl;
  1042. tp_index = tl_index;
  1043. tp_index_t_3 = (tp_index + t) * 3;
  1044. if ((e = tp[tp_index_t_3]) == 0)
  1045. {
  1046. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1047. s.window[q++] = (byte)tp[tp_index_t_3 + 2];
  1048. m--;
  1049. continue;
  1050. }
  1051. do
  1052. {
  1053. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1054. if ((e & 16) != 0)
  1055. {
  1056. e &= 15;
  1057. c = tp[tp_index_t_3 + 2] + ((int)b & InternalInflateConstants.InflateMask[e]);
  1058. b >>= e; k -= e;
  1059. // decode distance base of block to copy
  1060. while (k < 15)
  1061. {
  1062. // max bits for distance code
  1063. n--;
  1064. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1065. }
  1066. t = b & md;
  1067. tp = td;
  1068. tp_index = td_index;
  1069. tp_index_t_3 = (tp_index + t) * 3;
  1070. e = tp[tp_index_t_3];
  1071. do
  1072. {
  1073. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1074. if ((e & 16) != 0)
  1075. {
  1076. // get extra bits to add to distance base
  1077. e &= 15;
  1078. while (k < e)
  1079. {
  1080. // get extra bits (up to 13)
  1081. n--;
  1082. b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
  1083. }
  1084. d = tp[tp_index_t_3 + 2] + (b & InternalInflateConstants.InflateMask[e]);
  1085. b >>= e; k -= e;
  1086. // do the copy
  1087. m -= c;
  1088. if (q >= d)
  1089. {
  1090. // offset before dest
  1091. // just copy
  1092. r = q - d;
  1093. if (q - r > 0 && 2 > (q - r))
  1094. {
  1095. s.window[q++] = s.window[r++]; // minimum count is three,
  1096. s.window[q++] = s.window[r++]; // so unroll loop a little
  1097. c -= 2;
  1098. }
  1099. else
  1100. {
  1101. Array.Copy(s.window, r, s.window, q, 2);
  1102. q += 2; r += 2; c -= 2;
  1103. }
  1104. }
  1105. else
  1106. {
  1107. // else offset after destination
  1108. r = q - d;
  1109. do
  1110. {
  1111. r += s.end; // force pointer in window
  1112. }
  1113. while (r < 0); // covers invalid distances
  1114. e = s.end - r;
  1115. if (c > e)
  1116. {
  1117. // if source crosses,
  1118. c -= e; // wrapped copy
  1119. if (q - r > 0 && e > (q - r))
  1120. {
  1121. do
  1122. {
  1123. s.window[q++] = s.window[r++];
  1124. }
  1125. while (--e != 0);
  1126. }
  1127. else
  1128. {
  1129. Array.Copy(s.window, r, s.window, q, e);
  1130. q += e; r += e; e = 0;
  1131. }
  1132. r = 0; // copy rest from start of window
  1133. }
  1134. }
  1135. // copy all or what's left
  1136. if (q - r > 0 && c > (q - r))
  1137. {
  1138. do
  1139. {
  1140. s.window[q++] = s.window[r++];
  1141. }
  1142. while (--c != 0);
  1143. }
  1144. else
  1145. {
  1146. Array.Copy(s.window, r, s.window, q, c);
  1147. q += c; r += c; c = 0;
  1148. }
  1149. break;
  1150. }
  1151. else if ((e & 64) == 0)
  1152. {
  1153. t += tp[tp_index_t_3 + 2];
  1154. t += (b & InternalInflateConstants.InflateMask[e]);
  1155. tp_index_t_3 = (tp_index + t) * 3;
  1156. e = tp[tp_index_t_3];
  1157. }
  1158. else
  1159. {
  1160. z.Message = "invalid distance code";
  1161. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1162. s.bitb = b; s.bitk = k;
  1163. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1164. s.writeAt = q;
  1165. return ZlibConstants.Z_DATA_ERROR;
  1166. }
  1167. }
  1168. while (true);
  1169. break;
  1170. }
  1171. if ((e & 64) == 0)
  1172. {
  1173. t += tp[tp_index_t_3 + 2];
  1174. t += (b & InternalInflateConstants.InflateMask[e]);
  1175. tp_index_t_3 = (tp_index + t) * 3;
  1176. if ((e = tp[tp_index_t_3]) == 0)
  1177. {
  1178. b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
  1179. s.window[q++] = (byte)tp[tp_index_t_3 + 2];
  1180. m--;
  1181. break;
  1182. }
  1183. }
  1184. else if ((e & 32) != 0)
  1185. {
  1186. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1187. s.bitb = b; s.bitk = k;
  1188. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1189. s.writeAt = q;
  1190. return ZlibConstants.Z_STREAM_END;
  1191. }
  1192. else
  1193. {
  1194. z.Message = "invalid literal/length code";
  1195. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1196. s.bitb = b; s.bitk = k;
  1197. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1198. s.writeAt = q;
  1199. return ZlibConstants.Z_DATA_ERROR;
  1200. }
  1201. }
  1202. while (true);
  1203. }
  1204. while (m >= 258 && n >= 10);
  1205. // not enough input or output--restore pointers and return
  1206. c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
  1207. s.bitb = b; s.bitk = k;
  1208. z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
  1209. s.writeAt = q;
  1210. return ZlibConstants.Z_OK;
  1211. }
  1212. }
  1213. internal sealed class InflateManager
  1214. {
  1215. // preset dictionary flag in zlib header
  1216. private const int PRESET_DICT = 0x20;
  1217. private const int Z_DEFLATED = 8;
  1218. private enum InflateManagerMode
  1219. {
  1220. METHOD = 0, // waiting for method byte
  1221. FLAG = 1, // waiting for flag byte
  1222. DICT4 = 2, // four dictionary check bytes to go
  1223. DICT3 = 3, // three dictionary check bytes to go
  1224. DICT2 = 4, // two dictionary check bytes to go
  1225. DICT1 = 5, // one dictionary check byte to go
  1226. DICT0 = 6, // waiting for inflateSetDictionary
  1227. BLOCKS = 7, // decompressing blocks
  1228. CHECK4 = 8, // four check bytes to go
  1229. CHECK3 = 9, // three check bytes to go
  1230. CHECK2 = 10, // two check bytes to go
  1231. CHECK1 = 11, // one check byte to go
  1232. DONE = 12, // finished check, done
  1233. BAD = 13, // got an error--stay here
  1234. }
  1235. private InflateManagerMode mode; // current inflate mode
  1236. internal ZlibCodec _codec; // pointer back to this zlib stream
  1237. // mode dependent information
  1238. internal int method; // if FLAGS, method byte
  1239. // if CHECK, check values to compare
  1240. internal uint computedCheck; // computed check value
  1241. internal uint expectedCheck; // stream check value
  1242. // if BAD, inflateSync's marker bytes count
  1243. internal int marker;
  1244. // mode independent information
  1245. //internal int nowrap; // flag for no wrapper
  1246. private bool _handleRfc1950HeaderBytes = true;
  1247. internal bool HandleRfc1950HeaderBytes
  1248. {
  1249. get { return _handleRfc1950HeaderBytes; }
  1250. set { _handleRfc1950HeaderBytes = value; }
  1251. }
  1252. internal int wbits; // log2(window size) (8..15, defaults to 15)
  1253. internal InflateBlocks blocks; // current inflate_blocks state
  1254. public InflateManager() { }
  1255. public InflateManager(bool expectRfc1950HeaderBytes)
  1256. {
  1257. _handleRfc1950HeaderBytes = expectRfc1950HeaderBytes;
  1258. }
  1259. internal int Reset()
  1260. {
  1261. _codec.TotalBytesIn = _codec.TotalBytesOut = 0;
  1262. _codec.Message = null;
  1263. mode = HandleRfc1950HeaderBytes ? InflateManagerMode.METHOD : InflateManagerMode.BLOCKS;
  1264. blocks.Reset();
  1265. return ZlibConstants.Z_OK;
  1266. }
  1267. internal int End()
  1268. {
  1269. if (blocks != null)
  1270. blocks.Free();
  1271. blocks = null;
  1272. return ZlibConstants.Z_OK;
  1273. }
  1274. internal int Initialize(ZlibCodec codec, int w)
  1275. {
  1276. _codec = codec;
  1277. _codec.Message = null;
  1278. blocks = null;
  1279. // handle undocumented nowrap option (no zlib header or check)
  1280. //nowrap = 0;
  1281. //if (w < 0)
  1282. //{
  1283. // w = - w;
  1284. // nowrap = 1;
  1285. //}
  1286. // set window size
  1287. if (w < 8 || w > 15)
  1288. {
  1289. End();
  1290. throw new ZlibException("Bad window size.");
  1291. //return ZlibConstants.Z_STREAM_ERROR;
  1292. }
  1293. wbits = w;
  1294. blocks = new InflateBlocks(codec,
  1295. HandleRfc1950HeaderBytes ? this : null,
  1296. 1 << w);
  1297. // reset state
  1298. Reset();
  1299. return ZlibConstants.Z_OK;
  1300. }
  1301. internal int Inflate(FlushType flush)
  1302. {
  1303. int b;
  1304. if (_codec.InputBuffer == null)
  1305. throw new ZlibException("InputBuffer is null. ");
  1306. // int f = (flush == FlushType.Finish)
  1307. // ? ZlibConstants.Z_BUF_ERROR
  1308. // : ZlibConstants.Z_OK;
  1309. // workitem 8870
  1310. int f = ZlibConstants.Z_OK;
  1311. int r = ZlibConstants.Z_BUF_ERROR;
  1312. while (true)
  1313. {
  1314. switch (mode)
  1315. {
  1316. case InflateManagerMode.METHOD:
  1317. if (_codec.AvailableBytesIn == 0) return r;
  1318. r = f;
  1319. _codec.AvailableBytesIn--;
  1320. _codec.TotalBytesIn++;
  1321. if (((method = _codec.InputBuffer[_codec.NextIn++]) & 0xf) != Z_DEFLATED)
  1322. {
  1323. mode = InflateManagerMode.BAD;
  1324. _codec.Message = String.Format("unknown compression method (0x{0:X2})", method);
  1325. marker = 5; // can't try inflateSync
  1326. break;
  1327. }
  1328. if ((method >> 4) + 8 > wbits)
  1329. {
  1330. mode = InflateManagerMode.BAD;
  1331. _codec.Message = String.Format("invalid window size ({0})", (method >> 4) + 8);
  1332. marker = 5; // can't try inflateSync
  1333. break;
  1334. }
  1335. mode = InflateManagerMode.FLAG;
  1336. break;
  1337. case InflateManagerMode.FLAG:
  1338. if (_codec.AvailableBytesIn == 0) return r;
  1339. r = f;
  1340. _codec.AvailableBytesIn--;
  1341. _codec.TotalBytesIn++;
  1342. b = (_codec.InputBuffer[_codec.NextIn++]) & 0xff;
  1343. if ((((method << 8) + b) % 31) != 0)
  1344. {
  1345. mode = InflateManagerMode.BAD;
  1346. _codec.Message = "incorrect header check";
  1347. marker = 5; // can't try inflateSync
  1348. break;
  1349. }
  1350. mode = ((b & PRESET_DICT) == 0)
  1351. ? InflateManagerMode.BLOCKS
  1352. : InflateManagerMode.DICT4;
  1353. break;
  1354. case InflateManagerMode.DICT4:
  1355. if (_codec.AvailableBytesIn == 0) return r;
  1356. r = f;
  1357. _codec.AvailableBytesIn--;
  1358. _codec.TotalBytesIn++;
  1359. expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
  1360. mode = InflateManagerMode.DICT3;
  1361. break;
  1362. case InflateManagerMode.DICT3:
  1363. if (_codec.AvailableBytesIn == 0) return r;
  1364. r = f;
  1365. _codec.AvailableBytesIn--;
  1366. _codec.TotalBytesIn++;
  1367. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
  1368. mode = InflateManagerMode.DICT2;
  1369. break;
  1370. case InflateManagerMode.DICT2:
  1371. if (_codec.AvailableBytesIn == 0) return r;
  1372. r = f;
  1373. _codec.AvailableBytesIn--;
  1374. _codec.TotalBytesIn++;
  1375. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
  1376. mode = InflateManagerMode.DICT1;
  1377. break;
  1378. case InflateManagerMode.DICT1:
  1379. if (_codec.AvailableBytesIn == 0) return r;
  1380. r = f;
  1381. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1382. expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
  1383. _codec._Adler32 = expectedCheck;
  1384. mode = InflateManagerMode.DICT0;
  1385. return ZlibConstants.Z_NEED_DICT;
  1386. case InflateManagerMode.DICT0:
  1387. mode = InflateManagerMode.BAD;
  1388. _codec.Message = "need dictionary";
  1389. marker = 0; // can try inflateSync
  1390. return ZlibConstants.Z_STREAM_ERROR;
  1391. case InflateManagerMode.BLOCKS:
  1392. r = blocks.Process(r);
  1393. if (r == ZlibConstants.Z_DATA_ERROR)
  1394. {
  1395. mode = InflateManagerMode.BAD;
  1396. marker = 0; // can try inflateSync
  1397. break;
  1398. }
  1399. if (r == ZlibConstants.Z_OK) r = f;
  1400. if (r != ZlibConstants.Z_STREAM_END)
  1401. return r;
  1402. r = f;
  1403. computedCheck = blocks.Reset();
  1404. if (!HandleRfc1950HeaderBytes)
  1405. {
  1406. mode = InflateManagerMode.DONE;
  1407. return ZlibConstants.Z_STREAM_END;
  1408. }
  1409. mode = InflateManagerMode.CHECK4;
  1410. break;
  1411. case InflateManagerMode.CHECK4:
  1412. if (_codec.AvailableBytesIn == 0) return r;
  1413. r = f;
  1414. _codec.AvailableBytesIn--;
  1415. _codec.TotalBytesIn++;
  1416. expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
  1417. mode = InflateManagerMode.CHECK3;
  1418. break;
  1419. case InflateManagerMode.CHECK3:
  1420. if (_codec.AvailableBytesIn == 0) return r;
  1421. r = f;
  1422. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1423. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
  1424. mode = InflateManagerMode.CHECK2;
  1425. break;
  1426. case InflateManagerMode.CHECK2:
  1427. if (_codec.AvailableBytesIn == 0) return r;
  1428. r = f;
  1429. _codec.AvailableBytesIn--;
  1430. _codec.TotalBytesIn++;
  1431. expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
  1432. mode = InflateManagerMode.CHECK1;
  1433. break;
  1434. case InflateManagerMode.CHECK1:
  1435. if (_codec.AvailableBytesIn == 0) return r;
  1436. r = f;
  1437. _codec.AvailableBytesIn--; _codec.TotalBytesIn++;
  1438. expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
  1439. if (computedCheck != expectedCheck)
  1440. {
  1441. mode = InflateManagerMode.BAD;
  1442. _codec.Message = "incorrect data check";
  1443. marker = 5; // can't try inflateSync
  1444. break;
  1445. }
  1446. mode = InflateManagerMode.DONE;
  1447. return ZlibConstants.Z_STREAM_END;
  1448. case InflateManagerMode.DONE:
  1449. return ZlibConstants.Z_STREAM_END;
  1450. case InflateManagerMode.BAD:
  1451. throw new ZlibException(String.Format("Bad state ({0})", _codec.Message));
  1452. default:
  1453. throw new ZlibException("Stream error.");
  1454. }
  1455. }
  1456. }
  1457. internal int SetDictionary(byte[] dictionary)
  1458. {
  1459. int index = 0;
  1460. int length = dictionary.Length;
  1461. if (mode != InflateManagerMode.DICT0)
  1462. throw new ZlibException("Stream error.");
  1463. if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
  1464. {
  1465. return ZlibConstants.Z_DATA_ERROR;
  1466. }
  1467. _codec._Adler32 = Adler.Adler32(0, null, 0, 0);
  1468. if (length >= (1 << wbits))
  1469. {
  1470. length = (1 << wbits) - 1;
  1471. index = dictionary.Length - length;
  1472. }
  1473. blocks.SetDictionary(dictionary, index, length);
  1474. mode = InflateManagerMode.BLOCKS;
  1475. return ZlibConstants.Z_OK;
  1476. }
  1477. private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
  1478. internal int Sync()
  1479. {
  1480. int n; // number of bytes to look at
  1481. int p; // pointer to bytes
  1482. int m; // number of marker bytes found in a row
  1483. long r, w; // temporaries to save total_in and total_out
  1484. // set up
  1485. if (mode != InflateManagerMode.BAD)
  1486. {
  1487. mode = InflateManagerMode.BAD;
  1488. marker = 0;
  1489. }
  1490. if ((n = _codec.AvailableBytesIn) == 0)
  1491. return ZlibConstants.Z_BUF_ERROR;
  1492. p = _codec.NextIn;
  1493. m = marker;
  1494. // search
  1495. while (n != 0 && m < 4)
  1496. {
  1497. if (_codec.InputBuffer[p] == mark[m])
  1498. {
  1499. m++;
  1500. }
  1501. else if (_codec.InputBuffer[p] != 0)
  1502. {
  1503. m = 0;
  1504. }
  1505. else
  1506. {
  1507. m = 4 - m;
  1508. }
  1509. p++; n--;
  1510. }
  1511. // restore
  1512. _codec.TotalBytesIn += p - _codec.NextIn;
  1513. _codec.NextIn = p;
  1514. _codec.AvailableBytesIn = n;
  1515. marker = m;
  1516. // return no joy or set up to restart on a new block
  1517. if (m != 4)
  1518. {
  1519. return ZlibConstants.Z_DATA_ERROR;
  1520. }
  1521. r = _codec.TotalBytesIn;
  1522. w = _codec.TotalBytesOut;
  1523. Reset();
  1524. _codec.TotalBytesIn = r;
  1525. _codec.TotalBytesOut = w;
  1526. mode = InflateManagerMode.BLOCKS;
  1527. return ZlibConstants.Z_OK;
  1528. }
  1529. // Returns true if inflate is currently at the end of a block generated
  1530. // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1531. // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  1532. // but removes the length bytes of the resulting empty stored block. When
  1533. // decompressing, PPP checks that at the end of input packet, inflate is
  1534. // waiting for these length bytes.
  1535. internal int SyncPoint(ZlibCodec z)
  1536. {
  1537. return blocks.SyncPoint();
  1538. }
  1539. }
  1540. }