Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions apps/web/src/components/files/binaryPreview.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,41 @@ it("formats singular and plural byte labels for SQLite BLOB cells", async () =>
vi.unstubAllGlobals();
}
});

it("reads GNU base-256 TAR modification dates, including ones before 1970", async () => {
const encode = (value: string) => new TextEncoder().encode(value);
const entries = [
{ path: "before-epoch.txt", mtime: new Uint8Array(12).fill(0xff) },
{ path: "far-future.txt", mtime: Uint8Array.of(0x80, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0) },
{ path: "out-of-range.txt", mtime: Uint8Array.of(0x80, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0) },
];
const bytes = new Uint8Array(entries.length * 512 + 1024);
entries.forEach((entry, index) => {
const offset = index * 512;
bytes.set(encode(entry.path), offset);
bytes.set(encode("00000000000\0"), offset + 124);
bytes.set(entry.mtime, offset + 136);
bytes.fill(32, offset + 148, offset + 156);
bytes.set(encode("0"), offset + 156);
const sum = bytes.subarray(offset, offset + 512).reduce((acc, byte) => acc + byte, 0);
bytes.set(encode(sum.toString(8).padStart(6, "0") + "\0 "), offset + 148);
});

try {
const request = await startWorker();
const result = await request({
type: "open",
kind: "archive",
name: "gnu.tar",
bytes: bytes.buffer,
});
if (result.type !== "archive") throw new Error(`Expected archive, got ${result.type}`);
expect(result.entries.map(({ modified }) => modified)).toEqual([
"1969-12-31T23:59:59.000Z",
"2242-03-16T12:56:32.000Z",
null,
]);
} finally {
vi.unstubAllGlobals();
}
});
10 changes: 9 additions & 1 deletion apps/web/src/components/files/binaryPreview.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
if (!/^[0-7]*$/.test(value)) throw new PreviewError({ code: "tar-numeric-format" });
return Number.parseInt(value || "0", 8);
};
const timestamp = (offset: number, length: number) => {
const first = bytes[offset]!;
if ((first & 0x80) === 0) return number(offset, length);
let value = BigInt(first & 0x40 ? first - 0x100 : first & 0x7f);
for (let index = 1; index < length; index++)
value = (value << 8n) | BigInt(bytes[offset + index]!);
return Number(value);
};
let longPath: string | null = null;
let longLink: string | null = null;
const globalPax: Record<string, string> = {};
Expand Down Expand Up @@ -134,7 +142,7 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
if (attributes.mtime && !/^[+-]?\d+(?:\.\d+)?$/.test(attributes.mtime))
throw new PreviewError({ code: "tar-metadata-damaged" });
const modified =
(attributes.mtime ? Number(attributes.mtime) : number(offset + 136, 12)) * 1000;
(attributes.mtime ? Number(attributes.mtime) : timestamp(offset + 136, 12)) * 1000;
const date = new Date(modified);
const linked = type === "1" ? files.get(linkpath) : undefined;
entries.push({
Expand Down
Loading