Skip to content

Agent-Hellboy/cpp-filetype

Repository files navigation

cpp-filetype

A C++ library for detecting file types based on their magic bytes.

Build

Documentation

Documentation

Installation

From Source

  1. Clone the repository:
$ git clone https://github.com/agent-hellboy/cpp-filetype.git
  1. Build and install:
$ rm -rf build
$ cd build
$ cmake -DBUILD_TESTS=ON ..
$ cmake --build .
$ ctest --output-on-failure

To install, run:

$ sudo cmake --install .

installation will do below:

  • install library to /usr/local/lib
  • install headers to /usr/local/include
  • install cmake configuration to /usr/local/lib/cmake/filetype
sudo cmake --install .
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/libfiletype.a
-- Up-to-date: /usr/local/include
-- Up-to-date: /usr/local/include/filetype
-- Up-to-date: /usr/local/include/filetype/types
-- Up-to-date: /usr/local/include/filetype/types/document.hpp
-- Up-to-date: /usr/local/include/filetype/types/archive.hpp
-- Up-to-date: /usr/local/include/filetype/types/audio.hpp
-- Up-to-date: /usr/local/include/filetype/types/video.hpp
-- Up-to-date: /usr/local/include/filetype/types/image.hpp
-- Up-to-date: /usr/local/include/filetype/type.hpp
-- Up-to-date: /usr/local/include/filetype/types.hpp
-- Up-to-date: /usr/local/include/filetype/filetype.hpp
-- Installing: /usr/local/lib/cmake/filetype/filetypeTargets.cmake
-- Installing: /usr/local/lib/cmake/filetype/filetypeTargets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/filetype/filetypeConfig.cmake
-- Installing: /usr/local/lib/cmake/filetype/filetypeConfigVersion.cmake

Usage

#include "filetype/filetype.hpp"

int main() {
    std::vector<uint8_t> data = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
    const ::filetype::Type* type = ::filetype::match(data);
    if (type) {
        std::cout << "Detected: " << type->mime << " (" << type->extension << ")\n";
    } else {
        std::cout << "Unknown file type\n";
    }
    return 0;
}

To build the example within the repository, ensure that you have successfully installed the library

g++ -std=c++17 example/file_detect.cpp -o file_detect -lfiletype

Development

Prerequisites for Development

Install pre-commit:

pip install pre-commit

Install and run pre-commit hooks:

pre-commit install
pre-commit run --all

License

MIT License (c) 2025 Prince Roshan

About

A minimal cpp based filetype library

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published