-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use UTF8 encoding on Tar string fields #75902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
7209fe1
5a7c567
88f1b50
950313c
ceab870
2a64be0
e0a3392
b81998f
b7f6582
f9d2b26
a629811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,45 +378,45 @@ private int WriteName(Span<byte> buffer) | |
| } | ||
|
|
||
| // 'https://www.freebsd.org/cgi/man.cgi?tar(5)' | ||
| // If the pathname is too long to fit in the 100 bytes provided by the standard format, | ||
| // If the path name is too long to fit in the 100 bytes provided by the standard format, | ||
| // it can be split at any / character with the first portion going into the prefix field. | ||
| private int WriteUstarName(Span<byte> buffer) | ||
| { | ||
| // We can have a pathname as big as 256, prefix + '/' + name, | ||
| // We can have a path name as big as 256, prefix + '/' + name, | ||
| // the separator in between can be neglected as the reader will append it when it joins both fields. | ||
| const int MaxPathname = FieldLengths.Prefix + FieldLengths.Name + 1; | ||
| const int MaxPathName = FieldLengths.Prefix + 1 + FieldLengths.Name; | ||
|
|
||
| if (GetUtf8TextLength(_name) > MaxPathname) | ||
| if (GetUtf8TextLength(_name) > MaxPathName) | ||
| { | ||
| throw new ArgumentException(SR.TarEntryFieldExceedsMaxLength, ArgNameEntry); | ||
| throw new ArgumentException(SR.Format(SR.TarEntryFieldExceedsMaxLength, nameof(TarEntry.Name)), ArgNameEntry); | ||
| } | ||
|
|
||
| Span<byte> encodingBuffer = stackalloc byte[MaxPathname]; | ||
| Span<byte> encodingBuffer = stackalloc byte[MaxPathName]; | ||
| int encoded = Encoding.UTF8.GetBytes(_name, encodingBuffer); | ||
| ReadOnlySpan<byte> pathnameBytes = encodingBuffer.Slice(0, encoded); | ||
| ReadOnlySpan<byte> pathNameBytes = encodingBuffer.Slice(0, encoded); | ||
|
|
||
| // If the pathname is able to fit in Name, we can write it down there and avoid calculating Prefix. | ||
| if (pathnameBytes.Length <= FieldLengths.Name) | ||
| if (pathNameBytes.Length <= FieldLengths.Name) | ||
| { | ||
| return WriteLeftAlignedBytesAndGetChecksum(pathnameBytes, buffer.Slice(FieldLocations.Name, FieldLengths.Name)); | ||
| return WriteLeftAlignedBytesAndGetChecksum(pathNameBytes, buffer.Slice(FieldLocations.Name, FieldLengths.Name)); | ||
| } | ||
|
|
||
| int lastIdx = pathnameBytes.LastIndexOfAny(PathInternal.Utf8DirectorySeparators); | ||
| int lastIdx = pathNameBytes.LastIndexOfAny(PathInternal.Utf8DirectorySeparators); | ||
| scoped ReadOnlySpan<byte> name; | ||
| scoped ReadOnlySpan<byte> prefix; | ||
|
|
||
| if (lastIdx < 1) // splitting at the root is not allowed. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I copied this behavior from GNU Tar, it does not store one single separtor in the prefix, I assume we should do the same. No big deal to go the other way but "absolute path" is already very odd scenario for tar and I don't have a reason to not do the same. On top of that, I think we should instruct users to be aware about/avoid using absolute paths with Tar APIs. |
||
| { | ||
| name = pathnameBytes; | ||
| name = pathNameBytes; | ||
| prefix = default; | ||
| } | ||
| else | ||
| { | ||
| name = pathnameBytes.Slice(lastIdx + 1); | ||
| prefix = pathnameBytes.Slice(0, lastIdx); | ||
| name = pathNameBytes.Slice(lastIdx + 1); | ||
| prefix = pathNameBytes.Slice(0, lastIdx); | ||
| } | ||
|
|
||
| // At this point pathnameBytes.Length > 100. | ||
| // At this point path name is > 100. | ||
| // Attempt to split it in a way it can use prefix. | ||
| while (prefix.Length - name.Length > FieldLengths.Prefix) | ||
jozkee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
|
|
@@ -426,8 +426,8 @@ private int WriteUstarName(Span<byte> buffer) | |
| break; | ||
| } | ||
|
|
||
| name = pathnameBytes.Slice(lastIdx + 1); | ||
| prefix = pathnameBytes.Slice(0, lastIdx); | ||
| name = pathNameBytes.Slice(lastIdx + 1); | ||
| prefix = pathNameBytes.Slice(0, lastIdx); | ||
| } | ||
|
|
||
| if (prefix.Length <= FieldLengths.Prefix && name.Length <= FieldLengths.Name) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.