Looking for determine file type from the magic number

8.3k Views Asked by At

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?

4

There are 4 best solutions below

1
On

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

0
On

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

0
On

JmimeMagic is a java library for such

0
On

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;
    }

}