< Summary

Information
Class: Anichron.Worker.Ingestion.Proxy.ImageCompleteness
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Proxy/ImageCompleteness.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 63
Line coverage: 100%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EnsureHeif(...)100%1010100%
LargeSize(...)100%22100%
EnsureJpeg(...)100%1414100%
SegmentLength(...)100%22100%
HeifEndsEarly()100%11100%
JpegEndsEarly()100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Proxy/ImageCompleteness.cs

#LineLine coverage
 1using System.Buffers.Binary;
 2
 3namespace Anichron.Worker.Ingestion.Proxy;
 4
 5internal static class ImageCompleteness
 6{
 7    private const byte markerPrefix = 0xFF;
 8    private const byte endOfImage = 0xD9;
 9
 10    public static void EnsureHeif(ReadOnlySpan<byte> heif)
 2311    {
 7112        while (!heif.IsEmpty)
 5913        {
 5914            if (!BinaryPrimitives.TryReadUInt32BigEndian(heif, out var compactSize))
 115                throw HeifEndsEarly();
 16
 5817            var size = compactSize == 1 ? LargeSize(heif) : compactSize;
 5618            if (size == 0)
 119                return;
 5520            if (size > (ulong)heif.Length)
 721                throw HeifEndsEarly();
 22
 4823            heif = heif[(int)size..];
 4824        }
 1325    }
 26
 27    private static ulong LargeSize(ReadOnlySpan<byte> box)
 1728        => box.Length >= 16 ? BinaryPrimitives.ReadUInt64BigEndian(box[8..]) : throw HeifEndsEarly();
 29
 30    public static void EnsureJpeg(ReadOnlySpan<byte> jpeg)
 5031    {
 5032        var rest = jpeg[2..];
 157033        while (true)
 157034        {
 157035            var prefix = rest.IndexOf(markerPrefix);
 157036            if (prefix < 0)
 1337                throw JpegEndsEarly();
 38
 155739            rest = rest[(prefix + 1)..];
 155740            var code = rest.IndexOfAnyExcept(markerPrefix);
 155741            if (code < 0)
 242                throw JpegEndsEarly();
 43
 155544            var marker = rest[code];
 155545            rest = rest[(code + 1)..];
 155546            if (marker == endOfImage)
 3547                return;
 152048            if (marker is 0x00 or 0x01 or (>= 0xD0 and <= 0xD7))
 127549                continue;
 50
 24551            rest = rest[Math.Min(SegmentLength(rest), rest.Length)..];
 24552        }
 3553    }
 54
 55    private static ushort SegmentLength(ReadOnlySpan<byte> segment)
 24556        => BinaryPrimitives.TryReadUInt16BigEndian(segment, out var length) ? length : (ushort)0;
 57
 58    private static IncompleteImageException HeifEndsEarly()
 1059        => new("The image data is incomplete: the file ends inside a HEIF box.");
 60
 61    private static IncompleteImageException JpegEndsEarly()
 1562        => new("The image data is incomplete: the file ends before the JPEG's end-of-image marker.");
 63}