ZlibBaseStream.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // ZlibBaseStream.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: <2011-August-06 21:22:38>
  19. //
  20. // ------------------------------------------------------------------
  21. //
  22. // This module defines the ZlibBaseStream class, which is an intnernal
  23. // base class for DeflateStream, ZlibStream and GZipStream.
  24. //
  25. // ------------------------------------------------------------------
  26. using System;
  27. using System.IO;
  28. namespace BestHTTP.Decompression.Zlib
  29. {
  30. internal enum ZlibStreamFlavor { ZLIB = 1950, DEFLATE = 1951, GZIP = 1952 }
  31. internal class ZlibBaseStream : System.IO.Stream
  32. {
  33. protected internal ZlibCodec _z = null; // deferred init... new ZlibCodec();
  34. protected internal StreamMode _streamMode = StreamMode.Undefined;
  35. protected internal FlushType _flushMode;
  36. protected internal ZlibStreamFlavor _flavor;
  37. protected internal CompressionMode _compressionMode;
  38. protected internal CompressionLevel _level;
  39. protected internal bool _leaveOpen;
  40. protected internal byte[] _workingBuffer;
  41. protected internal int _bufferSize = ZlibConstants.WorkingBufferSizeDefault;
  42. protected internal int windowBitsMax;
  43. protected internal byte[] _buf1 = new byte[1];
  44. protected internal System.IO.Stream _stream;
  45. protected internal CompressionStrategy Strategy = CompressionStrategy.Default;
  46. // workitem 7159
  47. BestHTTP.Decompression.Crc.CRC32 crc;
  48. protected internal string _GzipFileName;
  49. protected internal string _GzipComment;
  50. protected internal DateTime _GzipMtime;
  51. protected internal int _gzipHeaderByteCount;
  52. internal int Crc32 { get { if (crc == null) return 0; return crc.Crc32Result; } }
  53. public ZlibBaseStream(System.IO.Stream stream,
  54. CompressionMode compressionMode,
  55. CompressionLevel level,
  56. ZlibStreamFlavor flavor,
  57. bool leaveOpen)
  58. :this(stream, compressionMode, level, flavor,leaveOpen, ZlibConstants.WindowBitsDefault)
  59. { }
  60. public ZlibBaseStream(System.IO.Stream stream,
  61. CompressionMode compressionMode,
  62. CompressionLevel level,
  63. ZlibStreamFlavor flavor,
  64. bool leaveOpen,
  65. int windowBits)
  66. : base()
  67. {
  68. this._flushMode = FlushType.None;
  69. //this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
  70. this._stream = stream;
  71. this._leaveOpen = leaveOpen;
  72. this._compressionMode = compressionMode;
  73. this._flavor = flavor;
  74. this._level = level;
  75. this.windowBitsMax = windowBits;
  76. // workitem 7159
  77. if (flavor == ZlibStreamFlavor.GZIP)
  78. {
  79. this.crc = new BestHTTP.Decompression.Crc.CRC32();
  80. }
  81. }
  82. protected internal bool _wantCompress
  83. {
  84. get
  85. {
  86. return (this._compressionMode == CompressionMode.Compress);
  87. }
  88. }
  89. private ZlibCodec z
  90. {
  91. get
  92. {
  93. if (_z == null)
  94. {
  95. bool wantRfc1950Header = (this._flavor == ZlibStreamFlavor.ZLIB);
  96. _z = new ZlibCodec();
  97. if (this._compressionMode == CompressionMode.Decompress)
  98. {
  99. _z.InitializeInflate(this.windowBitsMax, wantRfc1950Header);
  100. }
  101. else
  102. {
  103. _z.Strategy = Strategy;
  104. _z.InitializeDeflate(this._level, this.windowBitsMax, wantRfc1950Header);
  105. }
  106. }
  107. return _z;
  108. }
  109. }
  110. private byte[] workingBuffer
  111. {
  112. get
  113. {
  114. if (_workingBuffer == null)
  115. _workingBuffer = Extensions.VariableSizedBufferPool.Get(_bufferSize, true);
  116. return _workingBuffer;
  117. }
  118. }
  119. public override void Write(System.Byte[] buffer, int offset, int count)
  120. {
  121. // workitem 7159
  122. // calculate the CRC on the unccompressed data (before writing)
  123. if (crc != null)
  124. crc.SlurpBlock(buffer, offset, count);
  125. if (_streamMode == StreamMode.Undefined)
  126. _streamMode = StreamMode.Writer;
  127. else if (_streamMode != StreamMode.Writer)
  128. throw new ZlibException("Cannot Write after Reading.");
  129. if (count == 0)
  130. return;
  131. // first reference of z property will initialize the private var _z
  132. z.InputBuffer = buffer;
  133. _z.NextIn = offset;
  134. _z.AvailableBytesIn = count;
  135. bool done = false;
  136. do
  137. {
  138. _z.OutputBuffer = workingBuffer;
  139. _z.NextOut = 0;
  140. _z.AvailableBytesOut = _workingBuffer.Length;
  141. int rc = (_wantCompress)
  142. ? _z.Deflate(_flushMode)
  143. : _z.Inflate(_flushMode);
  144. if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
  145. throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
  146. //if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
  147. _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
  148. done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
  149. // If GZIP and de-compress, we're done when 8 bytes remain.
  150. if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
  151. done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
  152. }
  153. while (!done);
  154. }
  155. private void finish()
  156. {
  157. if (_z == null) return;
  158. if (_streamMode == StreamMode.Writer)
  159. {
  160. bool done = false;
  161. do
  162. {
  163. _z.OutputBuffer = workingBuffer;
  164. _z.NextOut = 0;
  165. _z.AvailableBytesOut = _workingBuffer.Length;
  166. int rc = (_wantCompress)
  167. ? _z.Deflate(FlushType.Finish)
  168. : _z.Inflate(FlushType.Finish);
  169. if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
  170. {
  171. string verb = (_wantCompress ? "de" : "in") + "flating";
  172. if (_z.Message == null)
  173. throw new ZlibException(String.Format("{0}: (rc = {1})", verb, rc));
  174. else
  175. throw new ZlibException(verb + ": " + _z.Message);
  176. }
  177. if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
  178. {
  179. _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
  180. }
  181. done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
  182. // If GZIP and de-compress, we're done when 8 bytes remain.
  183. if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
  184. done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
  185. }
  186. while (!done);
  187. Flush();
  188. // workitem 7159
  189. if (_flavor == ZlibStreamFlavor.GZIP)
  190. {
  191. if (_wantCompress)
  192. {
  193. // Emit the GZIP trailer: CRC32 and size mod 2^32
  194. int c1 = crc.Crc32Result;
  195. _stream.Write(BitConverter.GetBytes(c1), 0, 4);
  196. int c2 = (Int32)(crc.TotalBytesRead & 0x00000000FFFFFFFF);
  197. _stream.Write(BitConverter.GetBytes(c2), 0, 4);
  198. }
  199. else
  200. {
  201. throw new ZlibException("Writing with decompression is not supported.");
  202. }
  203. }
  204. }
  205. // workitem 7159
  206. else if (_streamMode == StreamMode.Reader)
  207. {
  208. if (_flavor == ZlibStreamFlavor.GZIP)
  209. {
  210. if (!_wantCompress)
  211. {
  212. // workitem 8501: handle edge case (decompress empty stream)
  213. if (_z.TotalBytesOut == 0L)
  214. return;
  215. // Read and potentially verify the GZIP trailer:
  216. // CRC32 and size mod 2^32
  217. byte[] trailer = Extensions.VariableSizedBufferPool.Get(8, true);
  218. // workitems 8679 & 12554
  219. if (_z.AvailableBytesIn < 8)
  220. {
  221. // Make sure we have read to the end of the stream
  222. Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, _z.AvailableBytesIn);
  223. int bytesNeeded = 8 - _z.AvailableBytesIn;
  224. int bytesRead = _stream.Read(trailer,
  225. _z.AvailableBytesIn,
  226. bytesNeeded);
  227. if (bytesNeeded != bytesRead)
  228. {
  229. Extensions.VariableSizedBufferPool.Release(trailer);
  230. throw new ZlibException(String.Format("Missing or incomplete GZIP trailer. Expected 8 bytes, got {0}.",
  231. _z.AvailableBytesIn + bytesRead));
  232. }
  233. }
  234. else
  235. {
  236. Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, 8);
  237. }
  238. Int32 crc32_expected = BitConverter.ToInt32(trailer, 0);
  239. Int32 crc32_actual = crc.Crc32Result;
  240. Int32 isize_expected = BitConverter.ToInt32(trailer, 4);
  241. Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
  242. if (crc32_actual != crc32_expected)
  243. {
  244. Extensions.VariableSizedBufferPool.Release(trailer);
  245. throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
  246. }
  247. if (isize_actual != isize_expected)
  248. {
  249. Extensions.VariableSizedBufferPool.Release(trailer);
  250. throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
  251. }
  252. Extensions.VariableSizedBufferPool.Release(trailer);
  253. }
  254. else
  255. {
  256. throw new ZlibException("Reading with compression is not supported.");
  257. }
  258. }
  259. }
  260. }
  261. private void end()
  262. {
  263. if (z == null)
  264. return;
  265. if (_wantCompress)
  266. {
  267. _z.EndDeflate();
  268. }
  269. else
  270. {
  271. _z.EndInflate();
  272. }
  273. _z = null;
  274. Extensions.VariableSizedBufferPool.Release(_workingBuffer);
  275. _workingBuffer = null;
  276. }
  277. public
  278. #if !NETFX_CORE
  279. override
  280. #endif
  281. void Close()
  282. {
  283. if (_stream == null) return;
  284. try
  285. {
  286. finish();
  287. }
  288. finally
  289. {
  290. end();
  291. if (!_leaveOpen) _stream.Dispose();
  292. _stream = null;
  293. }
  294. }
  295. public override void Flush()
  296. {
  297. _stream.Flush();
  298. }
  299. public override System.Int64 Seek(System.Int64 offset, System.IO.SeekOrigin origin)
  300. {
  301. throw new NotImplementedException();
  302. //_outStream.Seek(offset, origin);
  303. }
  304. public override void SetLength(System.Int64 value)
  305. {
  306. _stream.SetLength(value);
  307. nomoreinput = false;
  308. }
  309. #if NOT
  310. public int Read()
  311. {
  312. if (Read(_buf1, 0, 1) == 0)
  313. return 0;
  314. // calculate CRC after reading
  315. if (crc!=null)
  316. crc.SlurpBlock(_buf1,0,1);
  317. return (_buf1[0] & 0xFF);
  318. }
  319. #endif
  320. private bool nomoreinput = false;
  321. private string ReadZeroTerminatedString()
  322. {
  323. var list = new System.Collections.Generic.List<byte>();
  324. bool done = false;
  325. do
  326. {
  327. // workitem 7740
  328. int n = _stream.Read(_buf1, 0, 1);
  329. if (n != 1)
  330. throw new ZlibException("Unexpected EOF reading GZIP header.");
  331. else
  332. {
  333. if (_buf1[0] == 0)
  334. done = true;
  335. else
  336. list.Add(_buf1[0]);
  337. }
  338. } while (!done);
  339. byte[] a = list.ToArray();
  340. return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
  341. }
  342. private int _ReadAndValidateGzipHeader()
  343. {
  344. int totalBytesRead = 0;
  345. // read the header on the first read
  346. byte[] header = Extensions.VariableSizedBufferPool.Get(10, true);
  347. int n = _stream.Read(header, 0, 10);
  348. // workitem 8501: handle edge case (decompress empty stream)
  349. if (n == 0)
  350. {
  351. Extensions.VariableSizedBufferPool.Release(header);
  352. return 0;
  353. }
  354. if (n != 10)
  355. {
  356. Extensions.VariableSizedBufferPool.Release(header);
  357. throw new ZlibException("Not a valid GZIP stream.");
  358. }
  359. if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
  360. {
  361. Extensions.VariableSizedBufferPool.Release(header);
  362. throw new ZlibException("Bad GZIP header.");
  363. }
  364. Int32 timet = BitConverter.ToInt32(header, 4);
  365. _GzipMtime = GZipStream._unixEpoch.AddSeconds(timet);
  366. totalBytesRead += n;
  367. if ((header[3] & 0x04) == 0x04)
  368. {
  369. // read and discard extra field
  370. n = _stream.Read(header, 0, 2); // 2-byte length field
  371. totalBytesRead += n;
  372. Int16 extraLength = (Int16)(header[0] + header[1] * 256);
  373. byte[] extra = Extensions.VariableSizedBufferPool.Get(extraLength, true);
  374. n = _stream.Read(extra, 0, extraLength);
  375. if (n != extraLength)
  376. {
  377. Extensions.VariableSizedBufferPool.Release(extra);
  378. Extensions.VariableSizedBufferPool.Release(header);
  379. throw new ZlibException("Unexpected end-of-file reading GZIP header.");
  380. }
  381. totalBytesRead += n;
  382. }
  383. if ((header[3] & 0x08) == 0x08)
  384. _GzipFileName = ReadZeroTerminatedString();
  385. if ((header[3] & 0x10) == 0x010)
  386. _GzipComment = ReadZeroTerminatedString();
  387. if ((header[3] & 0x02) == 0x02)
  388. Read(_buf1, 0, 1); // CRC16, ignore
  389. Extensions.VariableSizedBufferPool.Release(header);
  390. return totalBytesRead;
  391. }
  392. public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  393. {
  394. // According to MS documentation, any implementation of the IO.Stream.Read function must:
  395. // (a) throw an exception if offset & count reference an invalid part of the buffer,
  396. // or if count < 0, or if buffer is null
  397. // (b) return 0 only upon EOF, or if count = 0
  398. // (c) if not EOF, then return at least 1 byte, up to <count> bytes
  399. if (_streamMode == StreamMode.Undefined)
  400. {
  401. if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
  402. // for the first read, set up some controls.
  403. _streamMode = StreamMode.Reader;
  404. // (The first reference to _z goes through the private accessor which
  405. // may initialize it.)
  406. z.AvailableBytesIn = 0;
  407. if (_flavor == ZlibStreamFlavor.GZIP)
  408. {
  409. _gzipHeaderByteCount = _ReadAndValidateGzipHeader();
  410. // workitem 8501: handle edge case (decompress empty stream)
  411. if (_gzipHeaderByteCount == 0)
  412. return 0;
  413. }
  414. }
  415. if (_streamMode != StreamMode.Reader)
  416. throw new ZlibException("Cannot Read after Writing.");
  417. if (count == 0) return 0;
  418. if (nomoreinput && _wantCompress) return 0; // workitem 8557
  419. if (buffer == null) throw new ArgumentNullException("buffer");
  420. if (count < 0) throw new ArgumentOutOfRangeException("count");
  421. if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
  422. if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
  423. int rc = 0;
  424. // set up the output of the deflate/inflate codec:
  425. _z.OutputBuffer = buffer;
  426. _z.NextOut = offset;
  427. _z.AvailableBytesOut = count;
  428. // This is necessary in case _workingBuffer has been resized. (new byte[])
  429. // (The first reference to _workingBuffer goes through the private accessor which
  430. // may initialize it.)
  431. _z.InputBuffer = workingBuffer;
  432. do
  433. {
  434. // need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any.
  435. if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
  436. {
  437. // No data available, so try to Read data from the captive stream.
  438. _z.NextIn = 0;
  439. _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
  440. if (_z.AvailableBytesIn == 0)
  441. nomoreinput = true;
  442. }
  443. // we have data in InputBuffer; now compress or decompress as appropriate
  444. rc = (_wantCompress)
  445. ? _z.Deflate(_flushMode)
  446. : _z.Inflate(_flushMode);
  447. if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
  448. return 0;
  449. if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
  450. throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
  451. if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
  452. break; // nothing more to read
  453. }
  454. //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
  455. while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
  456. // workitem 8557
  457. // is there more room in output?
  458. if (_z.AvailableBytesOut > 0)
  459. {
  460. if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
  461. {
  462. // deferred
  463. }
  464. // are we completely done reading?
  465. if (nomoreinput)
  466. {
  467. // and in compression?
  468. if (_wantCompress)
  469. {
  470. // no more input data available; therefore we flush to
  471. // try to complete the read
  472. rc = _z.Deflate(FlushType.Finish);
  473. if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
  474. throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
  475. }
  476. }
  477. }
  478. rc = (count - _z.AvailableBytesOut);
  479. // calculate CRC after reading
  480. if (crc != null)
  481. crc.SlurpBlock(buffer, offset, rc);
  482. return rc;
  483. }
  484. public override System.Boolean CanRead
  485. {
  486. get { return this._stream.CanRead; }
  487. }
  488. public override System.Boolean CanSeek
  489. {
  490. get { return this._stream.CanSeek; }
  491. }
  492. public override System.Boolean CanWrite
  493. {
  494. get { return this._stream.CanWrite; }
  495. }
  496. public override System.Int64 Length
  497. {
  498. get { return _stream.Length; }
  499. }
  500. public override long Position
  501. {
  502. get { throw new NotImplementedException(); }
  503. set { throw new NotImplementedException(); }
  504. }
  505. internal enum StreamMode
  506. {
  507. Writer,
  508. Reader,
  509. Undefined,
  510. }
  511. }
  512. }