Skip to content
zit Artifact [5b94296757]
Login

Artifact [5b94296757]

Artifact 5b94296757fd79e01c0a4d8e87eb220603d6bce411f88d82f7b1f4a2c87b1a5f:


//! Artifact names and log-frame keys: the hex NAME a repository shows and the
//! raw digest its log is keyed by, converted in both directions. Free of the
//! CLI, so the core and the daemon can share it with the verbs.

const std = @import("std");

/// SHA3-256 digest length: the on-disk log frame is keyed by these 32 raw bytes
/// (logstore.max_hash_len), while the artifact NAME is their 64-hex form. `init`
/// wrote the SHA3-256 policy (B-commit-16), so the algorithm is this constant.
pub const digest_len = 32;

/// Decodes an artifact name into its binary digest — the log-frame key — writing
/// into `out` and returning the used prefix. Returns null for anything that is
/// not a name this suite mints, so the caller maps it to a diagnostic rather
/// than trapping.
///
/// LENGTH IS DERIVED FROM THE NAME, NOT ASSUMED. [FossilArtifact] §7.2 admits
/// two widths — 20 bytes (sha1) and 32 bytes (sha3-256) — and a repository is
/// free to hold either. This used to require exactly `digest_len * 2` hex
/// characters because `zit init` writes the sha3-256 policy, which made the
/// sha3 case look like the only case. It is not: `zit import-git` opens its
/// destination under git's native sha1, so every check-in in an imported
/// repository is named in 40 hex characters. The old check rejected all of
/// them, and `zit commit` reported the parent check-in as corrupt on a store
/// `zit verify` called clean.
///
/// The buffer is `digest_len` wide because that is the larger of the two; a
/// 20-byte digest uses the prefix. Callers pass the returned SLICE onward —
/// passing `&out` would reintroduce the bug with the padding included.
pub fn hexToDigest(hex: []const u8, out: *[digest_len]u8) ?[]const u8 {
    if (hex.len != 40 and hex.len != digest_len * 2) return null;
    const n = hex.len / 2;
    _ = std.fmt.hexToBytes(out[0..n], hex) catch return null;
    return out[0..n];
}

/// Lowercase-hex-encodes a raw binary `digest` into `out`, returning the used
/// prefix — the log-frame key's wire NAME (sync §3.3). The inverse of
/// `decodeWireName`; both live at the log↔store boundary so no other module
/// sees a raw digest cross into wire space.
pub fn digestToWireName(digest: []const u8, out: *[2 * digest_len]u8) []const u8 {
    std.debug.assert(digest.len <= digest_len);
    const hexdigits = "0123456789abcdef";
    for (digest, 0..) |b, i| {
        out[2 * i] = hexdigits[b >> 4];
        out[2 * i + 1] = hexdigits[b & 0x0f];
    }
    return out[0 .. 2 * digest.len];
}

/// Decodes a wire artifact NAME (40- or 64-hex, sync §3.3) into its raw binary
/// digest — the log-frame key. Returns null for anything that is not an
/// even-length hex string of at most `digest_len` bytes, so a malformed peer
/// name is rejected cleanly rather than trapping. The inverse of
/// `digestToWireName`.
pub fn decodeWireName(name: []const u8, out: *[digest_len]u8) ?[]const u8 {
    if (name.len == 0 or name.len % 2 != 0) return null;
    const nbytes = name.len / 2;
    if (nbytes > digest_len) return null;
    _ = std.fmt.hexToBytes(out[0..nbytes], name) catch return null;
    return out[0..nbytes];
}