adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.28k stars 480 forks source link

Incorrect code processing long filename (more than 100 symbols) #50

Open sfairat15 opened 9 years ago

sfairat15 commented 9 years ago

Hello.

We get exception when try to add files with names more than 100 symbols. System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at SharpCompress.Common.Tar.Headers.TarHeader.Write(Stream output)

Looks like next code is incorrect at TarHeader class

if (Name.Length > 100) { name = Name.Substring(101, Name.Length); ArchiveEncoding.Default.GetBytes(name).CopyTo(buffer, 345); }

Should be:

name = Name.Substring(101, Name.Length - 100); //??

sander2 commented 9 years ago

Actually, that's not quite correct. There are two fields: the name at byte 0, and the prefix field at byte 345. The full filepath is <prefix>/<name>. Note the slash between the name and the prefix is implicit; it is neither present in the prefix nor in the name field. This means even when the complete filepath is more than 100 chars long, the name field might not entirely be filled. Therefore, the maximum length of the filepath is dependent on the exact locations of the slashes in the filepath.

I have filed a pull request which makes use of LongLinks instead of the prefix field. This makes arbitrarily long filepaths possible.