TarInputStream.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // TarInputStream.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. // Linking this library statically or dynamically with other modules is
  20. // making a combined work based on this library. Thus, the terms and
  21. // conditions of the GNU General Public License cover the whole
  22. // combination.
  23. //
  24. // As a special exception, the copyright holders of this library give you
  25. // permission to link this library with independent modules to produce an
  26. // executable, regardless of the license terms of these independent
  27. // modules, and to copy and distribute the resulting executable under
  28. // terms of your choice, provided that you also meet, for each linked
  29. // independent module, the terms and conditions of the license of that
  30. // module. An independent module is a module which is not derived from
  31. // or based on this library. If you modify this library, you may extend
  32. // this exception to your version of the library, but you are not
  33. // obligated to do so. If you do not wish to do so, delete this
  34. // exception statement from your version.
  35. using System;
  36. using System.IO;
  37. using System.Text;
  38. namespace CommonMPQ.SharpZipLib.Tar
  39. {
  40. /// <summary>
  41. /// The TarInputStream reads a UNIX tar archive as an InputStream.
  42. /// methods are provided to position at each successive entry in
  43. /// the archive, and the read each entry as a normal input stream
  44. /// using read().
  45. /// </summary>
  46. public class TarInputStream : Stream
  47. {
  48. #region Constructors
  49. /// <summary>
  50. /// Construct a TarInputStream with default block factor
  51. /// </summary>
  52. /// <param name="inputStream">stream to source data from</param>
  53. public TarInputStream(Stream inputStream)
  54. : this(inputStream, TarBuffer.DefaultBlockFactor)
  55. {
  56. }
  57. /// <summary>
  58. /// Construct a TarInputStream with user specified block factor
  59. /// </summary>
  60. /// <param name="inputStream">stream to source data from</param>
  61. /// <param name="blockFactor">block factor to apply to archive</param>
  62. public TarInputStream(Stream inputStream, int blockFactor)
  63. {
  64. this.inputStream = inputStream;
  65. tarBuffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor);
  66. }
  67. #endregion
  68. /// <summary>
  69. /// Get/set flag indicating ownership of the underlying stream.
  70. /// When the flag is true <see cref="Close"></see> will close the underlying stream also.
  71. /// </summary>
  72. public bool IsStreamOwner
  73. {
  74. get { return tarBuffer.IsStreamOwner; }
  75. set { tarBuffer.IsStreamOwner = value; }
  76. }
  77. #region Stream Overrides
  78. /// <summary>
  79. /// Gets a value indicating whether the current stream supports reading
  80. /// </summary>
  81. public override bool CanRead
  82. {
  83. get {
  84. return inputStream.CanRead;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets a value indicating whether the current stream supports seeking
  89. /// This property always returns false.
  90. /// </summary>
  91. public override bool CanSeek {
  92. get {
  93. return false;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets a value indicating if the stream supports writing.
  98. /// This property always returns false.
  99. /// </summary>
  100. public override bool CanWrite {
  101. get {
  102. return false;
  103. }
  104. }
  105. /// <summary>
  106. /// The length in bytes of the stream
  107. /// </summary>
  108. public override long Length {
  109. get {
  110. return inputStream.Length;
  111. }
  112. }
  113. /// <summary>
  114. /// Gets or sets the position within the stream.
  115. /// Setting the Position is not supported and throws a NotSupportedExceptionNotSupportedException
  116. /// </summary>
  117. /// <exception cref="NotSupportedException">Any attempt to set position</exception>
  118. public override long Position {
  119. get {
  120. return inputStream.Position;
  121. }
  122. set {
  123. throw new NotSupportedException("TarInputStream Seek not supported");
  124. }
  125. }
  126. /// <summary>
  127. /// Flushes the baseInputStream
  128. /// </summary>
  129. public override void Flush()
  130. {
  131. inputStream.Flush();
  132. }
  133. /// <summary>
  134. /// Set the streams position. This operation is not supported and will throw a NotSupportedException
  135. /// </summary>
  136. /// <param name="offset">The offset relative to the origin to seek to.</param>
  137. /// <param name="origin">The <see cref="SeekOrigin"/> to start seeking from.</param>
  138. /// <returns>The new position in the stream.</returns>
  139. /// <exception cref="NotSupportedException">Any access</exception>
  140. public override long Seek(long offset, SeekOrigin origin)
  141. {
  142. throw new NotSupportedException("TarInputStream Seek not supported");
  143. }
  144. /// <summary>
  145. /// Sets the length of the stream
  146. /// This operation is not supported and will throw a NotSupportedException
  147. /// </summary>
  148. /// <param name="value">The new stream length.</param>
  149. /// <exception cref="NotSupportedException">Any access</exception>
  150. public override void SetLength(long value)
  151. {
  152. throw new NotSupportedException("TarInputStream SetLength not supported");
  153. }
  154. /// <summary>
  155. /// Writes a block of bytes to this stream using data from a buffer.
  156. /// This operation is not supported and will throw a NotSupportedException
  157. /// </summary>
  158. /// <param name="buffer">The buffer containing bytes to write.</param>
  159. /// <param name="offset">The offset in the buffer of the frist byte to write.</param>
  160. /// <param name="count">The number of bytes to write.</param>
  161. /// <exception cref="NotSupportedException">Any access</exception>
  162. public override void Write(byte[] buffer, int offset, int count)
  163. {
  164. throw new NotSupportedException("TarInputStream Write not supported");
  165. }
  166. /// <summary>
  167. /// Writes a byte to the current position in the file stream.
  168. /// This operation is not supported and will throw a NotSupportedException
  169. /// </summary>
  170. /// <param name="value">The byte value to write.</param>
  171. /// <exception cref="NotSupportedException">Any access</exception>
  172. public override void WriteByte(byte value)
  173. {
  174. throw new NotSupportedException("TarInputStream WriteByte not supported");
  175. }
  176. /// <summary>
  177. /// Reads a byte from the current tar archive entry.
  178. /// </summary>
  179. /// <returns>A byte cast to an int; -1 if the at the end of the stream.</returns>
  180. public override int ReadByte()
  181. {
  182. byte[] oneByteBuffer = new byte[1];
  183. int num = Read(oneByteBuffer, 0, 1);
  184. if (num <= 0)
  185. {
  186. // return -1 to indicate that no byte was read.
  187. return -1;
  188. }
  189. return oneByteBuffer[0];
  190. }
  191. /// <summary>
  192. /// Reads bytes from the current tar archive entry.
  193. ///
  194. /// This method is aware of the boundaries of the current
  195. /// entry in the archive and will deal with them appropriately
  196. /// </summary>
  197. /// <param name="buffer">
  198. /// The buffer into which to place bytes read.
  199. /// </param>
  200. /// <param name="offset">
  201. /// The offset at which to place bytes read.
  202. /// </param>
  203. /// <param name="count">
  204. /// The number of bytes to read.
  205. /// </param>
  206. /// <returns>
  207. /// The number of bytes read, or 0 at end of stream/EOF.
  208. /// </returns>
  209. public override int Read(byte[] buffer, int offset, int count)
  210. {
  211. if ( buffer == null )
  212. {
  213. throw new ArgumentNullException("buffer");
  214. }
  215. int totalRead = 0;
  216. if (entryOffset >= entrySize)
  217. {
  218. return 0;
  219. }
  220. long numToRead = count;
  221. if ((numToRead + entryOffset) > entrySize)
  222. {
  223. numToRead = entrySize - entryOffset;
  224. }
  225. if (readBuffer != null)
  226. {
  227. int sz = (numToRead > readBuffer.Length) ? readBuffer.Length : (int)numToRead;
  228. Array.Copy(readBuffer, 0, buffer, offset, sz);
  229. if (sz >= readBuffer.Length)
  230. {
  231. readBuffer = null;
  232. }
  233. else
  234. {
  235. int newLen = readBuffer.Length - sz;
  236. byte[] newBuf = new byte[newLen];
  237. Array.Copy(readBuffer, sz, newBuf, 0, newLen);
  238. readBuffer = newBuf;
  239. }
  240. totalRead += sz;
  241. numToRead -= sz;
  242. offset += sz;
  243. }
  244. while (numToRead > 0)
  245. {
  246. byte[] rec = tarBuffer.ReadBlock();
  247. if (rec == null)
  248. {
  249. // Unexpected EOF!
  250. throw new TarException("unexpected EOF with " + numToRead + " bytes unread");
  251. }
  252. int sz = (int)numToRead;
  253. int recLen = rec.Length;
  254. if (recLen > sz)
  255. {
  256. Array.Copy(rec, 0, buffer, offset, sz);
  257. readBuffer = new byte[recLen - sz];
  258. Array.Copy(rec, sz, readBuffer, 0, recLen - sz);
  259. }
  260. else
  261. {
  262. sz = recLen;
  263. Array.Copy(rec, 0, buffer, offset, recLen);
  264. }
  265. totalRead += sz;
  266. numToRead -= sz;
  267. offset += sz;
  268. }
  269. entryOffset += totalRead;
  270. return totalRead;
  271. }
  272. /// <summary>
  273. /// Closes this stream. Calls the TarBuffer's close() method.
  274. /// The underlying stream is closed by the TarBuffer.
  275. /// </summary>
  276. public override void Close()
  277. {
  278. tarBuffer.Close();
  279. }
  280. #endregion
  281. /// <summary>
  282. /// Set the entry factory for this instance.
  283. /// </summary>
  284. /// <param name="factory">The factory for creating new entries</param>
  285. public void SetEntryFactory(IEntryFactory factory)
  286. {
  287. entryFactory = factory;
  288. }
  289. /// <summary>
  290. /// Get the record size being used by this stream's TarBuffer.
  291. /// </summary>
  292. public int RecordSize
  293. {
  294. get { return tarBuffer.RecordSize; }
  295. }
  296. /// <summary>
  297. /// Get the record size being used by this stream's TarBuffer.
  298. /// </summary>
  299. /// <returns>
  300. /// TarBuffer record size.
  301. /// </returns>
  302. [Obsolete("Use RecordSize property instead")]
  303. public int GetRecordSize()
  304. {
  305. return tarBuffer.RecordSize;
  306. }
  307. /// <summary>
  308. /// Get the available data that can be read from the current
  309. /// entry in the archive. This does not indicate how much data
  310. /// is left in the entire archive, only in the current entry.
  311. /// This value is determined from the entry's size header field
  312. /// and the amount of data already read from the current entry.
  313. /// </summary>
  314. /// <returns>
  315. /// The number of available bytes for the current entry.
  316. /// </returns>
  317. public long Available {
  318. get {
  319. return entrySize - entryOffset;
  320. }
  321. }
  322. /// <summary>
  323. /// Skip bytes in the input buffer. This skips bytes in the
  324. /// current entry's data, not the entire archive, and will
  325. /// stop at the end of the current entry's data if the number
  326. /// to skip extends beyond that point.
  327. /// </summary>
  328. /// <param name="skipCount">
  329. /// The number of bytes to skip.
  330. /// </param>
  331. public void Skip(long skipCount)
  332. {
  333. // TODO: REVIEW efficiency of TarInputStream.Skip
  334. // This is horribly inefficient, but it ensures that we
  335. // properly skip over bytes via the TarBuffer...
  336. //
  337. byte[] skipBuf = new byte[8 * 1024];
  338. for (long num = skipCount; num > 0;) {
  339. int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num;
  340. int numRead = Read(skipBuf, 0, toRead);
  341. if (numRead == -1) {
  342. break;
  343. }
  344. num -= numRead;
  345. }
  346. }
  347. /// <summary>
  348. /// Return a value of true if marking is supported; false otherwise.
  349. /// </summary>
  350. /// <remarks>Currently marking is not supported, the return value is always false.</remarks>
  351. public bool IsMarkSupported {
  352. get {
  353. return false;
  354. }
  355. }
  356. /// <summary>
  357. /// Since we do not support marking just yet, we do nothing.
  358. /// </summary>
  359. /// <param name ="markLimit">
  360. /// The limit to mark.
  361. /// </param>
  362. public void Mark(int markLimit)
  363. {
  364. }
  365. /// <summary>
  366. /// Since we do not support marking just yet, we do nothing.
  367. /// </summary>
  368. public void Reset()
  369. {
  370. }
  371. /// <summary>
  372. /// Get the next entry in this tar archive. This will skip
  373. /// over any remaining data in the current entry, if there
  374. /// is one, and place the input stream at the header of the
  375. /// next entry, and read the header and instantiate a new
  376. /// TarEntry from the header bytes and return that entry.
  377. /// If there are no more entries in the archive, null will
  378. /// be returned to indicate that the end of the archive has
  379. /// been reached.
  380. /// </summary>
  381. /// <returns>
  382. /// The next TarEntry in the archive, or null.
  383. /// </returns>
  384. public TarEntry GetNextEntry()
  385. {
  386. if (hasHitEOF) {
  387. return null;
  388. }
  389. if (currentEntry != null) {
  390. SkipToNextEntry();
  391. }
  392. byte[] headerBuf = tarBuffer.ReadBlock();
  393. if (headerBuf == null) {
  394. hasHitEOF = true;
  395. } else if (TarBuffer.IsEndOfArchiveBlock(headerBuf)) {
  396. hasHitEOF = true;
  397. }
  398. if (hasHitEOF) {
  399. currentEntry = null;
  400. } else {
  401. try {
  402. TarHeader header = new TarHeader();
  403. header.ParseBuffer(headerBuf);
  404. if ( !header.IsChecksumValid )
  405. {
  406. throw new TarException("Header checksum is invalid");
  407. }
  408. this.entryOffset = 0;
  409. this.entrySize = header.Size;
  410. StringBuilder longName = null;
  411. if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) {
  412. byte[] nameBuffer = new byte[TarBuffer.BlockSize];
  413. long numToRead = this.entrySize;
  414. longName = new StringBuilder();
  415. while (numToRead > 0) {
  416. int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead));
  417. if (numRead == -1) {
  418. throw new InvalidHeaderException("Failed to read long name entry");
  419. }
  420. longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
  421. numToRead -= numRead;
  422. }
  423. SkipToNextEntry();
  424. headerBuf = this.tarBuffer.ReadBlock();
  425. } else if (header.TypeFlag == TarHeader.LF_GHDR) { // POSIX global extended header
  426. // Ignore things we dont understand completely for now
  427. SkipToNextEntry();
  428. headerBuf = this.tarBuffer.ReadBlock();
  429. } else if (header.TypeFlag == TarHeader.LF_XHDR) { // POSIX extended header
  430. // Ignore things we dont understand completely for now
  431. SkipToNextEntry();
  432. headerBuf = this.tarBuffer.ReadBlock();
  433. } else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) {
  434. // TODO: could show volume name when verbose
  435. SkipToNextEntry();
  436. headerBuf = this.tarBuffer.ReadBlock();
  437. } else if (header.TypeFlag != TarHeader.LF_NORMAL &&
  438. header.TypeFlag != TarHeader.LF_OLDNORM &&
  439. header.TypeFlag != TarHeader.LF_DIR) {
  440. // Ignore things we dont understand completely for now
  441. SkipToNextEntry();
  442. headerBuf = tarBuffer.ReadBlock();
  443. }
  444. if (entryFactory == null) {
  445. currentEntry = new TarEntry(headerBuf);
  446. if (longName != null) {
  447. currentEntry.Name = longName.ToString();
  448. }
  449. } else {
  450. currentEntry = entryFactory.CreateEntry(headerBuf);
  451. }
  452. // Magic was checked here for 'ustar' but there are multiple valid possibilities
  453. // so this is not done anymore.
  454. entryOffset = 0;
  455. // TODO: Review How do we resolve this discrepancy?!
  456. entrySize = this.currentEntry.Size;
  457. } catch (InvalidHeaderException ex) {
  458. entrySize = 0;
  459. entryOffset = 0;
  460. currentEntry = null;
  461. string errorText = string.Format("Bad header in record {0} block {1} {2}",
  462. tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, ex.Message);
  463. throw new InvalidHeaderException(errorText);
  464. }
  465. }
  466. return currentEntry;
  467. }
  468. /// <summary>
  469. /// Copies the contents of the current tar archive entry directly into
  470. /// an output stream.
  471. /// </summary>
  472. /// <param name="outputStream">
  473. /// The OutputStream into which to write the entry's data.
  474. /// </param>
  475. public void CopyEntryContents(Stream outputStream)
  476. {
  477. byte[] tempBuffer = new byte[32 * 1024];
  478. while (true) {
  479. int numRead = Read(tempBuffer, 0, tempBuffer.Length);
  480. if (numRead <= 0) {
  481. break;
  482. }
  483. outputStream.Write(tempBuffer, 0, numRead);
  484. }
  485. }
  486. void SkipToNextEntry()
  487. {
  488. long numToSkip = entrySize - entryOffset;
  489. if (numToSkip > 0)
  490. {
  491. Skip(numToSkip);
  492. }
  493. readBuffer = null;
  494. }
  495. /// <summary>
  496. /// This interface is provided, along with the method <see cref="SetEntryFactory"/>, to allow
  497. /// the programmer to have their own <see cref="TarEntry"/> subclass instantiated for the
  498. /// entries return from <see cref="GetNextEntry"/>.
  499. /// </summary>
  500. public interface IEntryFactory
  501. {
  502. /// <summary>
  503. /// Create an entry based on name alone
  504. /// </summary>
  505. /// <param name="name">
  506. /// Name of the new EntryPointNotFoundException to create
  507. /// </param>
  508. /// <returns>created TarEntry or descendant class</returns>
  509. TarEntry CreateEntry(string name);
  510. /// <summary>
  511. /// Create an instance based on an actual file
  512. /// </summary>
  513. /// <param name="fileName">
  514. /// Name of file to represent in the entry
  515. /// </param>
  516. /// <returns>
  517. /// Created TarEntry or descendant class
  518. /// </returns>
  519. TarEntry CreateEntryFromFile(string fileName);
  520. /// <summary>
  521. /// Create a tar entry based on the header information passed
  522. /// </summary>
  523. /// <param name="headerBuffer">
  524. /// Buffer containing header information to create an an entry from.
  525. /// </param>
  526. /// <returns>
  527. /// Created TarEntry or descendant class
  528. /// </returns>
  529. TarEntry CreateEntry(byte[] headerBuffer);
  530. }
  531. /// <summary>
  532. /// Standard entry factory class creating instances of the class TarEntry
  533. /// </summary>
  534. public class EntryFactoryAdapter : IEntryFactory
  535. {
  536. /// <summary>
  537. /// Create a <see cref="TarEntry"/> based on named
  538. /// </summary>
  539. /// <param name="name">The name to use for the entry</param>
  540. /// <returns>A new <see cref="TarEntry"/></returns>
  541. public TarEntry CreateEntry(string name)
  542. {
  543. return TarEntry.CreateTarEntry(name);
  544. }
  545. /// <summary>
  546. /// Create a tar entry with details obtained from <paramref name="fileName">file</paramref>
  547. /// </summary>
  548. /// <param name="fileName">The name of the file to retrieve details from.</param>
  549. /// <returns>A new <see cref="TarEntry"/></returns>
  550. public TarEntry CreateEntryFromFile(string fileName)
  551. {
  552. return TarEntry.CreateEntryFromFile(fileName);
  553. }
  554. /// <summary>
  555. /// Create an entry based on details in <paramref name="headerBuffer">header</paramref>
  556. /// </summary>
  557. /// <param name="headerBuffer">The buffer containing entry details.</param>
  558. /// <returns>A new <see cref="TarEntry"/></returns>
  559. public TarEntry CreateEntry(byte[] headerBuffer)
  560. {
  561. return new TarEntry(headerBuffer);
  562. }
  563. }
  564. #region Instance Fields
  565. /// <summary>
  566. /// Flag set when last block has been read
  567. /// </summary>
  568. protected bool hasHitEOF;
  569. /// <summary>
  570. /// Size of this entry as recorded in header
  571. /// </summary>
  572. protected long entrySize;
  573. /// <summary>
  574. /// Number of bytes read for this entry so far
  575. /// </summary>
  576. protected long entryOffset;
  577. /// <summary>
  578. /// Buffer used with calls to <code>Read()</code>
  579. /// </summary>
  580. protected byte[] readBuffer;
  581. /// <summary>
  582. /// Working buffer
  583. /// </summary>
  584. protected TarBuffer tarBuffer;
  585. /// <summary>
  586. /// Current entry being read
  587. /// </summary>
  588. TarEntry currentEntry;
  589. /// <summary>
  590. /// Factory used to create TarEntry or descendant class instance
  591. /// </summary>
  592. protected IEntryFactory entryFactory;
  593. /// <summary>
  594. /// Stream used as the source of input data.
  595. /// </summary>
  596. readonly Stream inputStream;
  597. #endregion
  598. }
  599. }
  600. /* The original Java file had this header:
  601. ** Authored by Timothy Gerard Endres
  602. ** <mailto:time@gjt.org> <http://www.trustice.com>
  603. **
  604. ** This work has been placed into the public domain.
  605. ** You may use this work in any way and for any purpose you wish.
  606. **
  607. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
  608. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
  609. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
  610. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
  611. ** REDISTRIBUTION OF THIS SOFTWARE.
  612. **
  613. */