Skip to content

lib-x/sniffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIME Type Detection Library in zig

A Zig library for detecting MIME types from file content using binary signatures. Translated from golang http lib

Usage

Installation run zig fetch --save "git+https://github.com/lib-x/sniffer#v0.0.1" Then, in your build.zig's build function, add the following before b.installArtifact(exe):

const sniffer_dep = b.dependency("sniffer", .{
    .target = target,
    .optimize = optimize,
});
exe.addModule("sniffer", sniffer_dep.module("sniffer"));

an example

const std = @import("std");
const sniffer = @import("sniffer");

pub fn main() !void {
    // Get file data
    var file = try std.fs.cwd().openFile("example.dat", .{});
    defer file.close();
    
    var buffer: [512]u8 = undefined;
    const bytes_read = try file.readAll(&buffer);
    
    // Detect MIME type
    const mime_type = sniffer.detectContentType(buffer[0..bytes_read]);
    std.debug.print("MIME type: {s}\n", .{mime_type});
}

Features

  • Detects common file formats including:

    • HTML documents
    • XML documents
    • PDF files
    • Images (PNG, JPEG, GIF, WebP, BMP, ICO)
    • Audio (MP3, MIDI, WAVE, AIFF)
    • Video (MP4, WebM, AVI)
    • Fonts (TTF, OTF, WOFF, WOFF2)
    • Archives (ZIP, RAR, GZIP)
    • WebAssembly files
    • Text files with various encodings
  • Examines only the first 512 bytes of data

  • Falls back to application/octet-stream if format is unknown

  • Handles UTF BOM detection

  • Supports text format detection

Notes

  • The library uses signature-based detection, examining file headers and patterns
  • No dependencies other than Zig standard library
  • Thread-safe as it's purely functional
  • No memory allocation during detection

Return Value

Returns a string containing the MIME type, for example:

  • "text/html; charset=utf-8"
  • "image/png"
  • "application/pdf"
  • "text/plain; charset=utf-8"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages