You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1156 lines
32 KiB

  1. // TarHeader.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. /* The tar format and its POSIX successor PAX have a long history which makes for compatability
  36. issues when creating and reading files.
  37. This is further complicated by a large number of programs with variations on formats
  38. One common issue is the handling of names longer than 100 characters.
  39. GNU style long names are currently supported.
  40. This is the ustar (Posix 1003.1) header.
  41. struct header
  42. {
  43. char t_name[100]; // 0 Filename
  44. char t_mode[8]; // 100 Permissions
  45. char t_uid[8]; // 108 Numerical User ID
  46. char t_gid[8]; // 116 Numerical Group ID
  47. char t_size[12]; // 124 Filesize
  48. char t_mtime[12]; // 136 st_mtime
  49. char t_chksum[8]; // 148 Checksum
  50. char t_typeflag; // 156 Type of File
  51. char t_linkname[100]; // 157 Target of Links
  52. char t_magic[6]; // 257 "ustar" or other...
  53. char t_version[2]; // 263 Version fixed to 00
  54. char t_uname[32]; // 265 User Name
  55. char t_gname[32]; // 297 Group Name
  56. char t_devmajor[8]; // 329 Major for devices
  57. char t_devminor[8]; // 337 Minor for devices
  58. char t_prefix[155]; // 345 Prefix for t_name
  59. char t_mfill[12]; // 500 Filler up to 512
  60. };
  61. */
  62. using System;
  63. using System.Text;
  64. namespace Externals.Compression.Tar
  65. {
  66. /// <summary>
  67. /// This class encapsulates the Tar Entry Header used in Tar Archives.
  68. /// The class also holds a number of tar constants, used mostly in headers.
  69. /// </summary>
  70. internal class TarHeader : ICloneable
  71. {
  72. #region Constants
  73. /// <summary>
  74. /// The length of the name field in a header buffer.
  75. /// </summary>
  76. public const int NAMELEN = 100;
  77. /// <summary>
  78. /// The length of the mode field in a header buffer.
  79. /// </summary>
  80. public const int MODELEN = 8;
  81. /// <summary>
  82. /// The length of the user id field in a header buffer.
  83. /// </summary>
  84. public const int UIDLEN = 8;
  85. /// <summary>
  86. /// The length of the group id field in a header buffer.
  87. /// </summary>
  88. public const int GIDLEN = 8;
  89. /// <summary>
  90. /// The length of the checksum field in a header buffer.
  91. /// </summary>
  92. public const int CHKSUMLEN = 8;
  93. /// <summary>
  94. /// Offset of checksum in a header buffer.
  95. /// </summary>
  96. public const int CHKSUMOFS = 148;
  97. /// <summary>
  98. /// The length of the size field in a header buffer.
  99. /// </summary>
  100. public const int SIZELEN = 12;
  101. /// <summary>
  102. /// The length of the magic field in a header buffer.
  103. /// </summary>
  104. public const int MAGICLEN = 6;
  105. /// <summary>
  106. /// The length of the version field in a header buffer.
  107. /// </summary>
  108. public const int VERSIONLEN = 2;
  109. /// <summary>
  110. /// The length of the modification time field in a header buffer.
  111. /// </summary>
  112. public const int MODTIMELEN = 12;
  113. /// <summary>
  114. /// The length of the user name field in a header buffer.
  115. /// </summary>
  116. public const int UNAMELEN = 32;
  117. /// <summary>
  118. /// The length of the group name field in a header buffer.
  119. /// </summary>
  120. public const int GNAMELEN = 32;
  121. /// <summary>
  122. /// The length of the devices field in a header buffer.
  123. /// </summary>
  124. public const int DEVLEN = 8;
  125. //
  126. // LF_ constants represent the "type" of an entry
  127. //
  128. /// <summary>
  129. /// The "old way" of indicating a normal file.
  130. /// </summary>
  131. public const byte LF_OLDNORM = 0;
  132. /// <summary>
  133. /// Normal file type.
  134. /// </summary>
  135. public const byte LF_NORMAL = (byte) '0';
  136. /// <summary>
  137. /// Link file type.
  138. /// </summary>
  139. public const byte LF_LINK = (byte) '1';
  140. /// <summary>
  141. /// Symbolic link file type.
  142. /// </summary>
  143. public const byte LF_SYMLINK = (byte) '2';
  144. /// <summary>
  145. /// Character device file type.
  146. /// </summary>
  147. public const byte LF_CHR = (byte) '3';
  148. /// <summary>
  149. /// Block device file type.
  150. /// </summary>
  151. public const byte LF_BLK = (byte) '4';
  152. /// <summary>
  153. /// Directory file type.
  154. /// </summary>
  155. public const byte LF_DIR = (byte) '5';
  156. /// <summary>
  157. /// FIFO (pipe) file type.
  158. /// </summary>
  159. public const byte LF_FIFO = (byte) '6';
  160. /// <summary>
  161. /// Contiguous file type.
  162. /// </summary>
  163. public const byte LF_CONTIG = (byte) '7';
  164. /// <summary>
  165. /// Posix.1 2001 global extended header
  166. /// </summary>
  167. public const byte LF_GHDR = (byte) 'g';
  168. /// <summary>
  169. /// Posix.1 2001 extended header
  170. /// </summary>
  171. public const byte LF_XHDR = (byte) 'x';
  172. // POSIX allows for upper case ascii type as extensions
  173. /// <summary>
  174. /// Solaris access control list file type
  175. /// </summary>
  176. public const byte LF_ACL = (byte) 'A';
  177. /// <summary>
  178. /// GNU dir dump file type
  179. /// This is a dir entry that contains the names of files that were in the
  180. /// dir at the time the dump was made
  181. /// </summary>
  182. public const byte LF_GNU_DUMPDIR = (byte) 'D';
  183. /// <summary>
  184. /// Solaris Extended Attribute File
  185. /// </summary>
  186. public const byte LF_EXTATTR = (byte) 'E' ;
  187. /// <summary>
  188. /// Inode (metadata only) no file content
  189. /// </summary>
  190. public const byte LF_META = (byte) 'I';
  191. /// <summary>
  192. /// Identifies the next file on the tape as having a long link name
  193. /// </summary>
  194. public const byte LF_GNU_LONGLINK = (byte) 'K';
  195. /// <summary>
  196. /// Identifies the next file on the tape as having a long name
  197. /// </summary>
  198. public const byte LF_GNU_LONGNAME = (byte) 'L';
  199. /// <summary>
  200. /// Continuation of a file that began on another volume
  201. /// </summary>
  202. public const byte LF_GNU_MULTIVOL = (byte) 'M';
  203. /// <summary>
  204. /// For storing filenames that dont fit in the main header (old GNU)
  205. /// </summary>
  206. public const byte LF_GNU_NAMES = (byte) 'N';
  207. /// <summary>
  208. /// GNU Sparse file
  209. /// </summary>
  210. public const byte LF_GNU_SPARSE = (byte) 'S';
  211. /// <summary>
  212. /// GNU Tape/volume header ignore on extraction
  213. /// </summary>
  214. public const byte LF_GNU_VOLHDR = (byte) 'V';
  215. /// <summary>
  216. /// The magic tag representing a POSIX tar archive. (includes trailing NULL)
  217. /// </summary>
  218. public const string TMAGIC = "ustar ";
  219. /// <summary>
  220. /// The magic tag representing an old GNU tar archive where version is included in magic and overwrites it
  221. /// </summary>
  222. public const string GNU_TMAGIC = "ustar ";
  223. const long timeConversionFactor = 10000000L; // 1 tick == 100 nanoseconds
  224. readonly static DateTime dateTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  225. #endregion
  226. #region Constructors
  227. /// <summary>
  228. /// Initialise a default TarHeader instance
  229. /// </summary>
  230. public TarHeader()
  231. {
  232. Magic = TMAGIC;
  233. Version = " ";
  234. Name = "";
  235. LinkName = "";
  236. UserId = defaultUserId;
  237. GroupId = defaultGroupId;
  238. UserName = defaultUser;
  239. GroupName = defaultGroupName;
  240. Size = 0;
  241. }
  242. #endregion
  243. #region Properties
  244. /// <summary>
  245. /// Get/set the name for this tar entry.
  246. /// </summary>
  247. /// <exception cref="ArgumentNullException">Thrown when attempting to set the property to null.</exception>
  248. public string Name
  249. {
  250. get { return name; }
  251. set {
  252. if ( value == null ) {
  253. throw new ArgumentNullException("value");
  254. }
  255. name = value;
  256. }
  257. }
  258. /// <summary>
  259. /// Get the name of this entry.
  260. /// </summary>
  261. /// <returns>The entry's name.</returns>
  262. [Obsolete("Use the Name property instead", true)]
  263. public string GetName()
  264. {
  265. return name;
  266. }
  267. /// <summary>
  268. /// Get/set the entry's Unix style permission mode.
  269. /// </summary>
  270. public int Mode
  271. {
  272. get { return mode; }
  273. set { mode = value; }
  274. }
  275. /// <summary>
  276. /// The entry's user id.
  277. /// </summary>
  278. /// <remarks>
  279. /// This is only directly relevant to unix systems.
  280. /// The default is zero.
  281. /// </remarks>
  282. public int UserId
  283. {
  284. get { return userId; }
  285. set { userId = value; }
  286. }
  287. /// <summary>
  288. /// Get/set the entry's group id.
  289. /// </summary>
  290. /// <remarks>
  291. /// This is only directly relevant to linux/unix systems.
  292. /// The default value is zero.
  293. /// </remarks>
  294. public int GroupId
  295. {
  296. get { return groupId; }
  297. set { groupId = value; }
  298. }
  299. /// <summary>
  300. /// Get/set the entry's size.
  301. /// </summary>
  302. /// <exception cref="ArgumentOutOfRangeException">Thrown when setting the size to less than zero.</exception>
  303. public long Size
  304. {
  305. get { return size; }
  306. set {
  307. if ( value < 0 ) {
  308. #if NETCF_1_0
  309. throw new ArgumentOutOfRangeException("value");
  310. #else
  311. throw new ArgumentOutOfRangeException("value", "Cannot be less than zero");
  312. #endif
  313. }
  314. size = value;
  315. }
  316. }
  317. /// <summary>
  318. /// Get/set the entry's modification time.
  319. /// </summary>
  320. /// <remarks>
  321. /// The modification time is only accurate to within a second.
  322. /// </remarks>
  323. /// <exception cref="ArgumentOutOfRangeException">Thrown when setting the date time to less than 1/1/1970.</exception>
  324. public DateTime ModTime
  325. {
  326. get { return modTime; }
  327. set {
  328. if ( value < dateTime1970 )
  329. {
  330. #if NETCF_1_0
  331. throw new ArgumentOutOfRangeException("value");
  332. #else
  333. throw new ArgumentOutOfRangeException("value", "ModTime cannot be before Jan 1st 1970");
  334. #endif
  335. }
  336. modTime = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second);
  337. }
  338. }
  339. /// <summary>
  340. /// Get the entry's checksum. This is only valid/updated after writing or reading an entry.
  341. /// </summary>
  342. public int Checksum
  343. {
  344. get { return checksum; }
  345. }
  346. /// <summary>
  347. /// Get value of true if the header checksum is valid, false otherwise.
  348. /// </summary>
  349. public bool IsChecksumValid
  350. {
  351. get { return isChecksumValid; }
  352. }
  353. /// <summary>
  354. /// Get/set the entry's type flag.
  355. /// </summary>
  356. public byte TypeFlag
  357. {
  358. get { return typeFlag; }
  359. set { typeFlag = value; }
  360. }
  361. /// <summary>
  362. /// The entry's link name.
  363. /// </summary>
  364. /// <exception cref="ArgumentNullException">Thrown when attempting to set LinkName to null.</exception>
  365. public string LinkName
  366. {
  367. get { return linkName; }
  368. set {
  369. if ( value == null ) {
  370. throw new ArgumentNullException("value");
  371. }
  372. linkName = value;
  373. }
  374. }
  375. /// <summary>
  376. /// Get/set the entry's magic tag.
  377. /// </summary>
  378. /// <exception cref="ArgumentNullException">Thrown when attempting to set Magic to null.</exception>
  379. public string Magic
  380. {
  381. get { return magic; }
  382. set {
  383. if ( value == null ) {
  384. throw new ArgumentNullException("value");
  385. }
  386. magic = value;
  387. }
  388. }
  389. /// <summary>
  390. /// The entry's version.
  391. /// </summary>
  392. /// <exception cref="ArgumentNullException">Thrown when attempting to set Version to null.</exception>
  393. public string Version
  394. {
  395. get {
  396. return version;
  397. }
  398. set {
  399. if ( value == null ) {
  400. throw new ArgumentNullException("value");
  401. }
  402. version = value;
  403. }
  404. }
  405. /// <summary>
  406. /// The entry's user name.
  407. /// </summary>
  408. public string UserName
  409. {
  410. get { return userName; }
  411. set {
  412. if (value != null) {
  413. userName = value.Substring(0, Math.Min(UNAMELEN, value.Length));
  414. }
  415. else {
  416. #if NETCF_1_0 || NETCF_2_0
  417. string currentUser = "PocketPC";
  418. #else
  419. string currentUser = Environment.UserName;
  420. #endif
  421. if (currentUser.Length > UNAMELEN) {
  422. currentUser = currentUser.Substring(0, UNAMELEN);
  423. }
  424. userName = currentUser;
  425. }
  426. }
  427. }
  428. /// <summary>
  429. /// Get/set the entry's group name.
  430. /// </summary>
  431. /// <remarks>
  432. /// This is only directly relevant to unix systems.
  433. /// </remarks>
  434. public string GroupName
  435. {
  436. get { return groupName; }
  437. set {
  438. if ( value == null ) {
  439. groupName = "None";
  440. }
  441. else {
  442. groupName = value;
  443. }
  444. }
  445. }
  446. /// <summary>
  447. /// Get/set the entry's major device number.
  448. /// </summary>
  449. public int DevMajor
  450. {
  451. get { return devMajor; }
  452. set { devMajor = value; }
  453. }
  454. /// <summary>
  455. /// Get/set the entry's minor device number.
  456. /// </summary>
  457. public int DevMinor
  458. {
  459. get { return devMinor; }
  460. set { devMinor = value; }
  461. }
  462. #endregion
  463. #region ICloneable Members
  464. /// <summary>
  465. /// Create a new <see cref="TarHeader"/> that is a copy of the current instance.
  466. /// </summary>
  467. /// <returns>A new <see cref="Object"/> that is a copy of the current instance.</returns>
  468. public object Clone()
  469. {
  470. return MemberwiseClone();
  471. }
  472. #endregion
  473. /// <summary>
  474. /// Parse TarHeader information from a header buffer.
  475. /// </summary>
  476. /// <param name = "header">
  477. /// The tar entry header buffer to get information from.
  478. /// </param>
  479. public void ParseBuffer(byte[] header)
  480. {
  481. if ( header == null )
  482. {
  483. throw new ArgumentNullException("header");
  484. }
  485. int offset = 0;
  486. name = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
  487. offset += TarHeader.NAMELEN;
  488. mode = (int)TarHeader.ParseOctal(header, offset, TarHeader.MODELEN);
  489. offset += TarHeader.MODELEN;
  490. UserId = (int)TarHeader.ParseOctal(header, offset, TarHeader.UIDLEN);
  491. offset += TarHeader.UIDLEN;
  492. GroupId = (int)TarHeader.ParseOctal(header, offset, TarHeader.GIDLEN);
  493. offset += TarHeader.GIDLEN;
  494. Size = TarHeader.ParseOctal(header, offset, TarHeader.SIZELEN);
  495. offset += TarHeader.SIZELEN;
  496. ModTime = GetDateTimeFromCTime(TarHeader.ParseOctal(header, offset, TarHeader.MODTIMELEN));
  497. offset += TarHeader.MODTIMELEN;
  498. checksum = (int)TarHeader.ParseOctal(header, offset, TarHeader.CHKSUMLEN);
  499. offset += TarHeader.CHKSUMLEN;
  500. TypeFlag = header[ offset++ ];
  501. LinkName = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
  502. offset += TarHeader.NAMELEN;
  503. Magic = TarHeader.ParseName(header, offset, TarHeader.MAGICLEN).ToString();
  504. offset += TarHeader.MAGICLEN;
  505. Version = TarHeader.ParseName(header, offset, TarHeader.VERSIONLEN).ToString();
  506. offset += TarHeader.VERSIONLEN;
  507. UserName = TarHeader.ParseName(header, offset, TarHeader.UNAMELEN).ToString();
  508. offset += TarHeader.UNAMELEN;
  509. GroupName = TarHeader.ParseName(header, offset, TarHeader.GNAMELEN).ToString();
  510. offset += TarHeader.GNAMELEN;
  511. DevMajor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
  512. offset += TarHeader.DEVLEN;
  513. DevMinor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
  514. // Fields past this point not currently parsed or used...
  515. isChecksumValid = Checksum == TarHeader.MakeCheckSum(header);
  516. }
  517. /// <summary>
  518. /// 'Write' header information to buffer provided, updating the <see cref="Checksum">check sum</see>.
  519. /// </summary>
  520. /// <param name="outBuffer">output buffer for header information</param>
  521. public void WriteHeader(byte[] outBuffer)
  522. {
  523. if ( outBuffer == null )
  524. {
  525. throw new ArgumentNullException("outBuffer");
  526. }
  527. int offset = 0;
  528. offset = GetNameBytes(Name, outBuffer, offset, NAMELEN);
  529. offset = GetOctalBytes(mode, outBuffer, offset, MODELEN);
  530. offset = GetOctalBytes(UserId, outBuffer, offset, UIDLEN);
  531. offset = GetOctalBytes(GroupId, outBuffer, offset, GIDLEN);
  532. offset = GetLongOctalBytes(Size, outBuffer, offset, SIZELEN);
  533. offset = GetLongOctalBytes(GetCTime(ModTime), outBuffer, offset, MODTIMELEN);
  534. int csOffset = offset;
  535. for (int c = 0; c < CHKSUMLEN; ++c)
  536. {
  537. outBuffer[offset++] = (byte)' ';
  538. }
  539. outBuffer[offset++] = TypeFlag;
  540. offset = GetNameBytes(LinkName, outBuffer, offset, NAMELEN);
  541. offset = GetAsciiBytes(Magic, 0, outBuffer, offset, MAGICLEN);
  542. offset = GetNameBytes(Version, outBuffer, offset, VERSIONLEN);
  543. offset = GetNameBytes(UserName, outBuffer, offset, UNAMELEN);
  544. offset = GetNameBytes(GroupName, outBuffer, offset, GNAMELEN);
  545. if ((TypeFlag == LF_CHR) || (TypeFlag == LF_BLK))
  546. {
  547. offset = GetOctalBytes(DevMajor, outBuffer, offset, DEVLEN);
  548. offset = GetOctalBytes(DevMinor, outBuffer, offset, DEVLEN);
  549. }
  550. for ( ; offset < outBuffer.Length; )
  551. {
  552. outBuffer[offset++] = 0;
  553. }
  554. checksum = ComputeCheckSum(outBuffer);
  555. GetCheckSumOctalBytes(checksum, outBuffer, csOffset, CHKSUMLEN);
  556. isChecksumValid = true;
  557. }
  558. /// <summary>
  559. /// Get a hash code for the current object.
  560. /// </summary>
  561. /// <returns>A hash code for the current object.</returns>
  562. public override int GetHashCode()
  563. {
  564. return Name.GetHashCode();
  565. }
  566. /// <summary>
  567. /// Determines if this instance is equal to the specified object.
  568. /// </summary>
  569. /// <param name="obj">The object to compare with.</param>
  570. /// <returns>true if the objects are equal, false otherwise.</returns>
  571. public override bool Equals(object obj)
  572. {
  573. TarHeader localHeader = obj as TarHeader;
  574. bool result;
  575. if ( localHeader != null )
  576. {
  577. result = (name == localHeader.name)
  578. && (mode == localHeader.mode)
  579. && (UserId == localHeader.UserId)
  580. && (GroupId == localHeader.GroupId)
  581. && (Size == localHeader.Size)
  582. && (ModTime == localHeader.ModTime)
  583. && (Checksum == localHeader.Checksum)
  584. && (TypeFlag == localHeader.TypeFlag)
  585. && (LinkName == localHeader.LinkName)
  586. && (Magic == localHeader.Magic)
  587. && (Version == localHeader.Version)
  588. && (UserName == localHeader.UserName)
  589. && (GroupName == localHeader.GroupName)
  590. && (DevMajor == localHeader.DevMajor)
  591. && (DevMinor == localHeader.DevMinor);
  592. }
  593. else
  594. {
  595. result = false;
  596. }
  597. return result;
  598. }
  599. /// <summary>
  600. /// Set defaults for values used when constructing a TarHeader instance.
  601. /// </summary>
  602. /// <param name="userId">Value to apply as a default for userId.</param>
  603. /// <param name="userName">Value to apply as a default for userName.</param>
  604. /// <param name="groupId">Value to apply as a default for groupId.</param>
  605. /// <param name="groupName">Value to apply as a default for groupName.</param>
  606. static internal void SetValueDefaults(int userId, string userName, int groupId, string groupName)
  607. {
  608. defaultUserId = userIdAsSet = userId;
  609. defaultUser = userNameAsSet = userName;
  610. defaultGroupId = groupIdAsSet = groupId;
  611. defaultGroupName = groupNameAsSet = groupName;
  612. }
  613. static internal void RestoreSetValues()
  614. {
  615. defaultUserId = userIdAsSet;
  616. defaultUser = userNameAsSet;
  617. defaultGroupId = groupIdAsSet;
  618. defaultGroupName = groupNameAsSet;
  619. }
  620. /// <summary>
  621. /// Parse an octal string from a header buffer.
  622. /// </summary>
  623. /// <param name = "header">The header buffer from which to parse.</param>
  624. /// <param name = "offset">The offset into the buffer from which to parse.</param>
  625. /// <param name = "length">The number of header bytes to parse.</param>
  626. /// <returns>The long equivalent of the octal string.</returns>
  627. static public long ParseOctal(byte[] header, int offset, int length)
  628. {
  629. if ( header == null ) {
  630. throw new ArgumentNullException("header");
  631. }
  632. long result = 0;
  633. bool stillPadding = true;
  634. int end = offset + length;
  635. for (int i = offset; i < end ; ++i) {
  636. if (header[i] == 0) {
  637. break;
  638. }
  639. if (header[i] == (byte)' ' || header[i] == '0') {
  640. if (stillPadding) {
  641. continue;
  642. }
  643. if (header[i] == (byte)' ') {
  644. break;
  645. }
  646. }
  647. stillPadding = false;
  648. result = (result << 3) + (header[i] - '0');
  649. }
  650. return result;
  651. }
  652. /// <summary>
  653. /// Parse a name from a header buffer.
  654. /// </summary>
  655. /// <param name="header">
  656. /// The header buffer from which to parse.
  657. /// </param>
  658. /// <param name="offset">
  659. /// The offset into the buffer from which to parse.
  660. /// </param>
  661. /// <param name="length">
  662. /// The number of header bytes to parse.
  663. /// </param>
  664. /// <returns>
  665. /// The name parsed.
  666. /// </returns>
  667. static public StringBuilder ParseName(byte[] header, int offset, int length)
  668. {
  669. if ( header == null ) {
  670. throw new ArgumentNullException("header");
  671. }
  672. if ( offset < 0 ) {
  673. #if NETCF_1_0
  674. throw new ArgumentOutOfRangeException("offset");
  675. #else
  676. throw new ArgumentOutOfRangeException("offset", "Cannot be less than zero");
  677. #endif
  678. }
  679. if ( length < 0 )
  680. {
  681. #if NETCF_1_0
  682. throw new ArgumentOutOfRangeException("length");
  683. #else
  684. throw new ArgumentOutOfRangeException("length", "Cannot be less than zero");
  685. #endif
  686. }
  687. if ( offset + length > header.Length )
  688. {
  689. throw new ArgumentException("Exceeds header size", "length");
  690. }
  691. StringBuilder result = new StringBuilder(length);
  692. for (int i = offset; i < offset + length; ++i) {
  693. if (header[i] == 0) {
  694. break;
  695. }
  696. result.Append((char)header[i]);
  697. }
  698. return result;
  699. }
  700. /// <summary>
  701. /// Add <paramref name="name">name</paramref> to the buffer as a collection of bytes
  702. /// </summary>
  703. /// <param name="name">The name to add</param>
  704. /// <param name="nameOffset">The offset of the first character</param>
  705. /// <param name="buffer">The buffer to add to</param>
  706. /// <param name="bufferOffset">The index of the first byte to add</param>
  707. /// <param name="length">The number of characters/bytes to add</param>
  708. /// <returns>The next free index in the <paramref name="buffer"/></returns>
  709. public static int GetNameBytes(StringBuilder name, int nameOffset, byte[] buffer, int bufferOffset, int length)
  710. {
  711. if ( name == null ) {
  712. throw new ArgumentNullException("name");
  713. }
  714. if ( buffer == null ) {
  715. throw new ArgumentNullException("buffer");
  716. }
  717. return GetNameBytes(name.ToString(), nameOffset, buffer, bufferOffset, length);
  718. }
  719. /// <summary>
  720. /// Add <paramref name="name">name</paramref> to the buffer as a collection of bytes
  721. /// </summary>
  722. /// <param name="name">The name to add</param>
  723. /// <param name="nameOffset">The offset of the first character</param>
  724. /// <param name="buffer">The buffer to add to</param>
  725. /// <param name="bufferOffset">The index of the first byte to add</param>
  726. /// <param name="length">The number of characters/bytes to add</param>
  727. /// <returns>The next free index in the <paramref name="buffer"/></returns>
  728. public static int GetNameBytes(string name, int nameOffset, byte[] buffer, int bufferOffset, int length)
  729. {
  730. if ( name == null )
  731. {
  732. throw new ArgumentNullException("name");
  733. }
  734. if ( buffer == null )
  735. {
  736. throw new ArgumentNullException("buffer");
  737. }
  738. int i;
  739. for (i = 0 ; i < length - 1 && nameOffset + i < name.Length; ++i) {
  740. buffer[bufferOffset + i] = (byte)name[nameOffset + i];
  741. }
  742. for (; i < length ; ++i) {
  743. buffer[bufferOffset + i] = 0;
  744. }
  745. return bufferOffset + length;
  746. }
  747. /// <summary>
  748. /// Add an entry name to the buffer
  749. /// </summary>
  750. /// <param name="name">
  751. /// The name to add
  752. /// </param>
  753. /// <param name="buffer">
  754. /// The buffer to add to
  755. /// </param>
  756. /// <param name="offset">
  757. /// The offset into the buffer from which to start adding
  758. /// </param>
  759. /// <param name="length">
  760. /// The number of header bytes to add
  761. /// </param>
  762. /// <returns>
  763. /// The index of the next free byte in the buffer
  764. /// </returns>
  765. public static int GetNameBytes(StringBuilder name, byte[] buffer, int offset, int length)
  766. {
  767. if ( name == null ) {
  768. throw new ArgumentNullException("name");
  769. }
  770. if ( buffer == null ) {
  771. throw new ArgumentNullException("buffer");
  772. }
  773. return GetNameBytes(name.ToString(), 0, buffer, offset, length);
  774. }
  775. /// <summary>
  776. /// Add an entry name to the buffer
  777. /// </summary>
  778. /// <param name="name">The name to add</param>
  779. /// <param name="buffer">The buffer to add to</param>
  780. /// <param name="offset">The offset into the buffer from which to start adding</param>
  781. /// <param name="length">The number of header bytes to add</param>
  782. /// <returns>The index of the next free byte in the buffer</returns>
  783. public static int GetNameBytes(string name, byte[] buffer, int offset, int length)
  784. {
  785. if ( name == null ) {
  786. throw new ArgumentNullException("name");
  787. }
  788. if ( buffer == null )
  789. {
  790. throw new ArgumentNullException("buffer");
  791. }
  792. return GetNameBytes(name, 0, buffer, offset, length);
  793. }
  794. /// <summary>
  795. /// Add a string to a buffer as a collection of ascii bytes.
  796. /// </summary>
  797. /// <param name="toAdd">The string to add</param>
  798. /// <param name="nameOffset">The offset of the first character to add.</param>
  799. /// <param name="buffer">The buffer to add to.</param>
  800. /// <param name="bufferOffset">The offset to start adding at.</param>
  801. /// <param name="length">The number of ascii characters to add.</param>
  802. /// <returns>The next free index in the buffer.</returns>
  803. public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int bufferOffset, int length )
  804. {
  805. if ( toAdd == null ) {
  806. throw new ArgumentNullException("toAdd");
  807. }
  808. if ( buffer == null ) {
  809. throw new ArgumentNullException("buffer");
  810. }
  811. for (int i = 0 ; i < length && nameOffset + i < toAdd.Length; ++i)
  812. {
  813. buffer[bufferOffset + i] = (byte)toAdd[nameOffset + i];
  814. }
  815. return bufferOffset + length;
  816. }
  817. /// <summary>
  818. /// Put an octal representation of a value into a buffer
  819. /// </summary>
  820. /// <param name = "value">
  821. /// the value to be converted to octal
  822. /// </param>
  823. /// <param name = "buffer">
  824. /// buffer to store the octal string
  825. /// </param>
  826. /// <param name = "offset">
  827. /// The offset into the buffer where the value starts
  828. /// </param>
  829. /// <param name = "length">
  830. /// The length of the octal string to create
  831. /// </param>
  832. /// <returns>
  833. /// The offset of the character next byte after the octal string
  834. /// </returns>
  835. public static int GetOctalBytes(long value, byte[] buffer, int offset, int length)
  836. {
  837. if ( buffer == null ) {
  838. throw new ArgumentNullException("buffer");
  839. }
  840. int localIndex = length - 1;
  841. // Either a space or null is valid here. We use NULL as per GNUTar
  842. buffer[offset + localIndex] = 0;
  843. --localIndex;
  844. if (value > 0) {
  845. for ( long v = value; (localIndex >= 0) && (v > 0); --localIndex ) {
  846. buffer[offset + localIndex] = (byte)((byte)'0' + (byte)(v & 7));
  847. v >>= 3;
  848. }
  849. }
  850. for ( ; localIndex >= 0; --localIndex ) {
  851. buffer[offset + localIndex] = (byte)'0';
  852. }
  853. return offset + length;
  854. }
  855. /// <summary>
  856. /// Put an octal representation of a value into a buffer
  857. /// </summary>
  858. /// <param name = "value">Value to be convert to octal</param>
  859. /// <param name = "buffer">The buffer to update</param>
  860. /// <param name = "offset">The offset into the buffer to store the value</param>
  861. /// <param name = "length">The length of the octal string</param>
  862. /// <returns>Index of next byte</returns>
  863. public static int GetLongOctalBytes(long value, byte[] buffer, int offset, int length)
  864. {
  865. return GetOctalBytes(value, buffer, offset, length);
  866. }
  867. /// <summary>
  868. /// Add the checksum integer to header buffer.
  869. /// </summary>
  870. /// <param name = "value"></param>
  871. /// <param name = "buffer">The header buffer to set the checksum for</param>
  872. /// <param name = "offset">The offset into the buffer for the checksum</param>
  873. /// <param name = "length">The number of header bytes to update.
  874. /// It's formatted differently from the other fields: it has 6 digits, a
  875. /// null, then a space -- rather than digits, a space, then a null.
  876. /// The final space is already there, from checksumming
  877. /// </param>
  878. /// <returns>The modified buffer offset</returns>
  879. static int GetCheckSumOctalBytes(long value, byte[] buffer, int offset, int length)
  880. {
  881. TarHeader.GetOctalBytes(value, buffer, offset, length - 1);
  882. return offset + length;
  883. }
  884. /// <summary>
  885. /// Compute the checksum for a tar entry header.
  886. /// The checksum field must be all spaces prior to this happening
  887. /// </summary>
  888. /// <param name = "buffer">The tar entry's header buffer.</param>
  889. /// <returns>The computed checksum.</returns>
  890. static int ComputeCheckSum(byte[] buffer)
  891. {
  892. int sum = 0;
  893. for (int i = 0; i < buffer.Length; ++i) {
  894. sum += buffer[i];
  895. }
  896. return sum;
  897. }
  898. /// <summary>
  899. /// Make a checksum for a tar entry ignoring the checksum contents.
  900. /// </summary>
  901. /// <param name = "buffer">The tar entry's header buffer.</param>
  902. /// <returns>The checksum for the buffer</returns>
  903. static int MakeCheckSum(byte[] buffer)
  904. {
  905. int sum = 0;
  906. for ( int i = 0; i < CHKSUMOFS; ++i )
  907. {
  908. sum += buffer[i];
  909. }
  910. for ( int i = 0; i < TarHeader.CHKSUMLEN; ++i)
  911. {
  912. sum += (byte)' ';
  913. }
  914. for (int i = CHKSUMOFS + CHKSUMLEN; i < buffer.Length; ++i)
  915. {
  916. sum += buffer[i];
  917. }
  918. return sum;
  919. }
  920. static int GetCTime(System.DateTime dateTime)
  921. {
  922. return unchecked((int)((dateTime.Ticks - dateTime1970.Ticks) / timeConversionFactor));
  923. }
  924. static DateTime GetDateTimeFromCTime(long ticks)
  925. {
  926. DateTime result;
  927. try {
  928. result = new DateTime(dateTime1970.Ticks + ticks * timeConversionFactor);
  929. }
  930. catch(ArgumentOutOfRangeException) {
  931. result = dateTime1970;
  932. }
  933. return result;
  934. }
  935. #region Instance Fields
  936. string name;
  937. int mode;
  938. int userId;
  939. int groupId;
  940. long size;
  941. DateTime modTime;
  942. int checksum;
  943. bool isChecksumValid;
  944. byte typeFlag;
  945. string linkName;
  946. string magic;
  947. string version;
  948. string userName;
  949. string groupName;
  950. int devMajor;
  951. int devMinor;
  952. #endregion
  953. #region Class Fields
  954. // Values used during recursive operations.
  955. static internal int userIdAsSet;
  956. static internal int groupIdAsSet;
  957. static internal string userNameAsSet;
  958. static internal string groupNameAsSet = "None";
  959. static internal int defaultUserId;
  960. static internal int defaultGroupId;
  961. static internal string defaultGroupName = "None";
  962. static internal string defaultUser;
  963. #endregion
  964. }
  965. }
  966. /* The original Java file had this header:
  967. *
  968. ** Authored by Timothy Gerard Endres
  969. ** <mailto:time@gjt.org> <http://www.trustice.com>
  970. **
  971. ** This work has been placed into the public domain.
  972. ** You may use this work in any way and for any purpose you wish.
  973. **
  974. ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
  975. ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
  976. ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
  977. ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
  978. ** REDISTRIBUTION OF THIS SOFTWARE.
  979. **
  980. */