Inflate.cs 71 KB

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