ZlibBaseStream.cs 22 KB

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