Fix detection of some PAX TAR formats#762
Merged
sindresorhus merged 1 commit intomainfrom May 23, 2025
Merged
Conversation
Collaborator
Author
|
Just in case we may conclude we need this in the future, this is an updated version the TAR checksum algorithm able to detect this kind of TAR files. This needs to read multiple blocks. Problem is that I have no way to do recursive peek reads yet, so cannot be used in a detection easily. import { StringType } from 'token-types';
import { type ITokenizer } from 'strtok3';
export async function isTarHeaderChecksumMatches(tokenizer: ITokenizer): Promise<boolean> {
const blockSize = 512;
const typeflagOffset = 156;
let blockIndex = 0;
while (true) {
const header = new Uint8Array(blockSize);
const size = await tokenizer.peekBuffer(header, { mayBeLess: true });
console.debug(`Reading block at index ${blockIndex}, bytes read: ${size}`);
if (size < blockSize) {
console.debug('Less than 512 bytes read — exiting.');
break;
}
if (header.every(byte => byte === 0)) {
console.debug('Encountered empty block — assuming end of archive.');
break;
}
const typeflag = String.fromCharCode(header[typeflagOffset]);
console.debug(`typeflag: "${typeflag}"`);
const rawSumStr = new StringType(8, 'ascii').get(header, 148);
const cleanedSumStr = rawSumStr.replace(/\0.*$/, '').trim();
if (cleanedSumStr === '') {
console.debug('Checksum field empty or spaces — skipping block.');
await tokenizer.ignore(blockSize);
blockIndex++;
continue;
}
const readSum = Number.parseInt(cleanedSumStr, 8);
console.debug(`Raw checksum string: "${rawSumStr}", cleaned: "${cleanedSumStr}", parsed: ${readSum}`);
if (Number.isNaN(readSum)) {
console.debug('Checksum is NaN — skipping block.');
await tokenizer.ignore(blockSize);
blockIndex++;
continue;
}
let sum = 0;
for (let i = 0; i < blockSize; i++) {
sum += (i >= 148 && i < 156) ? 0x20 : header[i];
}
console.debug(`Calculated checksum: ${sum}`);
if (readSum === sum) {
console.debug('Checksum valid.');
if (typeflag !== 'g' && typeflag !== 'x') {
return true; // Found valid regular header
} else {
console.debug(`Skipping extended header of type "${typeflag}".`);
}
} else {
console.debug('Checksum mismatch — skipping block.');
}
await tokenizer.ignore(blockSize);
blockIndex++;
}
return false;
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It checks for 6-byte magic signature:
ustar\0, POSIX and PAX Tar, orustar, GNU Tar, orResolves: #760