ZlibBaseStream.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 = new byte[_bufferSize];
  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 = new byte[8];
  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. throw new ZlibException(String.Format("Missing or incomplete GZIP trailer. Expected 8 bytes, got {0}.",
  230. _z.AvailableBytesIn + bytesRead));
  231. }
  232. }
  233. else
  234. {
  235. Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, trailer.Length);
  236. }
  237. Int32 crc32_expected = BitConverter.ToInt32(trailer, 0);
  238. Int32 crc32_actual = crc.Crc32Result;
  239. Int32 isize_expected = BitConverter.ToInt32(trailer, 4);
  240. Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
  241. if (crc32_actual != crc32_expected)
  242. throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
  243. if (isize_actual != isize_expected)
  244. throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
  245. }
  246. else
  247. {
  248. throw new ZlibException("Reading with compression is not supported.");
  249. }
  250. }
  251. }
  252. }
  253. private void end()
  254. {
  255. if (z == null)
  256. return;
  257. if (_wantCompress)
  258. {
  259. _z.EndDeflate();
  260. }
  261. else
  262. {
  263. _z.EndInflate();
  264. }
  265. _z = null;
  266. }
  267. public
  268. #if !NETFX_CORE
  269. override
  270. #endif
  271. void Close()
  272. {
  273. if (_stream == null) return;
  274. try
  275. {
  276. finish();
  277. }
  278. finally
  279. {
  280. end();
  281. if (!_leaveOpen) _stream.Dispose();
  282. _stream = null;
  283. }
  284. }
  285. public override void Flush()
  286. {
  287. _stream.Flush();
  288. }
  289. public override System.Int64 Seek(System.Int64 offset, System.IO.SeekOrigin origin)
  290. {
  291. throw new NotImplementedException();
  292. //_outStream.Seek(offset, origin);
  293. }
  294. public override void SetLength(System.Int64 value)
  295. {
  296. _stream.SetLength(value);
  297. nomoreinput = false;
  298. }
  299. #if NOT
  300. public int Read()
  301. {
  302. if (Read(_buf1, 0, 1) == 0)
  303. return 0;
  304. // calculate CRC after reading
  305. if (crc!=null)
  306. crc.SlurpBlock(_buf1,0,1);
  307. return (_buf1[0] & 0xFF);
  308. }
  309. #endif
  310. private bool nomoreinput = false;
  311. private string ReadZeroTerminatedString()
  312. {
  313. var list = new System.Collections.Generic.List<byte>();
  314. bool done = false;
  315. do
  316. {
  317. // workitem 7740
  318. int n = _stream.Read(_buf1, 0, 1);
  319. if (n != 1)
  320. throw new ZlibException("Unexpected EOF reading GZIP header.");
  321. else
  322. {
  323. if (_buf1[0] == 0)
  324. done = true;
  325. else
  326. list.Add(_buf1[0]);
  327. }
  328. } while (!done);
  329. byte[] a = list.ToArray();
  330. return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
  331. }
  332. private int _ReadAndValidateGzipHeader()
  333. {
  334. int totalBytesRead = 0;
  335. // read the header on the first read
  336. byte[] header = new byte[10];
  337. int n = _stream.Read(header, 0, header.Length);
  338. // workitem 8501: handle edge case (decompress empty stream)
  339. if (n == 0)
  340. return 0;
  341. if (n != 10)
  342. throw new ZlibException("Not a valid GZIP stream.");
  343. if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
  344. throw new ZlibException("Bad GZIP header.");
  345. Int32 timet = BitConverter.ToInt32(header, 4);
  346. _GzipMtime = GZipStream._unixEpoch.AddSeconds(timet);
  347. totalBytesRead += n;
  348. if ((header[3] & 0x04) == 0x04)
  349. {
  350. // read and discard extra field
  351. n = _stream.Read(header, 0, 2); // 2-byte length field
  352. totalBytesRead += n;
  353. Int16 extraLength = (Int16)(header[0] + header[1] * 256);
  354. byte[] extra = new byte[extraLength];
  355. n = _stream.Read(extra, 0, extra.Length);
  356. if (n != extraLength)
  357. throw new ZlibException("Unexpected end-of-file reading GZIP header.");
  358. totalBytesRead += n;
  359. }
  360. if ((header[3] & 0x08) == 0x08)
  361. _GzipFileName = ReadZeroTerminatedString();
  362. if ((header[3] & 0x10) == 0x010)
  363. _GzipComment = ReadZeroTerminatedString();
  364. if ((header[3] & 0x02) == 0x02)
  365. Read(_buf1, 0, 1); // CRC16, ignore
  366. return totalBytesRead;
  367. }
  368. public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  369. {
  370. // According to MS documentation, any implementation of the IO.Stream.Read function must:
  371. // (a) throw an exception if offset & count reference an invalid part of the buffer,
  372. // or if count < 0, or if buffer is null
  373. // (b) return 0 only upon EOF, or if count = 0
  374. // (c) if not EOF, then return at least 1 byte, up to <count> bytes
  375. if (_streamMode == StreamMode.Undefined)
  376. {
  377. if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
  378. // for the first read, set up some controls.
  379. _streamMode = StreamMode.Reader;
  380. // (The first reference to _z goes through the private accessor which
  381. // may initialize it.)
  382. z.AvailableBytesIn = 0;
  383. if (_flavor == ZlibStreamFlavor.GZIP)
  384. {
  385. _gzipHeaderByteCount = _ReadAndValidateGzipHeader();
  386. // workitem 8501: handle edge case (decompress empty stream)
  387. if (_gzipHeaderByteCount == 0)
  388. return 0;
  389. }
  390. }
  391. if (_streamMode != StreamMode.Reader)
  392. throw new ZlibException("Cannot Read after Writing.");
  393. if (count == 0) return 0;
  394. if (nomoreinput && _wantCompress) return 0; // workitem 8557
  395. if (buffer == null) throw new ArgumentNullException("buffer");
  396. if (count < 0) throw new ArgumentOutOfRangeException("count");
  397. if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
  398. if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
  399. int rc = 0;
  400. // set up the output of the deflate/inflate codec:
  401. _z.OutputBuffer = buffer;
  402. _z.NextOut = offset;
  403. _z.AvailableBytesOut = count;
  404. // This is necessary in case _workingBuffer has been resized. (new byte[])
  405. // (The first reference to _workingBuffer goes through the private accessor which
  406. // may initialize it.)
  407. _z.InputBuffer = workingBuffer;
  408. do
  409. {
  410. // need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any.
  411. if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
  412. {
  413. // No data available, so try to Read data from the captive stream.
  414. _z.NextIn = 0;
  415. _z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
  416. if (_z.AvailableBytesIn == 0)
  417. nomoreinput = true;
  418. }
  419. // we have data in InputBuffer; now compress or decompress as appropriate
  420. rc = (_wantCompress)
  421. ? _z.Deflate(_flushMode)
  422. : _z.Inflate(_flushMode);
  423. if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
  424. return 0;
  425. if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
  426. throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
  427. if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
  428. break; // nothing more to read
  429. }
  430. //while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
  431. while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
  432. // workitem 8557
  433. // is there more room in output?
  434. if (_z.AvailableBytesOut > 0)
  435. {
  436. if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
  437. {
  438. // deferred
  439. }
  440. // are we completely done reading?
  441. if (nomoreinput)
  442. {
  443. // and in compression?
  444. if (_wantCompress)
  445. {
  446. // no more input data available; therefore we flush to
  447. // try to complete the read
  448. rc = _z.Deflate(FlushType.Finish);
  449. if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
  450. throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
  451. }
  452. }
  453. }
  454. rc = (count - _z.AvailableBytesOut);
  455. // calculate CRC after reading
  456. if (crc != null)
  457. crc.SlurpBlock(buffer, offset, rc);
  458. return rc;
  459. }
  460. public override System.Boolean CanRead
  461. {
  462. get { return this._stream.CanRead; }
  463. }
  464. public override System.Boolean CanSeek
  465. {
  466. get { return this._stream.CanSeek; }
  467. }
  468. public override System.Boolean CanWrite
  469. {
  470. get { return this._stream.CanWrite; }
  471. }
  472. public override System.Int64 Length
  473. {
  474. get { return _stream.Length; }
  475. }
  476. public override long Position
  477. {
  478. get { throw new NotImplementedException(); }
  479. set { throw new NotImplementedException(); }
  480. }
  481. internal enum StreamMode
  482. {
  483. Writer,
  484. Reader,
  485. Undefined,
  486. }
  487. public static void CompressString(String s, Stream compressor)
  488. {
  489. byte[] uncompressed = System.Text.Encoding.UTF8.GetBytes(s);
  490. using (compressor)
  491. {
  492. compressor.Write(uncompressed, 0, uncompressed.Length);
  493. }
  494. }
  495. public static void CompressBuffer(byte[] b, Stream compressor)
  496. {
  497. // workitem 8460
  498. using (compressor)
  499. {
  500. compressor.Write(b, 0, b.Length);
  501. }
  502. }
  503. public static String UncompressString(byte[] compressed, Stream decompressor)
  504. {
  505. // workitem 8460
  506. byte[] working = new byte[1024];
  507. var encoding = System.Text.Encoding.UTF8;
  508. using (var output = new MemoryStream())
  509. {
  510. using (decompressor)
  511. {
  512. int n;
  513. while ((n = decompressor.Read(working, 0, working.Length)) != 0)
  514. {
  515. output.Write(working, 0, n);
  516. }
  517. }
  518. // reset to allow read from start
  519. output.Seek(0, SeekOrigin.Begin);
  520. var sr = new StreamReader(output, encoding);
  521. return sr.ReadToEnd();
  522. }
  523. }
  524. public static byte[] UncompressBuffer(byte[] compressed, Stream decompressor)
  525. {
  526. // workitem 8460
  527. byte[] working = new byte[1024];
  528. using (var output = new MemoryStream())
  529. {
  530. using (decompressor)
  531. {
  532. int n;
  533. while ((n = decompressor.Read(working, 0, working.Length)) != 0)
  534. {
  535. output.Write(working, 0, n);
  536. }
  537. }
  538. return output.ToArray();
  539. }
  540. }
  541. }
  542. }