Inflate.cs 76 KB

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