ZipInputStream.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // ZipInputStream.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. // Copyright (C) 2004 John Reilly
  5. //
  6. // This file was translated from java, it was part of the GNU Classpath
  7. // Copyright (C) 2001 Free Software Foundation, Inc.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. //
  23. // Linking this library statically or dynamically with other modules is
  24. // making a combined work based on this library. Thus, the terms and
  25. // conditions of the GNU General Public License cover the whole
  26. // combination.
  27. //
  28. // As a special exception, the copyright holders of this library give you
  29. // permission to link this library with independent modules to produce an
  30. // executable, regardless of the license terms of these independent
  31. // modules, and to copy and distribute the resulting executable under
  32. // terms of your choice, provided that you also meet, for each linked
  33. // independent module, the terms and conditions of the license of that
  34. // module. An independent module is a module which is not derived from
  35. // or based on this library. If you modify this library, you may extend
  36. // this exception to your version of the library, but you are not
  37. // obligated to do so. If you do not wish to do so, delete this
  38. // exception statement from your version.
  39. // HISTORY
  40. // 2010-05-25 Z-1663 Fixed exception when testing local header compressed size of -1
  41. using System;
  42. using System.IO;
  43. using CommonMPQ.SharpZipLib.Checksums;
  44. using CommonMPQ.SharpZipLib.Zip.Compression;
  45. using CommonMPQ.SharpZipLib.Zip.Compression.Streams;
  46. #if !NETCF_1_0
  47. using CommonMPQ.SharpZipLib.Encryption;
  48. #endif
  49. namespace CommonMPQ.SharpZipLib.Zip
  50. {
  51. /// <summary>
  52. /// This is an InflaterInputStream that reads the files baseInputStream an zip archive
  53. /// one after another. It has a special method to get the zip entry of
  54. /// the next file. The zip entry contains information about the file name
  55. /// size, compressed size, Crc, etc.
  56. /// It includes support for Stored and Deflated entries.
  57. /// <br/>
  58. /// <br/>Author of the original java version : Jochen Hoenicke
  59. /// </summary>
  60. ///
  61. /// <example> This sample shows how to read a zip file
  62. /// <code lang="C#">
  63. /// using System;
  64. /// using System.Text;
  65. /// using System.IO;
  66. ///
  67. /// using CommonMPQ.SharpZipLib.Zip;
  68. ///
  69. /// class MainClass
  70. /// {
  71. /// public static void Main(string[] args)
  72. /// {
  73. /// using ( ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]))) {
  74. ///
  75. /// ZipEntry theEntry;
  76. /// const int size = 2048;
  77. /// byte[] data = new byte[2048];
  78. ///
  79. /// while ((theEntry = s.GetNextEntry()) != null) {
  80. /// if ( entry.IsFile ) {
  81. /// Console.Write("Show contents (y/n) ?");
  82. /// if (Console.ReadLine() == "y") {
  83. /// while (true) {
  84. /// size = s.Read(data, 0, data.Length);
  85. /// if (size > 0) {
  86. /// Console.Write(new ASCIIEncoding().GetString(data, 0, size));
  87. /// } else {
  88. /// break;
  89. /// }
  90. /// }
  91. /// }
  92. /// }
  93. /// }
  94. /// }
  95. /// }
  96. /// }
  97. /// </code>
  98. /// </example>
  99. public class ZipInputStream : InflaterInputStream
  100. {
  101. #region Instance Fields
  102. /// <summary>
  103. /// Delegate for reading bytes from a stream.
  104. /// </summary>
  105. delegate int ReadDataHandler(byte[] b, int offset, int length);
  106. /// <summary>
  107. /// The current reader this instance.
  108. /// </summary>
  109. ReadDataHandler internalReader;
  110. Crc32 crc = new Crc32();
  111. ZipEntry entry;
  112. long size;
  113. int method;
  114. int flags;
  115. string password;
  116. #endregion
  117. #region Constructors
  118. /// <summary>
  119. /// Creates a new Zip input stream, for reading a zip archive.
  120. /// </summary>
  121. /// <param name="baseInputStream">The underlying <see cref="Stream"/> providing data.</param>
  122. public ZipInputStream(Stream baseInputStream)
  123. : base(baseInputStream, new Inflater(true))
  124. {
  125. internalReader = new ReadDataHandler(ReadingNotAvailable);
  126. }
  127. /// <summary>
  128. /// Creates a new Zip input stream, for reading a zip archive.
  129. /// </summary>
  130. /// <param name="baseInputStream">The underlying <see cref="Stream"/> providing data.</param>
  131. /// <param name="bufferSize">Size of the buffer.</param>
  132. public ZipInputStream( Stream baseInputStream, int bufferSize )
  133. : base(baseInputStream, new Inflater(true), bufferSize)
  134. {
  135. internalReader = new ReadDataHandler(ReadingNotAvailable);
  136. }
  137. #endregion
  138. /// <summary>
  139. /// Optional password used for encryption when non-null
  140. /// </summary>
  141. /// <value>A password for all encrypted <see cref="ZipEntry">entries </see> in this <see cref="ZipInputStream"/></value>
  142. public string Password
  143. {
  144. get {
  145. return password;
  146. }
  147. set {
  148. password = value;
  149. }
  150. }
  151. /// <summary>
  152. /// Gets a value indicating if there is a current entry and it can be decompressed
  153. /// </summary>
  154. /// <remarks>
  155. /// The entry can only be decompressed if the library supports the zip features required to extract it.
  156. /// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details.
  157. /// </remarks>
  158. public bool CanDecompressEntry {
  159. get {
  160. return (entry != null) && entry.CanDecompress;
  161. }
  162. }
  163. /// <summary>
  164. /// Advances to the next entry in the archive
  165. /// </summary>
  166. /// <returns>
  167. /// The next <see cref="ZipEntry">entry</see> in the archive or null if there are no more entries.
  168. /// </returns>
  169. /// <remarks>
  170. /// If the previous entry is still open <see cref="CloseEntry">CloseEntry</see> is called.
  171. /// </remarks>
  172. /// <exception cref="InvalidOperationException">
  173. /// Input stream is closed
  174. /// </exception>
  175. /// <exception cref="ZipException">
  176. /// Password is not set, password is invalid, compression method is invalid,
  177. /// version required to extract is not supported
  178. /// </exception>
  179. public ZipEntry GetNextEntry()
  180. {
  181. if (crc == null) {
  182. throw new InvalidOperationException("Closed.");
  183. }
  184. if (entry != null) {
  185. CloseEntry();
  186. }
  187. int header = inputBuffer.ReadLeInt();
  188. if (header == ZipConstants.CentralHeaderSignature ||
  189. header == ZipConstants.EndOfCentralDirectorySignature ||
  190. header == ZipConstants.CentralHeaderDigitalSignature ||
  191. header == ZipConstants.ArchiveExtraDataSignature ||
  192. header == ZipConstants.Zip64CentralFileHeaderSignature) {
  193. // No more individual entries exist
  194. Close();
  195. return null;
  196. }
  197. // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found
  198. // Spanning signature is same as descriptor signature and is untested as yet.
  199. if ( (header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature) ) {
  200. header = inputBuffer.ReadLeInt();
  201. }
  202. if (header != ZipConstants.LocalHeaderSignature) {
  203. throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header));
  204. }
  205. short versionRequiredToExtract = (short)inputBuffer.ReadLeShort();
  206. flags = inputBuffer.ReadLeShort();
  207. method = inputBuffer.ReadLeShort();
  208. uint dostime = (uint)inputBuffer.ReadLeInt();
  209. int crc2 = inputBuffer.ReadLeInt();
  210. csize = inputBuffer.ReadLeInt();
  211. size = inputBuffer.ReadLeInt();
  212. int nameLen = inputBuffer.ReadLeShort();
  213. int extraLen = inputBuffer.ReadLeShort();
  214. bool isCrypted = (flags & 1) == 1;
  215. byte[] buffer = new byte[nameLen];
  216. inputBuffer.ReadRawBuffer(buffer);
  217. string name = ZipConstants.ConvertToStringExt(flags, buffer);
  218. entry = new ZipEntry(name, versionRequiredToExtract);
  219. entry.Flags = flags;
  220. entry.CompressionMethod = (CompressionMethod)method;
  221. if ((flags & 8) == 0) {
  222. entry.Crc = crc2 & 0xFFFFFFFFL;
  223. entry.Size = size & 0xFFFFFFFFL;
  224. entry.CompressedSize = csize & 0xFFFFFFFFL;
  225. entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff);
  226. } else {
  227. // This allows for GNU, WinZip and possibly other archives, the PKZIP spec
  228. // says these values are zero under these circumstances.
  229. if (crc2 != 0) {
  230. entry.Crc = crc2 & 0xFFFFFFFFL;
  231. }
  232. if (size != 0) {
  233. entry.Size = size & 0xFFFFFFFFL;
  234. }
  235. if (csize != 0) {
  236. entry.CompressedSize = csize & 0xFFFFFFFFL;
  237. }
  238. entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff);
  239. }
  240. entry.DosTime = dostime;
  241. // If local header requires Zip64 is true then the extended header should contain
  242. // both values.
  243. // Handle extra data if present. This can set/alter some fields of the entry.
  244. if (extraLen > 0) {
  245. byte[] extra = new byte[extraLen];
  246. inputBuffer.ReadRawBuffer(extra);
  247. entry.ExtraData = extra;
  248. }
  249. entry.ProcessExtraData(true);
  250. if ( entry.CompressedSize >= 0 ) {
  251. csize = entry.CompressedSize;
  252. }
  253. if ( entry.Size >= 0 ) {
  254. size = entry.Size;
  255. }
  256. if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) {
  257. throw new ZipException("Stored, but compressed != uncompressed");
  258. }
  259. // Determine how to handle reading of data if this is attempted.
  260. if (entry.IsCompressionMethodSupported()) {
  261. internalReader = new ReadDataHandler(InitialRead);
  262. } else {
  263. internalReader = new ReadDataHandler(ReadingNotSupported);
  264. }
  265. return entry;
  266. }
  267. /// <summary>
  268. /// Read data descriptor at the end of compressed data.
  269. /// </summary>
  270. void ReadDataDescriptor()
  271. {
  272. if (inputBuffer.ReadLeInt() != ZipConstants.DataDescriptorSignature) {
  273. throw new ZipException("Data descriptor signature not found");
  274. }
  275. entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL;
  276. if ( entry.LocalHeaderRequiresZip64 ) {
  277. csize = inputBuffer.ReadLeLong();
  278. size = inputBuffer.ReadLeLong();
  279. } else {
  280. csize = inputBuffer.ReadLeInt();
  281. size = inputBuffer.ReadLeInt();
  282. }
  283. entry.CompressedSize = csize;
  284. entry.Size = size;
  285. }
  286. /// <summary>
  287. /// Complete cleanup as the final part of closing.
  288. /// </summary>
  289. /// <param name="testCrc">True if the crc value should be tested</param>
  290. void CompleteCloseEntry(bool testCrc)
  291. {
  292. StopDecrypting();
  293. if ((flags & 8) != 0) {
  294. ReadDataDescriptor();
  295. }
  296. size = 0;
  297. if ( testCrc &&
  298. ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1)) {
  299. throw new ZipException("CRC mismatch");
  300. }
  301. crc.Reset();
  302. if (method == (int)CompressionMethod.Deflated) {
  303. inf.Reset();
  304. }
  305. entry = null;
  306. }
  307. /// <summary>
  308. /// Closes the current zip entry and moves to the next one.
  309. /// </summary>
  310. /// <exception cref="InvalidOperationException">
  311. /// The stream is closed
  312. /// </exception>
  313. /// <exception cref="ZipException">
  314. /// The Zip stream ends early
  315. /// </exception>
  316. public void CloseEntry()
  317. {
  318. if (crc == null) {
  319. throw new InvalidOperationException("Closed");
  320. }
  321. if (entry == null) {
  322. return;
  323. }
  324. if (method == (int)CompressionMethod.Deflated) {
  325. if ((flags & 8) != 0) {
  326. // We don't know how much we must skip, read until end.
  327. byte[] tmp = new byte[4096];
  328. // Read will close this entry
  329. while (Read(tmp, 0, tmp.Length) > 0) {
  330. }
  331. return;
  332. }
  333. csize -= inf.TotalIn;
  334. inputBuffer.Available += inf.RemainingInput;
  335. }
  336. if ( (inputBuffer.Available > csize) && (csize >= 0) ) {
  337. inputBuffer.Available = (int)((long)inputBuffer.Available - csize);
  338. } else {
  339. csize -= inputBuffer.Available;
  340. inputBuffer.Available = 0;
  341. while (csize != 0) {
  342. long skipped = base.Skip(csize);
  343. if (skipped <= 0) {
  344. throw new ZipException("Zip archive ends early.");
  345. }
  346. csize -= skipped;
  347. }
  348. }
  349. CompleteCloseEntry(false);
  350. }
  351. /// <summary>
  352. /// Returns 1 if there is an entry available
  353. /// Otherwise returns 0.
  354. /// </summary>
  355. public override int Available {
  356. get {
  357. return entry != null ? 1 : 0;
  358. }
  359. }
  360. /// <summary>
  361. /// Returns the current size that can be read from the current entry if available
  362. /// </summary>
  363. /// <exception cref="ZipException">Thrown if the entry size is not known.</exception>
  364. /// <exception cref="InvalidOperationException">Thrown if no entry is currently available.</exception>
  365. public override long Length
  366. {
  367. get {
  368. if ( entry != null ) {
  369. if ( entry.Size >= 0 ) {
  370. return entry.Size;
  371. } else {
  372. throw new ZipException("Length not available for the current entry");
  373. }
  374. }
  375. else {
  376. throw new InvalidOperationException("No current entry");
  377. }
  378. }
  379. }
  380. /// <summary>
  381. /// Reads a byte from the current zip entry.
  382. /// </summary>
  383. /// <returns>
  384. /// The byte or -1 if end of stream is reached.
  385. /// </returns>
  386. public override int ReadByte()
  387. {
  388. byte[] b = new byte[1];
  389. if (Read(b, 0, 1) <= 0) {
  390. return -1;
  391. }
  392. return b[0] & 0xff;
  393. }
  394. /// <summary>
  395. /// Handle attempts to read by throwing an <see cref="InvalidOperationException"/>.
  396. /// </summary>
  397. /// <param name="destination">The destination array to store data in.</param>
  398. /// <param name="offset">The offset at which data read should be stored.</param>
  399. /// <param name="count">The maximum number of bytes to read.</param>
  400. /// <returns>Returns the number of bytes actually read.</returns>
  401. int ReadingNotAvailable(byte[] destination, int offset, int count)
  402. {
  403. throw new InvalidOperationException("Unable to read from this stream");
  404. }
  405. /// <summary>
  406. /// Handle attempts to read from this entry by throwing an exception
  407. /// </summary>
  408. int ReadingNotSupported(byte[] destination, int offset, int count)
  409. {
  410. throw new ZipException("The compression method for this entry is not supported");
  411. }
  412. /// <summary>
  413. /// Perform the initial read on an entry which may include
  414. /// reading encryption headers and setting up inflation.
  415. /// </summary>
  416. /// <param name="destination">The destination to fill with data read.</param>
  417. /// <param name="offset">The offset to start reading at.</param>
  418. /// <param name="count">The maximum number of bytes to read.</param>
  419. /// <returns>The actual number of bytes read.</returns>
  420. int InitialRead(byte[] destination, int offset, int count)
  421. {
  422. if ( !CanDecompressEntry ) {
  423. throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version.ToString() + ")");
  424. }
  425. // Handle encryption if required.
  426. if (entry.IsCrypted) {
  427. #if NETCF_1_0
  428. throw new ZipException("Encryption not supported for Compact Framework 1.0");
  429. #else
  430. if (password == null) {
  431. throw new ZipException("No password set.");
  432. }
  433. // Generate and set crypto transform...
  434. PkzipClassicManaged managed = new PkzipClassicManaged();
  435. byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password));
  436. inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null);
  437. byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize];
  438. inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize);
  439. if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) {
  440. throw new ZipException("Invalid password");
  441. }
  442. if (csize >= ZipConstants.CryptoHeaderSize) {
  443. csize -= ZipConstants.CryptoHeaderSize;
  444. }
  445. else if ( (entry.Flags & (int)GeneralBitFlags.Descriptor) == 0 ) {
  446. throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize));
  447. }
  448. #endif
  449. } else {
  450. #if !NETCF_1_0
  451. inputBuffer.CryptoTransform = null;
  452. #endif
  453. }
  454. if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0)) {
  455. if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0)) {
  456. inputBuffer.SetInflaterInput(inf);
  457. }
  458. internalReader = new ReadDataHandler(BodyRead);
  459. return BodyRead(destination, offset, count);
  460. }
  461. else {
  462. internalReader = new ReadDataHandler(ReadingNotAvailable);
  463. return 0;
  464. }
  465. }
  466. /// <summary>
  467. /// Read a block of bytes from the stream.
  468. /// </summary>
  469. /// <param name="buffer">The destination for the bytes.</param>
  470. /// <param name="offset">The index to start storing data.</param>
  471. /// <param name="count">The number of bytes to attempt to read.</param>
  472. /// <returns>Returns the number of bytes read.</returns>
  473. /// <remarks>Zero bytes read means end of stream.</remarks>
  474. public override int Read(byte[] buffer, int offset, int count)
  475. {
  476. if ( buffer == null ) {
  477. throw new ArgumentNullException("buffer");
  478. }
  479. if ( offset < 0 ) {
  480. #if NETCF_1_0
  481. throw new ArgumentOutOfRangeException("offset");
  482. #else
  483. throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
  484. #endif
  485. }
  486. if ( count < 0 ) {
  487. #if NETCF_1_0
  488. throw new ArgumentOutOfRangeException("count");
  489. #else
  490. throw new ArgumentOutOfRangeException("count", "Cannot be negative");
  491. #endif
  492. }
  493. if ( (buffer.Length - offset) < count ) {
  494. throw new ArgumentException("Invalid offset/count combination");
  495. }
  496. return internalReader(buffer, offset, count);
  497. }
  498. /// <summary>
  499. /// Reads a block of bytes from the current zip entry.
  500. /// </summary>
  501. /// <returns>
  502. /// The number of bytes read (this may be less than the length requested, even before the end of stream), or 0 on end of stream.
  503. /// </returns>
  504. /// <exception name="IOException">
  505. /// An i/o error occured.
  506. /// </exception>
  507. /// <exception cref="ZipException">
  508. /// The deflated stream is corrupted.
  509. /// </exception>
  510. /// <exception cref="InvalidOperationException">
  511. /// The stream is not open.
  512. /// </exception>
  513. int BodyRead(byte[] buffer, int offset, int count)
  514. {
  515. if ( crc == null ) {
  516. throw new InvalidOperationException("Closed");
  517. }
  518. if ( (entry == null) || (count <= 0) ) {
  519. return 0;
  520. }
  521. if ( offset + count > buffer.Length ) {
  522. throw new ArgumentException("Offset + count exceeds buffer size");
  523. }
  524. bool finished = false;
  525. switch (method) {
  526. case (int)CompressionMethod.Deflated:
  527. count = base.Read(buffer, offset, count);
  528. if (count <= 0) {
  529. if (!inf.IsFinished) {
  530. throw new ZipException("Inflater not finished!");
  531. }
  532. inputBuffer.Available = inf.RemainingInput;
  533. // A csize of -1 is from an unpatched local header
  534. if ((flags & 8) == 0 &&
  535. (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size)) {
  536. throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut);
  537. }
  538. inf.Reset();
  539. finished = true;
  540. }
  541. break;
  542. case (int)CompressionMethod.Stored:
  543. if ( (count > csize) && (csize >= 0) ) {
  544. count = (int)csize;
  545. }
  546. if ( count > 0 ) {
  547. count = inputBuffer.ReadClearTextBuffer(buffer, offset, count);
  548. if (count > 0) {
  549. csize -= count;
  550. size -= count;
  551. }
  552. }
  553. if (csize == 0) {
  554. finished = true;
  555. } else {
  556. if (count < 0) {
  557. throw new ZipException("EOF in stored block");
  558. }
  559. }
  560. break;
  561. }
  562. if (count > 0) {
  563. crc.Update(buffer, offset, count);
  564. }
  565. if (finished) {
  566. CompleteCloseEntry(true);
  567. }
  568. return count;
  569. }
  570. /// <summary>
  571. /// Closes the zip input stream
  572. /// </summary>
  573. public override void Close()
  574. {
  575. internalReader = new ReadDataHandler(ReadingNotAvailable);
  576. crc = null;
  577. entry = null;
  578. base.Close();
  579. }
  580. }
  581. }