/// <summary>
        /// Parse TarHeader information from a header buffer.
        /// </summary>
        /// <param name = "header">
        /// The tar entry header buffer to get information from.
        /// </param>
        public void ParseBuffer(byte[] header)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            int offset = 0;

            name    = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
            offset += TarHeader.NAMELEN;

            mode    = (int)TarHeader.ParseOctal(header, offset, TarHeader.MODELEN);
            offset += TarHeader.MODELEN;

            UserId  = (int)TarHeader.ParseOctal(header, offset, TarHeader.UIDLEN);
            offset += TarHeader.UIDLEN;

            GroupId = (int)TarHeader.ParseOctal(header, offset, TarHeader.GIDLEN);
            offset += TarHeader.GIDLEN;

            Size    = TarHeader.ParseOctal(header, offset, TarHeader.SIZELEN);
            offset += TarHeader.SIZELEN;

            ModTime = GetDateTimeFromCTime(TarHeader.ParseOctal(header, offset, TarHeader.MODTIMELEN));
            offset += TarHeader.MODTIMELEN;

            checksum = (int)TarHeader.ParseOctal(header, offset, TarHeader.CHKSUMLEN);
            offset  += TarHeader.CHKSUMLEN;

            TypeFlag = header[offset++];

            LinkName = TarHeader.ParseName(header, offset, TarHeader.NAMELEN).ToString();
            offset  += TarHeader.NAMELEN;

            Magic   = TarHeader.ParseName(header, offset, TarHeader.MAGICLEN).ToString();
            offset += TarHeader.MAGICLEN;

            Version = TarHeader.ParseName(header, offset, TarHeader.VERSIONLEN).ToString();
            offset += TarHeader.VERSIONLEN;

            UserName = TarHeader.ParseName(header, offset, TarHeader.UNAMELEN).ToString();
            offset  += TarHeader.UNAMELEN;

            GroupName = TarHeader.ParseName(header, offset, TarHeader.GNAMELEN).ToString();
            offset   += TarHeader.GNAMELEN;

            DevMajor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);
            offset  += TarHeader.DEVLEN;

            DevMinor = (int)TarHeader.ParseOctal(header, offset, TarHeader.DEVLEN);

            // Fields past this point not currently parsed or used...

            isChecksumValid = Checksum == TarHeader.MakeCheckSum(header);
        }
Пример #2
0
        /// <summary>
        /// Parse TarHeader information from a header buffer.
        /// </summary>
        /// <param name = "header">
        /// The tar entry header buffer to get information from.
        /// </param>
        /// <param name = "nameEncoding">
        /// The <see cref="Encoding"/> used for the Name field, or null for ASCII only
        /// </param>
        public void ParseBuffer(byte[] header, Encoding nameEncoding)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            int offset = 0;

            name    = ParseName(header, offset, NAMELEN, nameEncoding).ToString();
            offset += NAMELEN;

            mode    = (int)ParseOctal(header, offset, MODELEN);
            offset += MODELEN;

            UserId  = (int)ParseOctal(header, offset, UIDLEN);
            offset += UIDLEN;

            GroupId = (int)ParseOctal(header, offset, GIDLEN);
            offset += GIDLEN;

            Size    = ParseBinaryOrOctal(header, offset, SIZELEN);
            offset += SIZELEN;

            ModTime = GetDateTimeFromCTime(ParseOctal(header, offset, MODTIMELEN));
            offset += MODTIMELEN;

            checksum = (int)ParseOctal(header, offset, CHKSUMLEN);
            offset  += CHKSUMLEN;

            TypeFlag = header[offset++];

            LinkName = ParseName(header, offset, NAMELEN, nameEncoding).ToString();
            offset  += NAMELEN;

            Magic   = ParseName(header, offset, MAGICLEN, nameEncoding).ToString();
            offset += MAGICLEN;

            if (Magic == "ustar")
            {
                Version = ParseName(header, offset, VERSIONLEN, nameEncoding).ToString();
                offset += VERSIONLEN;

                UserName = ParseName(header, offset, UNAMELEN, nameEncoding).ToString();
                offset  += UNAMELEN;

                GroupName = ParseName(header, offset, GNAMELEN, nameEncoding).ToString();
                offset   += GNAMELEN;

                DevMajor = (int)ParseOctal(header, offset, DEVLEN);
                offset  += DEVLEN;

                DevMinor = (int)ParseOctal(header, offset, DEVLEN);
                offset  += DEVLEN;

                string prefix = ParseName(header, offset, PREFIXLEN, nameEncoding).ToString();
                if (!string.IsNullOrEmpty(prefix))
                {
                    Name = prefix + '/' + Name;
                }
            }

            isChecksumValid = Checksum == TarHeader.MakeCheckSum(header);
        }