Files
Chronos/tests/Chronos.Core.Tests/Metadata/Builders/IfdSpec.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

277 lines
11 KiB
C#

using System.Buffers.Binary;
using System.Text;
using Chronos.Core.Metadata.Tiff;
namespace Chronos.Core.Tests.Metadata.Builders;
/// <summary>Declarative IFD for synthetic test files: typed values, child-IFD pointers, data blobs and embedded
/// (MakerNote-style) IFDs. Serialised by <see cref="TiffFile"/> in either byte order.</summary>
internal sealed class IfdSpec
{
internal abstract record Entry(ushort Tag);
internal sealed record ValueEntry(ushort Tag, TiffType Type, object Data) : Entry(Tag);
internal sealed record ChildrenEntry(ushort Tag, IfdSpec[] Children) : Entry(Tag);
internal sealed record BlobEntry(ushort Tag, byte[] Data) : Entry(Tag);
internal sealed record EmbeddedEntry(ushort Tag, byte[] Prefix, IfdSpec Ifd, bool RelativeToTiff, bool LittleEndian) : Entry(Tag);
public List<Entry> Entries { get; } = [];
/// <summary>Next IFD in the chain (IFD1 after IFD0…).</summary>
public IfdSpec? Next { get; set; }
public IfdSpec Ascii(ushort tag, string value) => Add(new ValueEntry(tag, TiffType.Ascii, value));
public IfdSpec Short(ushort tag, params ushort[] values) => Add(new ValueEntry(tag, TiffType.Short, values));
public IfdSpec Long(ushort tag, params uint[] values) => Add(new ValueEntry(tag, TiffType.Long, values));
public IfdSpec Rational(ushort tag, params (uint, uint)[] values) => Add(new ValueEntry(tag, TiffType.Rational, values));
public IfdSpec SRational(ushort tag, int numerator, int denominator) => Add(new ValueEntry(tag, TiffType.SRational, new[] { (numerator, denominator) }));
public IfdSpec Undefined(ushort tag, byte[] data) => Add(new ValueEntry(tag, TiffType.Undefined, data));
public IfdSpec Bytes(ushort tag, params byte[] data) => Add(new ValueEntry(tag, TiffType.Byte, data));
/// <summary>LONG pointer(s) to child IFDs (Exif, GPS, SubIFDs, MakerNote sub-directories).</summary>
public IfdSpec Child(ushort tag, params IfdSpec[] children) => Add(new ChildrenEntry(tag, children));
/// <summary>LONG offset to <paramref name="data"/> (placed after all IFDs) plus an optional LONG length tag.</summary>
public IfdSpec Blob(ushort offsetTag, ushort? lengthTag, byte[] data)
{
Add(new BlobEntry(offsetTag, data));
return lengthTag is { } t ? Long(t, (uint)data.Length) : this;
}
public IfdSpec Strip(byte[] data) => Blob(TiffTags.StripOffsets, TiffTags.StripByteCounts, data);
/// <summary>UNDEFINED value made of <paramref name="prefix"/> and an IFD whose offsets are relative to the TIFF base
/// (<paramref name="relativeToTiff"/>) or to the start of the value itself.</summary>
public IfdSpec Embedded(ushort tag, byte[] prefix, IfdSpec ifd, bool relativeToTiff, bool littleEndian) =>
Add(new EmbeddedEntry(tag, prefix, ifd, relativeToTiff, littleEndian));
public IfdSpec Without(params ushort[] tags)
{
Entries.RemoveAll(e => tags.Contains(e.Tag));
return this;
}
private IfdSpec Add(Entry entry)
{
Entries.RemoveAll(e => e.Tag == entry.Tag);
Entries.Add(entry);
return this;
}
}
/// <summary>Serialises <see cref="IfdSpec"/> trees: IFDs in depth-first order, each followed by its values, blobs last.</summary>
internal static class TiffFile
{
/// <summary>A complete TIFF file/block with header (<paramref name="magic"/> overrides "II*\0"/"MM\0*", e.g. "IIRO");
/// <paramref name="headerExtra"/> follows the 8-byte header (the CR2 "CR" marker block).</summary>
public static byte[] Build(IfdSpec ifd0, bool littleEndian = true, string? magic = null, byte[]? headerExtra = null)
{
var w = new IfdWriter(littleEndian, 0);
w.Raw(Encoding.Latin1.GetBytes(magic ?? (littleEndian ? "II*\0" : "MM\0*")));
w.Raw(new byte[4]);
if (headerExtra is not null) w.Raw(headerExtra);
w.PatchU32(4, (uint)w.WriteChain(ifd0));
w.FlushBlobs();
return w.ToArray();
}
/// <summary>An IFD and its values laid out as if placed at <paramref name="origin"/> relative to the offset base.</summary>
public static byte[] Block(IfdSpec ifd, bool littleEndian, int origin)
{
var w = new IfdWriter(littleEndian, origin);
w.WriteChain(ifd);
w.FlushBlobs();
return w.ToArray();
}
private sealed class IfdWriter(bool le, int origin)
{
private readonly List<byte> _b = [];
private readonly List<(int Patch, byte[] Data)> _blobs = [];
public byte[] ToArray() => _b.ToArray();
public void Raw(byte[] bytes) => _b.AddRange(bytes);
public int WriteChain(IfdSpec spec)
{
var position = WriteIfd(spec, out var nextPatch);
if (spec.Next is { } next) PatchU32(nextPatch, (uint)WriteChain(next));
return position;
}
public void FlushBlobs()
{
foreach (var (patch, data) in _blobs)
{
Align();
PatchU32(patch, (uint)(origin + _b.Count));
_b.AddRange(data);
}
_blobs.Clear();
}
public void PatchU32(int at, uint value)
{
Span<byte> s = stackalloc byte[4];
if (le) BinaryPrimitives.WriteUInt32LittleEndian(s, value);
else BinaryPrimitives.WriteUInt32BigEndian(s, value);
for (var i = 0; i < 4; i++) _b[at + i] = s[i];
}
private void PatchU16(int at, ushort value)
{
Span<byte> s = stackalloc byte[2];
if (le) BinaryPrimitives.WriteUInt16LittleEndian(s, value);
else BinaryPrimitives.WriteUInt16BigEndian(s, value);
_b[at] = s[0];
_b[at + 1] = s[1];
}
private void Align()
{
if ((origin + _b.Count) % 2 != 0) _b.Add(0);
}
private int WriteIfd(IfdSpec spec, out int nextPatch)
{
Align();
var start = _b.Count;
var entries = spec.Entries.OrderBy(e => e.Tag).ToList();
_b.AddRange(new byte[2 + 12 * entries.Count + 4]);
PatchU16(start, (ushort)entries.Count);
nextPatch = start + 2 + 12 * entries.Count;
var children = new List<(int Patch, IfdSpec Child)>();
for (var i = 0; i < entries.Count; i++)
{
var at = start + 2 + 12 * i;
var e = entries[i];
PatchU16(at, e.Tag);
switch (e)
{
case IfdSpec.ValueEntry v:
var (bytes, count) = Encode(v);
PutValue(at, v.Type, count, bytes);
break;
case IfdSpec.ChildrenEntry c:
PatchU16(at + 2, (ushort)TiffType.Long);
PatchU32(at + 4, (uint)c.Children.Length);
if (c.Children.Length == 1)
{
children.Add((at + 8, c.Children[0]));
}
else
{
Align();
PatchU32(at + 8, (uint)(origin + _b.Count));
for (var k = 0; k < c.Children.Length; k++)
{
children.Add((_b.Count, c.Children[k]));
_b.AddRange(new byte[4]);
}
}
break;
case IfdSpec.BlobEntry blob:
PatchU16(at + 2, (ushort)TiffType.Long);
PatchU32(at + 4, 1);
_blobs.Add((at + 8, blob.Data));
break;
case IfdSpec.EmbeddedEntry m:
Align();
var position = origin + _b.Count;
var block = Block(m.Ifd, m.LittleEndian, m.RelativeToTiff ? position + m.Prefix.Length : m.Prefix.Length);
PatchU16(at + 2, (ushort)TiffType.Undefined);
PatchU32(at + 4, (uint)(m.Prefix.Length + block.Length));
PatchU32(at + 8, (uint)position);
_b.AddRange(m.Prefix);
_b.AddRange(block);
break;
}
}
foreach (var (patch, child) in children) PatchU32(patch, (uint)WriteChain(child));
return origin + start;
}
private void PutValue(int at, TiffType type, uint count, byte[] bytes)
{
PatchU16(at + 2, (ushort)type);
PatchU32(at + 4, count);
if (bytes.Length <= 4)
{
for (var i = 0; i < bytes.Length; i++) _b[at + 8 + i] = bytes[i];
return;
}
Align();
PatchU32(at + 8, (uint)(origin + _b.Count));
_b.AddRange(bytes);
}
private (byte[] Bytes, uint Count) Encode(IfdSpec.ValueEntry v)
{
switch (v.Data)
{
case string s:
var ascii = Encoding.UTF8.GetBytes(s + "\0");
return (ascii, (uint)ascii.Length);
case byte[] raw:
return (raw, (uint)raw.Length);
case ushort[] shorts:
{
var b = new byte[2 * shorts.Length];
for (var i = 0; i < shorts.Length; i++) Write16(b.AsSpan(2 * i), shorts[i]);
return (b, (uint)shorts.Length);
}
case uint[] longs:
{
var b = new byte[4 * longs.Length];
for (var i = 0; i < longs.Length; i++) Write32(b.AsSpan(4 * i), longs[i]);
return (b, (uint)longs.Length);
}
case (uint, uint)[] rationals:
{
var b = new byte[8 * rationals.Length];
for (var i = 0; i < rationals.Length; i++)
{
Write32(b.AsSpan(8 * i), rationals[i].Item1);
Write32(b.AsSpan(8 * i + 4), rationals[i].Item2);
}
return (b, (uint)rationals.Length);
}
case (int, int)[] signed:
{
var b = new byte[8 * signed.Length];
for (var i = 0; i < signed.Length; i++)
{
Write32(b.AsSpan(8 * i), (uint)signed[i].Item1);
Write32(b.AsSpan(8 * i + 4), (uint)signed[i].Item2);
}
return (b, (uint)signed.Length);
}
default:
throw new InvalidOperationException("Unsupported test value " + v.Data.GetType());
}
}
private void Write16(Span<byte> s, ushort v)
{
if (le) BinaryPrimitives.WriteUInt16LittleEndian(s, v);
else BinaryPrimitives.WriteUInt16BigEndian(s, v);
}
private void Write32(Span<byte> s, uint v)
{
if (le) BinaryPrimitives.WriteUInt32LittleEndian(s, v);
else BinaryPrimitives.WriteUInt32BigEndian(s, v);
}
}
}