// Reads fields only found in ustar format or above and converts them to their expected data type. // Throws if any conversion fails. private void ReadMagicAttribute(Span <byte> buffer) { Span <byte> magic = buffer.Slice(FieldLocations.Magic, FieldLengths.Magic); // If at this point the magic value is all nulls, we definitely have a V7 if (TarHelpers.IsAllNullBytes(magic)) { _format = TarEntryFormat.V7; return; } // When the magic field is set, the archive is newer than v7. if (magic.SequenceEqual(GnuMagicBytes)) { _magic = GnuMagic; _format = TarEntryFormat.Gnu; } else if (magic.SequenceEqual(UstarMagicBytes)) { _magic = UstarMagic; if (_format == TarEntryFormat.V7) { // Important: Only change to ustar if we had not changed the format to pax already _format = TarEntryFormat.Ustar; } } else { _magic = Encoding.ASCII.GetString(magic); } }
// Attempts to read the fields shared by all formats and stores them in their expected data type. // Throws if any data type conversion fails. // Returns true on success, false if checksum is zero. private static TarHeader?TryReadCommonAttributes(Span <byte> buffer, TarEntryFormat initialFormat) { // Start by collecting fields that need special checks that return early when data is wrong // Empty checksum means this is an invalid (all blank) entry, finish early Span <byte> spanChecksum = buffer.Slice(FieldLocations.Checksum, FieldLengths.Checksum); if (TarHelpers.IsAllNullBytes(spanChecksum)) { return(null); } int checksum = (int)TarHelpers.ParseOctal <uint>(spanChecksum); // Zero checksum means the whole header is empty if (checksum == 0) { return(null); } long size = (int)TarHelpers.ParseOctal <uint>(buffer.Slice(FieldLocations.Size, FieldLengths.Size)); if (size < 0) { throw new FormatException(string.Format(SR.TarSizeFieldNegative)); } // Continue with the rest of the fields that require no special checks TarHeader header = new(initialFormat, name : TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.Name, FieldLengths.Name)), mode : (int)TarHelpers.ParseOctal <uint>(buffer.Slice(FieldLocations.Mode, FieldLengths.Mode)), mTime : TarHelpers.GetDateTimeOffsetFromSecondsSinceEpoch((long)TarHelpers.ParseOctal <ulong>(buffer.Slice(FieldLocations.MTime, FieldLengths.MTime))), typeFlag : (TarEntryType)buffer[FieldLocations.TypeFlag]) { _checksum = checksum, _size = size, _uid = (int)TarHelpers.ParseOctal <uint>(buffer.Slice(FieldLocations.Uid, FieldLengths.Uid)), _gid = (int)TarHelpers.ParseOctal <uint>(buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)), _linkName = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.LinkName, FieldLengths.LinkName)) }; if (header._format == TarEntryFormat.Unknown) { header._format = header._typeFlag switch { TarEntryType.ExtendedAttributes or TarEntryType.GlobalExtendedAttributes => TarEntryFormat.Pax, TarEntryType.DirectoryList or TarEntryType.LongLink or TarEntryType.LongPath or TarEntryType.MultiVolume or TarEntryType.RenamedOrSymlinked or TarEntryType.TapeVolume => TarEntryFormat.Gnu, // V7 is the only one that uses 'V7RegularFile'. TarEntryType.V7RegularFile => TarEntryFormat.V7, TarEntryType.SparseFile => throw new NotSupportedException(string.Format(SR.TarEntryTypeNotSupported, header._typeFlag)), // We can quickly determine the *minimum* possible format if the entry type // is the POSIX 'RegularFile', although later we could upgrade it to PAX or GNU _ => (header._typeFlag == TarEntryType.RegularFile) ? TarEntryFormat.Ustar : TarEntryFormat.V7 }; } return(header); }
// Attempts to read the fields shared by all formats and stores them in their expected data type. // Throws if any data type conversion fails. // Returns true on success, false if checksum is zero. private bool TryReadCommonAttributes(Span <byte> buffer) { // Start by collecting fields that need special checks that return early when data is wrong // Empty checksum means this is an invalid (all blank) entry, finish early Span <byte> spanChecksum = buffer.Slice(FieldLocations.Checksum, FieldLengths.Checksum); if (TarHelpers.IsAllNullBytes(spanChecksum)) { return(false); } _checksum = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(spanChecksum); // Zero checksum means the whole header is empty if (_checksum == 0) { return(false); } _size = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(buffer.Slice(FieldLocations.Size, FieldLengths.Size)); if (_size < 0) { throw new FormatException(string.Format(SR.TarSizeFieldNegative, _name)); } // Continue with the rest of the fields that require no special checks _name = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.Name, FieldLengths.Name)); _mode = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(buffer.Slice(FieldLocations.Mode, FieldLengths.Mode)); _uid = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(buffer.Slice(FieldLocations.Uid, FieldLengths.Uid)); _gid = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(buffer.Slice(FieldLocations.Gid, FieldLengths.Gid)); int mTime = TarHelpers.GetTenBaseNumberFromOctalAsciiChars(buffer.Slice(FieldLocations.MTime, FieldLengths.MTime)); _mTime = TarHelpers.GetDateTimeFromSecondsSinceEpoch(mTime); _typeFlag = (TarEntryType)buffer[FieldLocations.TypeFlag]; _linkName = TarHelpers.GetTrimmedUtf8String(buffer.Slice(FieldLocations.LinkName, FieldLengths.LinkName)); if (_format == TarFormat.Unknown) { _format = _typeFlag switch { TarEntryType.ExtendedAttributes or TarEntryType.GlobalExtendedAttributes => TarFormat.Pax, TarEntryType.DirectoryList or TarEntryType.LongLink or TarEntryType.LongPath or TarEntryType.MultiVolume or TarEntryType.RenamedOrSymlinked or TarEntryType.SparseFile or TarEntryType.TapeVolume => TarFormat.Gnu, // V7 is the only one that uses 'V7RegularFile'. TarEntryType.V7RegularFile => TarFormat.V7, // We can quickly determine the *minimum* possible format if the entry type // is the POSIX 'RegularFile', although later we could upgrade it to PAX or GNU _ => (_typeFlag == TarEntryType.RegularFile) ? TarFormat.Ustar : TarFormat.V7 }; } return(true); }