CheapD AKA Ju
CheapD AKA Ju

Reputation: 721

Looking for determine file type from the magic number

It is possible to determine the file type from the magic number of file?

If I've understood, the magic number can have different size, maybe a reference dictionary or something like a library could help me?

Upvotes: 1

Views: 9020

Answers (4)

gerardw
gerardw

Reputation: 6330

Use libmagic (apt-get install libmagic-dev on Ubuntu systems).

Example below uses the default magic database to query the file passed on the command line. (Essentially an implementation of the file command. See man libmagic for more details/functions.

#include <iostream>
#include <magic.h>
#include <cassert>
int main(int argc, char **argv) {
    if (argc == 1) {
            std::cerr << "Usage "  << argv[0] << " [filename]" << std::endl;
            return -1;
    }
    const char * fname = argv[1];
    magic_t cookie = magic_open(0);
    assert (cookie !=nullptr);
    int rc = magic_load(cookie, nullptr);
    assert(rc == 0);
    auto f=  magic_file(cookie, fname);
    if (f ==nullptr) {
        std::cerr << magic_error(cookie) << std::endl;
    } else {
        std::cout << fname << ' ' << f << std::endl;
    }

}

Upvotes: 0

Monish Sen
Monish Sen

Reputation: 1888

JmimeMagic is a java library for such

Upvotes: 0

TheCodeArtist
TheCodeArtist

Reputation: 22477

The file command on Linux does precisely that. Study its internals to see how it identifies files using their magic-numbers(signature bytes). The complete source-code is available at darwinsys.com/file.

The following 2 lists are the most comprehensive ones with file-types and their magic-numbers:
- File Signature Table
- Linux Magic Numbers

Upvotes: 0

Gangadhar
Gangadhar

Reputation: 10516

it is possible to determine the file type from the magic number of file

yes you can ,because each file format has different magic number.

for example FFD8 for .jpg files

See here Magic Numbers in Files

Upvotes: 1

Related Questions