Files
Chronos/src/Titano.Core/Packaging/PayloadFormat.cs
T
Claude d79ab1e41b feat(packaging): add TPK1 self-extracting payload format
Installer payload appended to the setup program: JSON manifest (components,
files with size/SHA-256/offsets/mtime, shortcuts, file types) plus deflate or
stored entries and a 64-byte footer with the manifest hash. The reader finds
the footer before an Authenticode table, validates every offset, rejects
unsafe paths and bounds inflation by the declared size; the writer is
deterministic, deduplicates identical content and strips a signed stub.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:56:37 +00:00

53 lines
2.4 KiB
C#

using System.Buffers.Binary;
namespace Titano.Core.Packaging;
/// <summary>
/// The TPK1 self-extracting layout: <c>[setup stub][manifest JSON][entry data][zero padding][footer]</c>, optionally
/// followed by an Authenticode certificate table that signtool appends when the installer is signed.
/// The footer is 64 bytes, little-endian: magic <c>"TPK1END\0"</c> · payload offset (int64, absolute offset of the
/// manifest) · manifest length (int64) · data length (int64) · SHA-256 of the manifest bytes (32 bytes).
/// Entry offsets in the manifest are relative to the first byte after the manifest. Padding aligns the end of the
/// footer to 8 bytes so a signature can be appended without signtool inserting bytes of its own.
/// </summary>
public static class PayloadFormat
{
public const int FooterLength = 64;
public const int Alignment = 8;
public const int ManifestFormatVersion = 1;
/// <summary>Upper bound for the manifest; a larger value in a footer means corruption.</summary>
public const int MaxManifestLength = 64 << 20;
/// <summary>Deflate cannot expand data by more than about 1032:1; larger declared sizes are rejected up front.</summary>
public const int MaxDeflateRatio = 1032;
public static ReadOnlySpan<byte> Magic => "TPK1END\0"u8;
}
/// <summary>Decoded TPK1 footer.</summary>
internal readonly record struct PayloadFooter(long PayloadOffset, long ManifestLength, long DataLength, byte[] ManifestHash)
{
public void WriteTo(Span<byte> destination)
{
PayloadFormat.Magic.CopyTo(destination);
BinaryPrimitives.WriteInt64LittleEndian(destination[8..], PayloadOffset);
BinaryPrimitives.WriteInt64LittleEndian(destination[16..], ManifestLength);
BinaryPrimitives.WriteInt64LittleEndian(destination[24..], DataLength);
ManifestHash.CopyTo(destination[32..]);
}
/// <summary>Parses a footer; false when the magic does not match.</summary>
public static bool TryParse(ReadOnlySpan<byte> source, out PayloadFooter footer)
{
footer = default;
if (source.Length < PayloadFormat.FooterLength || !source[..8].SequenceEqual(PayloadFormat.Magic)) return false;
footer = new PayloadFooter(
BinaryPrimitives.ReadInt64LittleEndian(source[8..]),
BinaryPrimitives.ReadInt64LittleEndian(source[16..]),
BinaryPrimitives.ReadInt64LittleEndian(source[24..]),
source.Slice(32, 32).ToArray());
return true;
}
}