TarHeader.cs 32 KB

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