// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! PAX extended header records. //! //! A PAX extended header (typeflag `x`) is an ordinary member whose content //! is a list of records of the form `" =\n"`, where `` //! is the decimal length of the entire record *including its own digits*. //! They override fields of the header that follows: a name longer than the //! header has room for, a size of 8 GiB or more, a date before 1970. const std = @import("std"); /// The key names this library reads and writes. Every other key is passed /// over and reported. pub const key_path = "path"; pub const key_linkpath = "linkpath"; pub const key_size = "size"; pub const key_mtime = "mtime"; fn decimalDigits(n: usize) usize { var d: usize = 1; var v = n / 10; while (v != 0) : (v /= 10) d += 1; return d; } /// The length of a record for a key and value of these lengths. /// /// The length prefix counts itself, so it is found by iterating: guess the /// digits needed for everything else, then add them and see whether that /// carried into another digit. It settles within two rounds. pub fn recordLen(key_len: usize, value_len: usize) usize { // " " + key + "=" + value + "\n" const rest = key_len + value_len + 3; var digits = decimalDigits(rest); while (decimalDigits(rest + digits) != digits) digits = decimalDigits(rest + digits); return rest + digits; } /// Writes one record to the start of `buf`, returning how many bytes it /// took, or null if `buf` is too short to hold it. pub fn writeRecord(buf: []u8, key: []const u8, value: []const u8) ?usize { const len = recordLen(key.len, value.len); if (len > buf.len) return null; const written = std.fmt.bufPrint(buf, "{d} {s}={s}\n", .{ len, key, value }) catch unreachable; std.debug.assert(written.len == len); return len; } pub const ValueError = error{InvalidPaxRecord}; /// Parses a `size` value: plain decimal, nothing else. pub fn parseSize(value: []const u8) ValueError!u64 { if (value.len == 0) return error.InvalidPaxRecord; for (value) |c| if (c < '0' or c > '9') return error.InvalidPaxRecord; return std.fmt.parseInt(u64, value, 10) catch error.InvalidPaxRecord; } /// Parses an `mtime` value to whole seconds. It is decimal seconds with an /// optional sign and fraction, `-1.5` being a second and a half before the /// epoch; the fraction is dropped by rounding toward negative infinity, so /// that a time is never moved later than it was. pub fn parseTime(value: []const u8) ValueError!i64 { const dot = std.mem.findScalar(u8, value, '.'); const whole = value[0 .. dot orelse value.len]; const negative = whole.len > 0 and whole[0] == '-'; const digits = if (negative) whole[1..] else whole; if (digits.len == 0) return error.InvalidPaxRecord; for (digits) |c| if (c < '0' or c > '9') return error.InvalidPaxRecord; var has_fraction = false; if (dot) |d| { const fraction = value[d + 1 ..]; for (fraction) |c| { if (c < '0' or c > '9') return error.InvalidPaxRecord; if (c != '0') has_fraction = true; } } const magnitude = std.fmt.parseInt(u64, digits, 10) catch return error.InvalidPaxRecord; if (!negative) return std.math.cast(i64, magnitude) orelse error.InvalidPaxRecord; const floor = @as(i128, magnitude) + @intFromBool(has_fraction); return std.math.cast(i64, -floor) orelse error.InvalidPaxRecord; } const testing = std.testing; test recordLen { var buf: [4096]u8 = undefined; // Around every point where the length prefix gains a digit. for (0..1100) |value_len| { const value = buf[1024..][0..value_len]; @memset(value, 'v'); const n = writeRecord(buf[0..1024], "path", value) orelse continue; const space = std.mem.findScalar(u8, buf[0..n], ' ').?; try testing.expectEqual(n, try std.fmt.parseInt(usize, buf[0..space], 10)); try testing.expectEqual('\n', buf[n - 1]); } try testing.expectEqual(9, recordLen(1, 4)); try testing.expectEqual(11, recordLen(1, 5)); } test writeRecord { var buf: [32]u8 = undefined; const n = writeRecord(&buf, "path", "a/b").?; try testing.expectEqualStrings("12 path=a/b\n", buf[0..n]); try testing.expect(writeRecord(buf[0..11], "path", "a/b") == null); } test parseSize { try testing.expectEqual(0, try parseSize("0")); try testing.expectEqual(1 << 40, try parseSize("1099511627776")); try testing.expectEqual(std.math.maxInt(u64), try parseSize("18446744073709551615")); try testing.expectError(error.InvalidPaxRecord, parseSize("18446744073709551616")); try testing.expectError(error.InvalidPaxRecord, parseSize("")); try testing.expectError(error.InvalidPaxRecord, parseSize("+1")); try testing.expectError(error.InvalidPaxRecord, parseSize("1 ")); } test parseTime { try testing.expectEqual(1700000000, try parseTime("1700000000")); try testing.expectEqual(1700000000, try parseTime("1700000000.999999999")); try testing.expectEqual(-1, try parseTime("-1")); try testing.expectEqual(-2, try parseTime("-1.5")); try testing.expectEqual(-1, try parseTime("-1.000")); try testing.expectEqual(5, try parseTime("5.")); try testing.expectEqual(std.math.minInt(i64), try parseTime("-9223372036854775808")); try testing.expectError(error.InvalidPaxRecord, parseTime("-9223372036854775808.5")); try testing.expectError(error.InvalidPaxRecord, parseTime("9223372036854775808")); try testing.expectError(error.InvalidPaxRecord, parseTime("")); try testing.expectError(error.InvalidPaxRecord, parseTime("-")); try testing.expectError(error.InvalidPaxRecord, parseTime(".5")); try testing.expectError(error.InvalidPaxRecord, parseTime("1.5x")); }