~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
90 lines · 3.8 KB Raw History
const std = @import("std");

pub const sniff_bytes = 4100;

pub fn detect(bytes: []const u8, fallback: []const u8) []const u8 {
    return sniff(bytes) orelse fallback;
}

fn sniff(b: []const u8) ?[]const u8 {
    const signatures = .{
        .{ "\x89PNG\r\n\x1a\n", "image/png" },
        .{ "\xff\xd8\xff", "image/jpeg" },
        .{ "GIF87a", "image/gif" },
        .{ "GIF89a", "image/gif" },
        .{ "BM", "image/bmp" },
        .{ "II\x2a\x00", "image/tiff" },
        .{ "MM\x00\x2a", "image/tiff" },
        .{ "\x00\x00\x01\x00", "image/x-icon" },
        .{ "fLaC", "audio/flac" },
        .{ "ID3", "audio/mpeg" },
        .{ "#!AMR\n", "audio/amr" },
        .{ "%PDF-", "application/pdf" },
        .{ "PK\x03\x04", "application/zip" },
        .{ "PK\x05\x06", "application/zip" },
        .{ "\x1f\x8b\x08", "application/gzip" },
    };
    inline for (signatures) |signature| {
        if (std.mem.startsWith(u8, b, signature[0])) return signature[1];
    }
    if (at(b, 0, "RIFF")) {
        if (at(b, 8, "WEBP")) return "image/webp";
        if (at(b, 8, "WAVE")) return "audio/wav";
        if (at(b, 8, "AVI ")) return "video/vnd.avi";
    }
    if (at(b, 0, "FORM") and (at(b, 8, "AIFF") or at(b, 8, "AIFC"))) return "audio/aiff";
    if (at(b, 0, "OggS")) {
        if (at(b, 28, "OpusHead")) return "audio/ogg";
        if (at(b, 28, "\x01vorbis")) return "audio/ogg";
        if (at(b, 28, "\x80theora")) return "video/ogg";
        return "application/ogg";
    }
    if (b.len >= 12 and at(b, 4, "ftyp")) {
        const brand = b[8..12];
        if (std.mem.eql(u8, brand, "avif") or std.mem.eql(u8, brand, "avis")) return "image/avif";
        if (std.mem.eql(u8, brand, "heic") or std.mem.eql(u8, brand, "heix")) return "image/heic";
        if (std.mem.eql(u8, brand, "mif1")) return "image/heif";
        if (std.mem.eql(u8, brand, "M4A ")) return "audio/x-m4a";
        if (std.mem.eql(u8, brand, "qt  ")) return "video/quicktime";
        return "video/mp4";
    }
    if (at(b, 0, "\x1a\x45\xdf\xa3")) {
        if (std.mem.indexOf(u8, b, "webm") != null) return "video/webm";
        if (std.mem.indexOf(u8, b, "matroska") != null) return "video/x-matroska";
    }
    if (b.len >= 2 and b[0] == 0xff) {
        if (b[1] & 0xf6 == 0xf0) return "audio/aac";
        if (b[1] & 0xe0 == 0xe0 and b[1] & 0x06 != 0) return "audio/mpeg";
    }
    return null;
}

fn at(bytes: []const u8, offset: usize, signature: []const u8) bool {
    return bytes.len >= offset and std.mem.startsWith(u8, bytes[offset..], signature);
}

test "detect common image video and audio formats before considering the header" {
    const fixtures = .{
        .{ "\x89PNG\r\n\x1a\n", "image/png" },
        .{ "\xff\xd8\xff\xe0", "image/jpeg" },
        .{ "GIF89a", "image/gif" },
        .{ "RIFF\x00\x00\x00\x00WEBP", "image/webp" },
        .{ "\x00\x00\x00\x18ftypavif", "image/avif" },
        .{ "\x00\x00\x00\x18ftypheic", "image/heic" },
        .{ "\x00\x00\x00\x18ftypisom", "video/mp4" },
        .{ "\x00\x00\x00\x18ftypM4A ", "audio/x-m4a" },
        .{ "\x1a\x45\xdf\xa3\x87\x42\x82\x84webm", "video/webm" },
        .{ "fLaC", "audio/flac" },
        .{ "ID3\x04\x00", "audio/mpeg" },
        .{ "\xff\xfb", "audio/mpeg" },
        .{ "\xff\xf1", "audio/aac" },
        .{ "RIFF\x00\x00\x00\x00WAVE", "audio/wav" },
    };
    inline for (fixtures) |fixture| {
        try std.testing.expectEqualStrings(fixture[1], detect(fixture[0], "text/plain"));
        for (0..fixture[0].len) |length| _ = detect(fixture[0][0..length], "application/octet-stream");
    }
    try std.testing.expectEqualStrings("text/plain", detect("unknown content", "text/plain"));
    try std.testing.expectEqualStrings("application/octet-stream", detect("", "application/octet-stream"));
    try std.testing.expectEqualStrings("text/plain", detect("\x89PNG", "text/plain"));
}