Files
Chronos/tests/Chronos.Core.Tests/Metadata/Builders/JpegBuilder.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

150 lines
6.6 KiB
C#

using System.Buffers.Binary;
using System.Text;
namespace Chronos.Core.Tests.Metadata.Builders;
/// <summary>Synthetic JPEG: SOI, arbitrary segments/raw bytes, an optional APP2 MPF index of appended secondary
/// images, SOFn, SOS with a fake scan, EOI. No real entropy data: parsers stop at SOS.</summary>
internal sealed class JpegBuilder
{
public const string XmpSignature = "http://ns.adobe.com/xap/1.0/\0";
public const string ExtendedXmpSignature = "http://ns.adobe.com/xmp/extension/\0";
private readonly List<byte[]> _parts = [];
private readonly List<(byte[] Jpeg, uint Type)> _secondary = [];
private int _mpfPart = -1;
/// <summary>A minimal JPEG with just a frame header (used as embedded previews).</summary>
public static byte[] Minimal(int width, int height, byte sofMarker = 0xC0, int precision = 8, int scanBytes = 32) =>
new JpegBuilder().Build(width, height, sofMarker, precision, scanBytes);
public JpegBuilder Segment(byte marker, byte[] payload)
{
var s = new byte[4 + payload.Length];
s[0] = 0xFF;
s[1] = marker;
BinaryPrimitives.WriteUInt16BigEndian(s.AsSpan(2), (ushort)(payload.Length + 2));
payload.CopyTo(s, 4);
_parts.Add(s);
return this;
}
/// <summary>Bytes inserted verbatim (fill bytes, garbage).</summary>
public JpegBuilder Raw(params byte[] bytes)
{
_parts.Add(bytes);
return this;
}
public JpegBuilder Exif(byte[] tiff) => Segment(0xE1, [.. "Exif\0\0"u8, .. tiff]);
public JpegBuilder Xmp(string packet) => Segment(0xE1, [.. Encoding.ASCII.GetBytes(XmpSignature), .. Encoding.UTF8.GetBytes(packet)]);
/// <summary>Extended XMP split into <paramref name="chunk"/>-byte APP1 segments (written in reverse order to prove
/// reassembly uses offsets).</summary>
public JpegBuilder ExtendedXmp(string guid, string packet, int chunk)
{
var data = Encoding.UTF8.GetBytes(packet);
var segments = new List<byte[]>();
for (var offset = 0; offset < data.Length; offset += chunk)
{
var piece = data.AsSpan(offset, Math.Min(chunk, data.Length - offset));
var payload = new byte[ExtendedXmpSignature.Length + 32 + 8 + piece.Length];
Encoding.ASCII.GetBytes(ExtendedXmpSignature).CopyTo(payload, 0);
Encoding.ASCII.GetBytes(guid).CopyTo(payload, ExtendedXmpSignature.Length);
BinaryPrimitives.WriteUInt32BigEndian(payload.AsSpan(ExtendedXmpSignature.Length + 32), (uint)data.Length);
BinaryPrimitives.WriteUInt32BigEndian(payload.AsSpan(ExtendedXmpSignature.Length + 36), (uint)offset);
piece.CopyTo(payload.AsSpan(ExtendedXmpSignature.Length + 40));
segments.Add(payload);
}
segments.Reverse();
foreach (var s in segments) Segment(0xE1, s);
return this;
}
/// <summary>APP2 MPF index; each secondary JPEG is appended after the primary's EOI with the given MP type code.</summary>
public JpegBuilder Mpf(params (byte[] Jpeg, uint Type)[] secondary)
{
_secondary.AddRange(secondary);
_mpfPart = _parts.Count;
_parts.Add([]);
return this;
}
public byte[] Build(int width, int height, byte sofMarker = 0xC0, int precision = 8, int scanBytes = 32)
{
var tail = new List<byte>();
tail.AddRange(SegmentBytes(sofMarker, [(byte)precision, (byte)(height >> 8), (byte)height, (byte)(width >> 8), (byte)width, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1]));
tail.AddRange(SegmentBytes(0xDA, [3, 1, 0, 2, 0x11, 3, 0x11, 0, 0x3F, 0]));
for (var i = 0; i < scanBytes; i++) tail.Add((byte)(i * 37 % 255));
tail.AddRange([0xFF, 0xD9]);
if (_mpfPart >= 0) _parts[_mpfPart] = SegmentBytes(0xE2, MpfPayload(new uint[_secondary.Count], 0));
var headLength = 2 + _parts.Sum(p => p.Length);
var primaryLength = headLength + tail.Count;
if (_mpfPart >= 0)
{
var mpHeader = 2 + _parts.Take(_mpfPart).Sum(p => p.Length) + 4 + 4;
var offsets = new uint[_secondary.Count];
var at = primaryLength;
for (var i = 0; i < offsets.Length; i++)
{
offsets[i] = (uint)(at - mpHeader);
at += _secondary[i].Jpeg.Length;
}
_parts[_mpfPart] = SegmentBytes(0xE2, MpfPayload(offsets, primaryLength));
}
var file = new List<byte>(primaryLength);
file.AddRange([0xFF, 0xD8]);
foreach (var p in _parts) file.AddRange(p);
file.AddRange(tail);
foreach (var (jpeg, _) in _secondary) file.AddRange(jpeg);
return file.ToArray();
byte[] MpfPayload(uint[] offsets, int primarySize)
{
// "MPF\0", big-endian MP header, index IFD (version, count, entries), then 16-byte MP entries.
var n = offsets.Length + 1;
var b = new byte[4 + 8 + 2 + 3 * 12 + 4 + 16 * n];
var s = b.AsSpan();
"MPF\0MM\0*"u8.CopyTo(s);
BinaryPrimitives.WriteUInt32BigEndian(s[8..], 8);
var ifd = s[12..];
BinaryPrimitives.WriteUInt16BigEndian(ifd, 3);
WriteEntry(ifd[2..], 0xB000, 7, 4, BinaryPrimitives.ReadUInt32BigEndian("0100"u8));
WriteEntry(ifd[14..], 0xB001, 4, 1, (uint)n);
WriteEntry(ifd[26..], 0xB002, 7, (uint)(16 * n), 8 + 2 + 36 + 4);
var entries = s[(4 + 8 + 2 + 36 + 4)..];
BinaryPrimitives.WriteUInt32BigEndian(entries, 0x20030000);
BinaryPrimitives.WriteUInt32BigEndian(entries[4..], (uint)primarySize);
for (var i = 0; i < offsets.Length; i++)
{
var e = entries[(16 * (i + 1))..];
BinaryPrimitives.WriteUInt32BigEndian(e, _secondary[i].Type);
BinaryPrimitives.WriteUInt32BigEndian(e[4..], (uint)_secondary[i].Jpeg.Length);
BinaryPrimitives.WriteUInt32BigEndian(e[8..], offsets[i]);
}
return b;
}
}
private static void WriteEntry(Span<byte> s, ushort tag, ushort type, uint count, uint value)
{
BinaryPrimitives.WriteUInt16BigEndian(s, tag);
BinaryPrimitives.WriteUInt16BigEndian(s[2..], type);
BinaryPrimitives.WriteUInt32BigEndian(s[4..], count);
BinaryPrimitives.WriteUInt32BigEndian(s[8..], value);
}
private static byte[] SegmentBytes(byte marker, byte[] payload)
{
var s = new byte[4 + payload.Length];
s[0] = 0xFF;
s[1] = marker;
BinaryPrimitives.WriteUInt16BigEndian(s.AsSpan(2), (ushort)(payload.Length + 2));
payload.CopyTo(s, 4);
return s;
}
}