XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Buffers.Binary;
using System.IO.Compression;

namespace XFEWarframe.Client;

/// <summary>Bounded PNG encoding and integrity validation for original render captures.</summary>
public static class PngImages
{
    private static readonly byte[] Signature = [137, 80, 78, 71, 13, 10, 26, 10];
    private static readonly uint[] CrcTable = Enumerable.Range(0, 256).Select(i =>
    {
        var crc = (uint)i;
        for (var bit = 0; bit < 8; bit++) crc = (crc >> 1) ^ ((crc & 1) == 0 ? 0u : 0xedb88320u);
        return crc;
    }).ToArray();

    public static byte[] CropRgba(byte[] rgba, int frameWidth, int frameHeight, int left, int top, int width, int height)
    {
        if (frameWidth is < 1 or > 8192 || frameHeight is < 1 or > 8192 ||
            (long)frameWidth * frameHeight * 4 != rgba.LongLength || width is < 1 or > 4096 || height is < 1 or > 4096 ||
            left < 0 || top < 0 || (long)left + width > frameWidth || (long)top + height > frameHeight)
            throw new InvalidDataException("Card rectangle lies outside the render surface.");
        using var output = new MemoryStream(); output.Write(Signature);
        var header = new byte[13]; BinaryPrimitives.WriteInt32BigEndian(header, width);
        BinaryPrimitives.WriteInt32BigEndian(header.AsSpan(4), height); header[8] = 8; header[9] = 6;
        Chunk(output, "IHDR"u8, header);
        using var compressed = new MemoryStream();
        using (var zip = new ZLibStream(compressed, CompressionLevel.Fastest, true))
            for (var y = top; y < top + height; y++)
            {
                zip.WriteByte(0); zip.Write(rgba.AsSpan((y * frameWidth + left) * 4, width * 4));
            }
        Chunk(output, "IDAT"u8, compressed.ToArray()); Chunk(output, "IEND"u8, []);
        return output.ToArray();
    }

    public static void Validate(ReadOnlySpan<byte> png)
    {
        if (png.Length is < 57 or > 8_000_000 || !png[..8].SequenceEqual(Signature)) Invalid();
        var position = 8; var header = false; var data = false; var ended = false;
        while (position <= png.Length - 12)
        {
            var length = BinaryPrimitives.ReadInt32BigEndian(png.Slice(position, 4));
            if (length < 0 || length > png.Length - position - 12) Invalid();
            var type = png.Slice(position + 4, 4); var bytes = png.Slice(position + 8, length);
            if (Crc(type, bytes) != BinaryPrimitives.ReadUInt32BigEndian(png.Slice(position + 8 + length, 4))) Invalid();
            if (!header)
            {
                if (!type.SequenceEqual("IHDR"u8) || length != 13) Invalid();
                var width = BinaryPrimitives.ReadInt32BigEndian(bytes); var height = BinaryPrimitives.ReadInt32BigEndian(bytes[4..]);
                if (width is < 1 or > 8192 || height is < 1 or > 8192 || (long)width * height > 16_777_216 ||
                    bytes[10] != 0 || bytes[11] != 0 || bytes[12] > 1 || bytes[9] is not (0 or 2 or 3 or 4 or 6)) Invalid();
                if (bytes[9] switch { 0 => bytes[8] is not (1 or 2 or 4 or 8 or 16), 3 => bytes[8] is not (1 or 2 or 4 or 8), _ => bytes[8] is not (8 or 16) }) Invalid();
                header = true;
            }
            else if (type.SequenceEqual("IHDR"u8)) Invalid();
            if (type.SequenceEqual("IDAT"u8)) data = true;
            position += length + 12;
            if (type.SequenceEqual("IEND"u8)) { if (length != 0 || !data || position != png.Length) Invalid(); ended = true; break; }
        }
        if (!ended) Invalid();
    }

    private static void Invalid() => throw new InvalidDataException("Invalid or damaged PNG cache entry.");
    private static uint Crc(ReadOnlySpan<byte> type, ReadOnlySpan<byte> bytes)
    {
        var crc = uint.MaxValue;
        foreach (var b in type) crc = CrcTable[(crc ^ b) & 255] ^ (crc >> 8);
        foreach (var b in bytes) crc = CrcTable[(crc ^ b) & 255] ^ (crc >> 8);
        return ~crc;
    }
    private static void Chunk(Stream output, ReadOnlySpan<byte> type, ReadOnlySpan<byte> bytes)
    {
        Span<byte> word = stackalloc byte[4]; BinaryPrimitives.WriteInt32BigEndian(word, bytes.Length);
        output.Write(word); output.Write(type); output.Write(bytes);
        BinaryPrimitives.WriteUInt32BigEndian(word, Crc(type, bytes)); output.Write(word);
    }
}