Skip to content
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

Make parsing of file names more robust #8

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions tar-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ export class TarExtractor {
parseHeader(buffer: Buffer, paxHeaderData: Record<string, any>): TarFileHeader | null {
const h = new TarFileHeader();

h.name = buffer.subarray(0, 100).toString().replace(/\0/g, '');
h.name = buffer.subarray(0, 100).toString('utf8').replace(/\0+$/, '');
// XXX: Remove this debug print later
this.__log(`parsed file name '${h.name}' from header block of size ${buffer.length}`);
if (!h.name) {
return null;
}

// 8 bytes for mode
// 8 bytes for uid
// 8 bytes for gid
h.size = parseInt(buffer.subarray(124, 136).toString(), 8);
h.size = parseInt(buffer.subarray(124, 136).toString('utf8'), 8);
// 12 bytes for mtime
// 8 bytes for checksum
h.typeFlag = buffer.subarray(156, 157).toString();
h.typeFlag = buffer.subarray(156, 157).toString('utf8');
// 100 bytes for linkname
h.ustarIndicator = buffer.subarray(257, 263).toString();
h.prefix = buffer.subarray(345, 500).toString().replace(/\0/g, '');
h.ustarIndicator = buffer.subarray(257, 263).toString('utf8');
h.prefix = buffer.subarray(345, 500).toString('utf8').replace(/\0+$/, '');
if (h.prefix) {
h.name = `${h.prefix}/${h.name}`;
}
Expand Down
Loading