ParseName() public static method

Parse a name from a header buffer.
public static ParseName ( byte header, int offset, int length ) : StringBuilder
header byte /// The header buffer from which to parse. ///
offset int /// The offset into the buffer from which to parse. ///
length int /// The number of header bytes to parse. ///
return StringBuilder
        /// <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);
        }
Example #2
0
        /// <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)
        {
            int offset = 0;

            name    = TarHeader.ParseName(header, offset, TarHeader.NAMELEN);
            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);
            offset  += TarHeader.NAMELEN;

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

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

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

            groupName = TarHeader.ParseName(header, offset, TarHeader.GNAMELEN);
            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...
        }
Example #3
0
        public void ParseBuffer(byte[] header)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            int num = 0;

            this.name     = TarHeader.ParseName(header, num, 100).ToString();
            num          += 100;
            this.mode     = (int)TarHeader.ParseOctal(header, num, 8);
            num          += 8;
            this.UserId   = (int)TarHeader.ParseOctal(header, num, 8);
            num          += 8;
            this.GroupId  = (int)TarHeader.ParseOctal(header, num, 8);
            num          += 8;
            this.Size     = TarHeader.ParseBinaryOrOctal(header, num, 12);
            num          += 12;
            this.ModTime  = TarHeader.GetDateTimeFromCTime(TarHeader.ParseOctal(header, num, 12));
            num          += 12;
            this.checksum = (int)TarHeader.ParseOctal(header, num, 8);
            num          += 8;
            this.TypeFlag = header[num++];
            this.LinkName = TarHeader.ParseName(header, num, 100).ToString();
            num          += 100;
            this.Magic    = TarHeader.ParseName(header, num, 6).ToString();
            num          += 6;
            if (this.Magic == "ustar")
            {
                this.Version   = TarHeader.ParseName(header, num, 2).ToString();
                num           += 2;
                this.UserName  = TarHeader.ParseName(header, num, 32).ToString();
                num           += 32;
                this.GroupName = TarHeader.ParseName(header, num, 32).ToString();
                num           += 32;
                this.DevMajor  = (int)TarHeader.ParseOctal(header, num, 8);
                num           += 8;
                this.DevMinor  = (int)TarHeader.ParseOctal(header, num, 8);
                num           += 8;
                string text = TarHeader.ParseName(header, num, 155).ToString();
                if (!string.IsNullOrEmpty(text))
                {
                    this.Name = text + '/' + this.Name;
                }
            }
            this.isChecksumValid = (this.Checksum == TarHeader.MakeCheckSum(header));
        }
Example #4
0
        /// <summary>
        /// Get the next entry in this tar archive. This will skip
        /// over any remaining data in the current entry, if there
        /// is one, and place the input stream at the header of the
        /// next entry, and read the header and instantiate a new
        /// TarEntry from the header bytes and return that entry.
        /// If there are no more entries in the archive, null will
        /// be returned to indicate that the end of the archive has
        /// been reached.
        /// </summary>
        /// <returns>
        /// The next TarEntry in the archive, or null.
        /// </returns>
        public TarEntry GetNextEntry()
        {
            if (hasHitEOF)
            {
                return(null);
            }

            if (currentEntry != null)
            {
                SkipToNextEntry();
            }

            byte[] headerBuf = tarBuffer.ReadBlock();

            if (headerBuf == null)
            {
                hasHitEOF = true;
            }
            else if (TarBuffer.IsEndOfArchiveBlock(headerBuf))
            {
                hasHitEOF = true;
            }

            if (hasHitEOF)
            {
                currentEntry = null;
            }
            else
            {
                try {
                    TarHeader header = new TarHeader();
                    header.ParseBuffer(headerBuf);
                    if (!header.IsChecksumValid)
                    {
                        throw new TarException("Header checksum is invalid");
                    }
                    this.entryOffset = 0;
                    this.entrySize   = header.Size;

                    StringBuilder longName = null;

                    if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME)
                    {
                        byte[] nameBuffer = new byte[TarBuffer.BlockSize];
                        long   numToRead  = this.entrySize;

                        longName = new StringBuilder();

                        while (numToRead > 0)
                        {
                            int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead));

                            if (numRead == -1)
                            {
                                throw new InvalidHeaderException("Failed to read long name entry");
                            }

                            longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
                            numToRead -= numRead;
                        }

                        SkipToNextEntry();
                        headerBuf = this.tarBuffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_GHDR)      // POSIX global extended header
                    // Ignore things we dont understand completely for now
                    {
                        SkipToNextEntry();
                        headerBuf = this.tarBuffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_XHDR)      // POSIX extended header
                    // Ignore things we dont understand completely for now
                    {
                        SkipToNextEntry();
                        headerBuf = this.tarBuffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR)
                    {
                        // TODO: could show volume name when verbose
                        SkipToNextEntry();
                        headerBuf = this.tarBuffer.ReadBlock();
                    }
                    else if (header.TypeFlag != TarHeader.LF_NORMAL &&
                             header.TypeFlag != TarHeader.LF_OLDNORM &&
                             header.TypeFlag != TarHeader.LF_DIR)
                    {
                        // Ignore things we dont understand completely for now
                        SkipToNextEntry();
                        headerBuf = tarBuffer.ReadBlock();
                    }

                    if (entryFactory == null)
                    {
                        currentEntry = new TarEntry(headerBuf);
                        if (longName != null)
                        {
                            currentEntry.Name = longName.ToString();
                        }
                    }
                    else
                    {
                        currentEntry = entryFactory.CreateEntry(headerBuf);
                    }

                    // Magic was checked here for 'ustar' but there are multiple valid possibilities
                    // so this is not done anymore.

                    entryOffset = 0;

                    // TODO: Review How do we resolve this discrepancy?!
                    entrySize = this.currentEntry.Size;
                } catch (InvalidHeaderException ex) {
                    entrySize    = 0;
                    entryOffset  = 0;
                    currentEntry = null;
                    string errorText = string.Format("Bad header in record {0} block {1} {2}",
                                                     tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, ex.Message);
                    throw new InvalidHeaderException(errorText);
                }
            }
            return(currentEntry);
        }
Example #5
0
        /// <summary>
        /// Get the next entry in this tar archive. This will skip
        /// over any remaining data in the current entry, if there
        /// is one, and place the input stream at the header of the
        /// next entry, and read the header and instantiate a new
        /// TarEntry from the header bytes and return that entry.
        /// If there are no more entries in the archive, null will
        /// be returned to indicate that the end of the archive has
        /// been reached.
        /// </summary>
        /// <returns>
        /// The next TarEntry in the archive, or null.
        /// </returns>
        public TarEntry GetNextEntry()
        {
            if (this.hasHitEOF)
            {
                return(null);
            }

            if (this.currEntry != null)
            {
                SkipToNextEntry();
            }

            byte[] headerBuf = this.buffer.ReadBlock();

            if (headerBuf == null)
            {
                if (this.debug)
                {
                    //Console.WriteLine.WriteLine("READ NULL BLOCK");
                }

                this.hasHitEOF = true;
            }
            else if (this.buffer.IsEOFBlock(headerBuf))
            {
                if (this.debug)
                {
                    //Console.WriteLine.WriteLine( "READ EOF BLOCK" );
                }

                this.hasHitEOF = true;
            }

            if (this.hasHitEOF)
            {
                this.currEntry = null;
            }
            else
            {
                try
                {
                    TarHeader header = new TarHeader();
                    header.ParseBuffer(headerBuf);
                    this.entryOffset = 0;
                    this.entrySize   = (int)header.size;

                    StringBuilder longName = null;

                    if (header.typeFlag == TarHeader.LF_GNU_LONGNAME)
                    {
                        Console.WriteLine("TarInputStream: Long name found '" + header.name + "' size = " + header.size); // DEBUG

                        byte[] nameBuffer = new byte[TarBuffer.BlockSize];

                        int numToRead = this.entrySize;

                        longName = new StringBuilder();

                        while (numToRead > 0)
                        {
                            int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : numToRead));

                            if (numRead == -1)
                            {
                                throw new InvalidHeaderException("Failed to read long name entry");
                            }

                            longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
                            numToRead -= numRead;
                        }

                        Console.WriteLine("TarInputStream: Long name is '" + longName.ToString()); // DEBUG

                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.typeFlag == TarHeader.LF_GHDR) // POSIX global extended header
                    {
                        // Ignore things we dont understand completely for now
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.typeFlag == TarHeader.LF_XHDR) // POSIX extended header
                    {
                        // Ignore things we dont understand completely for now
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.typeFlag == TarHeader.LF_GNU_VOLHDR)
                    {
                        // TODO could show volume name when verbose?
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.typeFlag != TarHeader.LF_NORMAL &&
                             header.typeFlag != TarHeader.LF_OLDNORM)
                    {
                        // Ignore things we dont understand completely for now
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }

                    if (this.eFactory == null)
                    {
                        this.currEntry = new TarEntry(headerBuf);
                        if (longName != null)
                        {
                            this.currEntry.TarHeader.name.Length = 0;
                            this.currEntry.TarHeader.name.Append(longName.ToString());
                        }
                    }
                    else
                    {
                        this.currEntry = this.eFactory.CreateEntry(headerBuf);
                    }

                    // TODO -jr- ustar is not the only magic possible by any means
                    // tar, xtar, ...
                    if (!(headerBuf[257] == 'u' && headerBuf[258] == 's' && headerBuf[259] == 't' && headerBuf[260] == 'a' && headerBuf[261] == 'r'))
                    {
                        throw new InvalidHeaderException("header magic is not 'ustar', but '" + headerBuf[257] + headerBuf[258] + headerBuf[259] + headerBuf[260] + headerBuf[261] +
                                                         "', or (dec) " + ((int)headerBuf[257]) + ", " + ((int)headerBuf[258]) + ", " + ((int)headerBuf[259]) + ", " + ((int)headerBuf[260]) + ", " + ((int)headerBuf[261]));
                    }

                    if (this.debug)
                    {
                        //Console.WriteLine.WriteLine("TarInputStream: SET CURRENTRY '" + this.currEntry.Name + "' size = " + this.currEntry.Size);
                    }

                    this.entryOffset = 0;

                    // TODO REVIEW How do we resolve this discrepancy?!
                    this.entrySize = (int)this.currEntry.Size;
                }
                catch (InvalidHeaderException ex)
                {
                    this.entrySize   = 0;
                    this.entryOffset = 0;
                    this.currEntry   = null;
                    throw new InvalidHeaderException("bad header in record " + this.buffer.GetCurrentBlockNum() + " block " + this.buffer.GetCurrentBlockNum() + ", " + ex.Message);
                }
            }
            return(this.currEntry);
        }
Example #6
0
 public TarEntry GetNextEntry()
 {
     if (this.hasHitEOF)
     {
         return(null);
     }
     if (this.currentEntry != null)
     {
         this.SkipToNextEntry();
     }
     byte[] array = this.buffer.ReadBlock();
     if (array == null)
     {
         this.hasHitEOF = true;
     }
     else if (TarBuffer.IsEndOfArchiveBlock(array))
     {
         this.hasHitEOF = true;
     }
     if (this.hasHitEOF)
     {
         this.currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader tarHeader = new TarHeader();
             tarHeader.ParseBuffer(array);
             if (!tarHeader.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             this.entryOffset = 0L;
             this.entrySize   = tarHeader.Size;
             StringBuilder stringBuilder = null;
             if (tarHeader.TypeFlag == 76)
             {
                 byte[] array2 = new byte[512];
                 long   num    = this.entrySize;
                 stringBuilder = new StringBuilder();
                 while (num > 0L)
                 {
                     int num2 = this.Read(array2, 0, (num > (long)array2.Length) ? array2.Length : ((int)num));
                     if (num2 == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     stringBuilder.Append(TarHeader.ParseName(array2, 0, num2).ToString());
                     num -= (long)num2;
                 }
                 this.SkipToNextEntry();
                 array = this.buffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 103)
             {
                 this.SkipToNextEntry();
                 array = this.buffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 120)
             {
                 this.SkipToNextEntry();
                 array = this.buffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 86)
             {
                 this.SkipToNextEntry();
                 array = this.buffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag != 48 && tarHeader.TypeFlag != 0 && tarHeader.TypeFlag != 53)
             {
                 this.SkipToNextEntry();
                 array = this.buffer.ReadBlock();
             }
             if (this.entryFactory == null)
             {
                 this.currentEntry = new TarEntry(array);
                 if (stringBuilder != null)
                 {
                     this.currentEntry.Name = stringBuilder.ToString();
                 }
             }
             else
             {
                 this.currentEntry = this.entryFactory.CreateEntry(array);
             }
             this.entryOffset = 0L;
             this.entrySize   = this.currentEntry.Size;
         }
         catch (InvalidHeaderException ex)
         {
             this.entrySize    = 0L;
             this.entryOffset  = 0L;
             this.currentEntry = null;
             string message = string.Format("Bad header in record {0} block {1} {2}", this.buffer.CurrentRecord, this.buffer.CurrentBlock, ex.Message);
             throw new InvalidHeaderException(message);
         }
     }
     return(this.currentEntry);
 }
Example #7
0
 public TarEntry GetNextEntry()
 {
     if (this.hasHitEOF)
     {
         return(null);
     }
     if (this.currentEntry != null)
     {
         this.SkipToNextEntry();
     }
     byte[] block = this.buffer.ReadBlock();
     if (block == null)
     {
         this.hasHitEOF = true;
     }
     else if (this.buffer.IsEOFBlock(block))
     {
         this.hasHitEOF = true;
     }
     if (this.hasHitEOF)
     {
         this.currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader header = new TarHeader();
             header.ParseBuffer(block);
             if (!header.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             this.entryOffset = 0L;
             this.entrySize   = header.Size;
             StringBuilder builder = null;
             if (header.TypeFlag == 0x4c)
             {
                 byte[] buffer    = new byte[0x200];
                 long   entrySize = this.entrySize;
                 builder = new StringBuilder();
                 while (entrySize > 0L)
                 {
                     int length = this.Read(buffer, 0, (entrySize > buffer.Length) ? buffer.Length : ((int)entrySize));
                     if (length == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     builder.Append(TarHeader.ParseName(buffer, 0, length).ToString());
                     entrySize -= length;
                 }
                 this.SkipToNextEntry();
                 block = this.buffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x67)
             {
                 this.SkipToNextEntry();
                 block = this.buffer.ReadBlock();
             }
             else if (header.TypeFlag == 120)
             {
                 this.SkipToNextEntry();
                 block = this.buffer.ReadBlock();
             }
             else if (header.TypeFlag == 0x56)
             {
                 this.SkipToNextEntry();
                 block = this.buffer.ReadBlock();
             }
             else if (((header.TypeFlag != 0x30) && (header.TypeFlag != 0)) && (header.TypeFlag != 0x35))
             {
                 this.SkipToNextEntry();
                 block = this.buffer.ReadBlock();
             }
             if (this.entryFactory == null)
             {
                 this.currentEntry = new TarEntry(block);
                 if (builder != null)
                 {
                     this.currentEntry.Name = builder.ToString();
                 }
             }
             else
             {
                 this.currentEntry = this.entryFactory.CreateEntry(block);
             }
             this.entryOffset = 0L;
             this.entrySize   = this.currentEntry.Size;
         }
         catch (InvalidHeaderException exception)
         {
             this.entrySize    = 0L;
             this.entryOffset  = 0L;
             this.currentEntry = null;
             throw new InvalidHeaderException($"Bad header in record {this.buffer.CurrentRecord} block {this.buffer.CurrentBlock} {exception.Message}");
         }
     }
     return(this.currentEntry);
 }
Example #8
0
 public TarEntry GetNextEntry()
 {
     if (hasHitEOF)
     {
         return(null);
     }
     if (currentEntry != null)
     {
         SkipToNextEntry();
     }
     byte[] array = tarBuffer.ReadBlock();
     if (array == null)
     {
         hasHitEOF = true;
     }
     else
     {
         hasHitEOF |= TarBuffer.IsEndOfArchiveBlock(array);
     }
     if (!hasHitEOF)
     {
         try
         {
             TarHeader tarHeader = new TarHeader();
             tarHeader.ParseBuffer(array);
             if (!tarHeader.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             entryOffset = 0L;
             entrySize   = tarHeader.Size;
             StringBuilder stringBuilder = null;
             if (tarHeader.TypeFlag == 76)
             {
                 byte[] array2 = new byte[512];
                 long   num    = entrySize;
                 stringBuilder = new StringBuilder();
                 while (num > 0)
                 {
                     int num2 = Read(array2, 0, (int)((num > array2.Length) ? array2.Length : num));
                     if (num2 == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     stringBuilder.Append(TarHeader.ParseName(array2, 0, num2).ToString());
                     num -= num2;
                 }
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 103)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 120)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 86)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag != 48 && tarHeader.TypeFlag != 0 && tarHeader.TypeFlag != 49 && tarHeader.TypeFlag != 50 && tarHeader.TypeFlag != 53)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             if (entryFactory == null)
             {
                 currentEntry = new TarEntry(array);
                 if (stringBuilder != null)
                 {
                     currentEntry.Name = stringBuilder.ToString();
                 }
             }
             else
             {
                 currentEntry = entryFactory.CreateEntry(array);
             }
             entryOffset = 0L;
             entrySize   = currentEntry.Size;
         }
         catch (InvalidHeaderException ex)
         {
             entrySize    = 0L;
             entryOffset  = 0L;
             currentEntry = null;
             throw new InvalidHeaderException($"Bad header in record {tarBuffer.CurrentRecord} block {tarBuffer.CurrentBlock} {ex.Message}");
         }
     }
     else
     {
         currentEntry = null;
     }
     return(currentEntry);
 }
Example #9
0
        public TarEntry GetNextEntry()
        {
            if (this.hasHitEOF)
            {
                return(null);
            }

            if (this.currentEntry != null)
            {
                SkipToNextEntry();
            }

            byte[] headerBuf = this.buffer.ReadBlock();

            if (headerBuf == null)
            {
                this.hasHitEOF = true;
            }
            else if (TarBuffer.IsEndOfArchiveBlock(headerBuf))
            {
                this.hasHitEOF = true;
            }

            if (this.hasHitEOF)
            {
                this.currentEntry = null;
            }
            else
            {
                try {
                    TarHeader header = new TarHeader();
                    header.ParseBuffer(headerBuf);
                    if (!header.IsChecksumValid)
                    {
                        throw new TarException("Header checksum is invalid");
                    }
                    this.entryOffset = 0;
                    this.entrySize   = header.Size;

                    StringBuilder longName = null;

                    if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME)
                    {
                        byte[] nameBuffer = new byte[TarBuffer.BlockSize];
                        long   numToRead  = this.entrySize;

                        longName = new StringBuilder();

                        while (numToRead > 0)
                        {
                            int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead));

                            if (numRead == -1)
                            {
                                throw new InvalidHeaderException("Failed to read long name entry");
                            }

                            longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString());
                            numToRead -= numRead;
                        }

                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_GHDR)
                    {
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_XHDR)
                    {
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR)
                    {
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }
                    else if (header.TypeFlag != TarHeader.LF_NORMAL &&
                             header.TypeFlag != TarHeader.LF_OLDNORM &&
                             header.TypeFlag != TarHeader.LF_DIR)
                    {
                        SkipToNextEntry();
                        headerBuf = this.buffer.ReadBlock();
                    }

                    if (this.entryFactory == null)
                    {
                        this.currentEntry = new TarEntry(headerBuf);
                        if (longName != null)
                        {
                            currentEntry.Name = longName.ToString();
                        }
                    }
                    else
                    {
                        this.currentEntry = this.entryFactory.CreateEntry(headerBuf);
                    }


                    this.entryOffset = 0;

                    this.entrySize = this.currentEntry.Size;
                } catch (InvalidHeaderException ex) {
                    this.entrySize    = 0;
                    this.entryOffset  = 0;
                    this.currentEntry = null;
                    string errorText = string.Format("Bad header in record {0} block {1} {2}",
                                                     buffer.CurrentRecord, buffer.CurrentBlock, ex.Message);
                    throw new InvalidHeaderException(errorText);
                }
            }
            return(this.currentEntry);
        }
Example #10
0
 public TarEntry GetNextEntry()
 {
     //IL_00a6: Unknown result type (might be due to invalid IL or missing references)
     //IL_00ac: Expected O, but got Unknown
     if (hasHitEOF)
     {
         return(null);
     }
     if (currentEntry != null)
     {
         SkipToNextEntry();
     }
     byte[] array = tarBuffer.ReadBlock();
     if (array == null)
     {
         hasHitEOF = true;
     }
     else if (TarBuffer.IsEndOfArchiveBlock(array))
     {
         hasHitEOF = true;
     }
     if (hasHitEOF)
     {
         currentEntry = null;
     }
     else
     {
         try
         {
             TarHeader tarHeader = new TarHeader();
             tarHeader.ParseBuffer(array);
             if (!tarHeader.IsChecksumValid)
             {
                 throw new TarException("Header checksum is invalid");
             }
             entryOffset = 0L;
             entrySize   = tarHeader.Size;
             StringBuilder val = null;
             if (tarHeader.TypeFlag == 76)
             {
                 byte[] array2 = new byte[512];
                 long   num    = entrySize;
                 val = new StringBuilder();
                 while (num > 0)
                 {
                     int num2 = ((Stream)this).Read(array2, 0, (int)((num > array2.Length) ? array2.Length : num));
                     if (num2 == -1)
                     {
                         throw new InvalidHeaderException("Failed to read long name entry");
                     }
                     val.Append(((object)TarHeader.ParseName(array2, 0, num2)).ToString());
                     num -= num2;
                 }
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 103)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 120)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag == 86)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             else if (tarHeader.TypeFlag != 48 && tarHeader.TypeFlag != 0 && tarHeader.TypeFlag != 53)
             {
                 SkipToNextEntry();
                 array = tarBuffer.ReadBlock();
             }
             if (entryFactory == null)
             {
                 currentEntry = new TarEntry(array);
                 if (val != null)
                 {
                     currentEntry.Name = ((object)val).ToString();
                 }
             }
             else
             {
                 currentEntry = entryFactory.CreateEntry(array);
             }
             entryOffset = 0L;
             entrySize   = currentEntry.Size;
         }
         catch (InvalidHeaderException ex)
         {
             entrySize    = 0L;
             entryOffset  = 0L;
             currentEntry = null;
             string message = $"Bad header in record {tarBuffer.CurrentRecord} block {tarBuffer.CurrentBlock} {((global::System.Exception)(object)ex).get_Message()}";
             throw new InvalidHeaderException(message);
         }
     }
     return(currentEntry);
 }