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

Add support for pre-POSIX tar #204

Merged
merged 3 commits into from Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added fixture/fixture-v7.tar
Binary file not shown.
30 changes: 29 additions & 1 deletion index.js
Expand Up @@ -16,6 +16,31 @@ function readUInt64LE(buf, offset = 0) {
return n;
}

const tarHeaderChecksumMatches = buf => { // Does not check if checksum field was valid
if (buf.length < 512) {
return false;
}

let sum = 256;
let signedBitCount = 0;

for (let i = 0; i < 148; i++) {
const byte = buf[i];
sum += byte;
signedBitCount += byte & 0x80;
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

for (let i = 156; i < 512; i++) {
const byte = buf[i];
sum += byte;
signedBitCount += byte & 0x80;
}

const readSum = parseInt(buf.toString('utf8', 148, 154), 8);

return sum === readSum || ((sum - (signedBitCount << 1)) === readSum);
};
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved

const fileType = input => {
if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) {
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
Expand Down Expand Up @@ -229,7 +254,10 @@ const fileType = input => {
};
}

if (check([0x75, 0x73, 0x74, 0x61, 0x72], {offset: 257})) {
if (
check([0x30, 0x30, 0x30, 0x30, 0x30, 0x30], {offset: 148, mask: [0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8]}) && // Valid tar checksum
tarHeaderChecksumMatches(buf)
) {
return {
ext: 'tar',
mime: 'application/x-tar'
Expand Down
4 changes: 4 additions & 0 deletions test.js
Expand Up @@ -191,6 +191,10 @@ const names = {
pcap: [
'fixture-big-endian',
'fixture-little-endian'
],
tar: [
'fixture',
'fixture-v7'
]
};

Expand Down